app/Customize/EventSubscriber/ProductHistoryEventSubscriber.php line 131

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