app/Plugin/RefineCheckItem42/Event.php line 95

Open in your IDE?
  1. <?php
  2. namespace Plugin\RefineCheckItem42;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Eccube\Common\EccubeConfig;
  5. use Eccube\Request\Context;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Cookie;
  9. use Symfony\Component\HttpKernel\Event\KernelEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class Event implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * Cookie 名称
  15.      */
  16.     const COOKIE_NAME 'eccube_product_history';
  17.     /**
  18.      * Cookie 保存件数
  19.      */
  20.     const MAX_SAVE_NUM 10;
  21.     /**
  22.      * Cookie 保存日数
  23.      */
  24.     const MAX_SAVE_DAY 30;
  25.     /**
  26.      * @var EccubeConfig
  27.      */
  28.     private $eccubeConfig;
  29.     /**
  30.      * @var Context
  31.      */
  32.     private $context;
  33.     /**
  34.      * @param EccubeConfig $eccubeConfig
  35.      * @param Context $context
  36.      */
  37.     public function __construct(
  38.         EccubeConfig $eccubeConfig,
  39.         Context $context
  40.     ) {
  41.         $this->eccubeConfig $eccubeConfig;
  42.         $this->context $context;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             KernelEvents::RESPONSE => 'onKernelResponse',
  51.         ];
  52.     }
  53.     /**
  54.      * 商品詳細ページアクセス時に商品IDをCookieに保存する
  55.      *
  56.      * @param ResponseEvent $event
  57.      * @throws \Exception
  58.      */
  59.     public function onKernelResponse(ResponseEvent $event)
  60.     {
  61.         if (!$event->isMasterRequest() || $this->context->isAdmin()) {
  62.             return;
  63.         }
  64.         if ($event->getRequest()->get('_route') !== 'product_detail') {
  65.             return;
  66.         }
  67.         $productId $event->getRequest()->get('id');
  68.         if (!is_null($productId)) {
  69.             $this->setCookie($productId$event);
  70.         }
  71.     }
  72.     /**
  73.      * Cookie の取得
  74.      *
  75.      * @param KernelEvent $event
  76.      * @return array|mixed
  77.      */
  78.     private function getProductIdsFromCookie(KernelEvent $event)
  79.     {
  80.         $cookie $event->getRequest()->cookies->get(self::COOKIE_NAME);
  81.         return json_decode($cookietrue) ?? [];
  82.     }
  83.     /**
  84.      * Cookieに商品IDを追加
  85.      *
  86.      * @param $productId
  87.      * @param ResponseEvent $event
  88.      */
  89.     private function setCookie($productIdResponseEvent $event)
  90.     {
  91.         $productIds = (new ArrayCollection($this->getProductIdsFromCookie($event)))->toArray();
  92.         $key array_search($productId$productIds);
  93.         if (false !== $key) {
  94.             array_splice($productIds$key1);
  95.         }
  96.         $productIds[] = $productId;
  97.         if (self::MAX_SAVE_NUM count($productIds)) {
  98.             array_splice($productIds0count($productIds) - self::MAX_SAVE_NUM);
  99.         }
  100.         $cookie = new Cookie(
  101.             self::COOKIE_NAME,
  102.             json_encode($productIds),
  103.             (new \DateTime())->modify(self::MAX_SAVE_DAY ' day'),
  104.             $this->eccubeConfig['env(ECCUBE_COOKIE_PATH)']
  105.         );
  106.         $response $event->getResponse();
  107.         $response->headers->setCookie($cookie);
  108.         $event->setResponse($response);
  109.     }
  110. }