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

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.      * 形式: "商品ID:タイムスタンプ,商品ID:タイムスタンプ,..."
  63.      *
  64.      * @param EventArgs $event
  65.      * @throws \Exception
  66.      */
  67.     public function createCookie(EventArgs $event)
  68.     {
  69.         $Product $event->getArgument('Product');
  70.         $productId $Product->getId();
  71.         $now time();
  72.         // 既存のデータを取得(商品ID => タイムスタンプ)
  73.         $productData $this->cookieHelper->getProductData();
  74.         // 現在の商品のタイムスタンプを更新(または新規追加)
  75.         $productData[$productId] = $now;
  76.         // タイムスタンプの降順(新しい順)でソート
  77.         arsort($productData);
  78.         
  79.         // Cookie保存数は100件に制限(パレス/プラザどちらにログインするか分からないため多めに保存)
  80.         $productData array_slice($productData0100true);
  81.         // "商品ID:タイムスタンプ" 形式の配列を作成
  82.         $cookieItems = [];
  83.         foreach ($productData as $id => $timestamp) {
  84.             $cookieItems[] = $id ':' $timestamp;
  85.         }
  86.         // 保持期間1ヶ月
  87.         $expire = new \DateTime();
  88.         $month 1;
  89.         $expire->modify("$month month");
  90.         // Cookie作成
  91.         $cookie = new Cookie(
  92.             $this->cookieHelper->getCookieName(),
  93.             implode(','$cookieItems),
  94.             $expire,
  95.             $this->checkProductConfig->get('env(ECCUBE_COOKIE_PATH)')
  96.         );
  97.         $this->cookie $cookie;
  98.     }
  99. }