app/Plugin/CheckProduct42/Service/CookieHelper.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright(c) 2019 SYSTEM FRIEND INC.
  4.  */
  5. namespace Plugin\CheckProduct42\Service;
  6. use Symfony\Component\HttpFoundation\Cookie;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class CookieHelper
  11. {
  12.     /**
  13.      * @var string
  14.      */
  15.     private $cookie_name 'plugin__check_product42__product';
  16.     /**
  17.      * @var Request
  18.      */
  19.     private $request;
  20.     public function __construct(
  21.         RequestStack $requestStack
  22.     )
  23.     {
  24.         $this->request $requestStack->getCurrentRequest();
  25.     }
  26.     /**
  27.      * Cookieを取得
  28.      *
  29.      * @return \Symfony\Component\HttpFoundation\ParameterBag|null
  30.      */
  31.     public function getCookie()
  32.     {
  33.         if (!$this->request) {
  34.             return null;
  35.         }
  36.         return $this->request->cookies;
  37.     }
  38.     /**
  39.      * Cookieを取得
  40.      *
  41.      * @return array
  42.      */
  43.     public function getProductIds(): array
  44.     {
  45.         $Cookie $this->getCookie();
  46.         if (empty($Cookie)) {
  47.             return [];
  48.         }
  49.         $stringProductIds explode(','$Cookie->get($this->getCookieName()));
  50.         $product_ids = [];
  51.         foreach ($stringProductIds as $id) {
  52.             if (strlen($id) > 0) {
  53.                 $product_ids[] = intval($id);
  54.             }
  55.         }
  56.         return $product_ids;
  57.     }
  58.     /**
  59.      * Cookieをセット
  60.      *
  61.      * @param Response $response
  62.      * @param Cookie $cookie
  63.      * @return Response
  64.      */
  65.     public function setCookie(Response $responseCookie $cookie): Response
  66.     {
  67.         $response->headers->setCookie($cookie);
  68.         return $response;
  69.     }
  70.     /**
  71.      * Cookie削除
  72.      *
  73.      * @param Response $response
  74.      * @param Cookie $cookie
  75.      * @return Response
  76.      */
  77.     public function remove(Response $responseCookie $cookie): Response
  78.     {
  79.         $response->headers->removeCookie($cookie->getName(), $cookie->getPath(), $cookie->getDomain());
  80.         return $response;
  81.     }
  82.     /**
  83.      * Cookie名をセット
  84.      *
  85.      * @param string $cookie_name
  86.      * @return $this
  87.      */
  88.     public function setCookieName(string $cookie_name): self
  89.     {
  90.         $this->cookie_name $cookie_name;
  91.         return $this;
  92.     }
  93.     /**
  94.      * Cookie名を取得
  95.      *
  96.      * @return string
  97.      */
  98.     public function getCookieName(): string
  99.     {
  100.         return $this->cookie_name;
  101.     }
  102. }