app/Customize/EventSubscriber/ProductHistoryEventSubscriber.php line 132

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of ProductHistory
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\EventSubscriber;
  13. use Customize\Service\ProductHistory\ProductCollection;
  14. use Eccube\Common\EccubeConfig;
  15. use Eccube\Event\TemplateEvent;
  16. use Eccube\Repository\ProductRepository;
  17. use Eccube\Request\Context;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\Cookie;
  21. //use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
  22. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent as FilterControllerArgumentsEvent;
  23. //use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  24. use Symfony\Component\HttpKernel\Event\ResponseEvent as FilterResponseEvent;
  25. use Symfony\Component\HttpKernel\Event\KernelEvent;
  26. use Symfony\Component\HttpKernel\KernelEvents;
  27. /**
  28.  * Class ProductHistoryEventSubscriber
  29.  * @package Customize\EventSubscriber
  30.  */
  31. class ProductHistoryEventSubscriber implements EventSubscriberInterface
  32. {
  33.     const COOKIE_NAME 'product_history';
  34.     /**
  35.      * @var ProductRepository
  36.      */
  37.     private $productRepository;
  38.     /**
  39.      * @var EccubeConfig
  40.      */
  41.     private $eccubeConfig;
  42.     /**
  43.      * @var Context
  44.      */
  45.     private $context;
  46.     /**
  47.      * @var EventDispatcherInterface
  48.      */
  49.     private $eventDispatcher;
  50.     public function __construct(
  51.         ProductRepository $productRepository,
  52.         EccubeConfig $eccubeConfig,
  53.         Context $context,
  54.         EventDispatcherInterface $eventDispatcher
  55.     )
  56.     {
  57.         $this->productRepository $productRepository;
  58.         $this->eccubeConfig $eccubeConfig;
  59.         $this->context $context;
  60.         $this->eventDispatcher $eventDispatcher;
  61.     }
  62.     /**
  63.      * @return array
  64.      */
  65.     public static function getSubscribedEvents(): array
  66.     {
  67.         return [
  68.             KernelEvents::RESPONSE => 'onKernelResponse',
  69.             KernelEvents::CONTROLLER_ARGUMENTS => 'onKernelControllerArguments'
  70.         ];
  71.     }
  72.     /**
  73.      * 商品詳細ページにアクセスしたら商品IDをCookieに保存
  74.      *
  75.      * @param FilterResponseEvent $event
  76.      * @throws \Exception
  77.      */
  78.     public function onKernelResponse(FilterResponseEvent $event): void
  79.     {
  80.         if (false === $event->isMasterRequest()) {
  81.             return;
  82.         }
  83.         if ($event->getRequest()->get('_route') !== 'product_detail') {
  84.             return;
  85.         }
  86.         if ($product_id $event->getRequest()->get('id')) {
  87.             $product $this->productRepository->find($product_id);
  88.             if (null === $product) {
  89.                 return;
  90.             }
  91.             $cookie $this->getCookie($event);
  92.             // 商品IDを追加
  93.             $products = new ProductCollection($cookie5);
  94.             $products->addProduct($product);
  95.             // Cookie作成・更新
  96.             $cookie $this->createCookie($products);
  97.             $response $event->getResponse();
  98.             $response->headers->setCookie($cookie);
  99.             $event->setResponse($response);
  100.         }
  101.     }
  102.     /**
  103.      * チェックした商品をフロント側のすべてのTwigテンプレートにセット
  104.      *
  105.      * @param FilterControllerArgumentsEvent $event
  106.      */
  107.     public function onKernelControllerArguments(FilterControllerArgumentsEvent $event): void
  108.     {
  109.         if ($this->context->isAdmin()) {
  110.             return;
  111.         }
  112.         if ($event->getRequest()->attributes->has('_template')) {
  113.             $cookie $this->getCookie($event);
  114.             $template $event->getRequest()->attributes->get('_template');
  115.             $this->eventDispatcher->addListener($template->getTemplate(), function (TemplateEvent $templateEvent) use ($cookie) {
  116.                 $productHistory = [];
  117.                 $products = new ProductCollection($cookie);
  118.                 if ($products->count() > 0) {
  119.                     foreach ($products as $product) {
  120.                         if ($product $this->productRepository->find($product)) {
  121.                             $productHistory[] = $product;
  122.                         }
  123.                     }
  124.                 }
  125.                 $templateEvent->setParameter('productHistory'$productHistory);
  126.             });
  127.         }
  128.     }
  129.     /**
  130.      * Cookie取得
  131.      *
  132.      * @param KernelEvent $event
  133.      * @return array
  134.      */
  135.     private function getCookie(KernelEvent $event): array
  136.     {
  137.         $cookie $event->getRequest()->cookies->get(self::COOKIE_NAME);
  138.         return json_decode($cookietrue) ?? [];
  139.     }
  140.     /**
  141.      * Cookie作成・更新
  142.      *
  143.      * @param ProductCollection $productCollection
  144.      * @return Cookie
  145.      * @throws \Exception
  146.      */
  147.     private function createCookie(ProductCollection $productCollection): Cookie
  148.     {
  149.         return new Cookie(
  150.             self::COOKIE_NAME,
  151.             json_encode($productCollection->toArray()),
  152.             (new \DateTime())->modify('1 month'),
  153.             $this->eccubeConfig['env(ECCUBE_COOKIE_PATH)']
  154.         );
  155.     }
  156. }