app/Plugin/CheckProduct42/EventSubscriber/CheckProductEvent.php line 79

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright(c) 2019 SYSTEM FRIEND INC.
  4.  */
  5. namespace Plugin\CheckProduct42\EventSubscriber;
  6. use Eccube\Event\EccubeEvents;
  7. use Eccube\Event\EventArgs;
  8. use Plugin\CheckProduct42\Service\CookieHelper;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Cookie;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class CheckProductEvent implements EventSubscriberInterface
  14. {
  15.     protected \Plugin\CheckProduct42\Common\EccubeConfigEx $checkProductConfig;
  16.     protected CookieHelper $cookieHelper;
  17.     protected $cookie;
  18.     /**
  19.      * CheckProductEvent constructor.
  20.      *
  21.      * @param \Plugin\CheckProduct42\Common\EccubeConfigEx $checkProductConfig
  22.      * @param CookieHelper $cookieHelper
  23.      */
  24.     public function __construct(
  25.         \Plugin\CheckProduct42\Common\EccubeConfigEx $checkProductConfig,
  26.         CookieHelper $cookieHelper
  27.     )
  28.     {
  29.         $this->checkProductConfig $checkProductConfig;
  30.         $this->cookieHelper $cookieHelper;
  31.     }
  32.     /**
  33.      * @return array
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::RESPONSE => 'onKernelResponse',
  39.             EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE => 'createCookie',
  40.         ];
  41.     }
  42.     /**
  43.      * クッキーに商品IDを保存
  44.      *
  45.      * @param ResponseEvent $event
  46.      * @throws \Exception
  47.      */
  48.     public function onKernelResponse(ResponseEvent $event)
  49.     {
  50.         $request $event->getRequest();
  51.         if ($request->get('_route') !== 'product_detail') {
  52.             return;
  53.         }
  54.         if($this->cookie){
  55.             $response $this->cookieHelper->setCookie($event->getResponse(), $this->cookie);
  56.             // Cookieをレスポンスにセット
  57.             $event->setResponse($response);
  58.         }
  59.     }
  60.     /**
  61.      * クッキーを作成
  62.      *
  63.      * @param EventArgs $event
  64.      * @throws \Exception
  65.      */
  66.     public function createCookie(EventArgs $event)
  67.     {
  68.         $Product $event->getArgument('Product');
  69.         $arr $this->cookieHelper->getProductIds();
  70.         array_unshift($arr$Product->getId());
  71.         $arr array_unique($arr);
  72.         $max $this->checkProductConfig->number_of_items();
  73.         $arr array_slice($arr0$max);
  74.         // 保持期間1ヶ月
  75.         $expire = new \DateTime();
  76.         $month 1;
  77.         $expire->modify("$month month");
  78.         // Cookie作成
  79.         $cookie = new Cookie(
  80.             $this->cookieHelper->getCookieName(),
  81.             implode(','$arr),
  82.             $expire,
  83.             $this->checkProductConfig->get('env(ECCUBE_COOKIE_PATH)')
  84.         );
  85.         $this->cookie $cookie;
  86.     }
  87. }