src/Aqarmap/Bundle/ListingBundle/Service/CallLog/Drivers/Cookies.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Moonshot Project 2019.
  4.  *
  5.  * @author Omar Makled <omar.makled@aqarmap.com>
  6.  */
  7. namespace Aqarmap\Bundle\ListingBundle\Service\CallLog\Drivers;
  8. use Aqarmap\Bundle\ListingBundle\Service\CallLog\LoggerInterface;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class Cookies implements LoggerInterface
  13. {
  14.     /**
  15.      * Log key name.
  16.      */
  17.     public const CALLLOG 'calllog';
  18.     /**
  19.      * Cookie instance.
  20.      *
  21.      * @var Cookie
  22.      */
  23.     protected $cookie;
  24.     /**
  25.      * @param Request $request
  26.      */
  27.     public function __construct(RequestStack $request)
  28.     {
  29.         $this->cookie $request->getCurrentRequest()->cookies;
  30.     }
  31.     public function add($listingId): void
  32.     {
  33.         $cookie $this->hasLog() ? $this->getLog() : [];
  34.         $cookie[] = $listingId;
  35.         $this->set(json_encode($cookie));
  36.     }
  37.     public function get()
  38.     {
  39.         return $this->hasLog() ? $this->getLog() : [];
  40.     }
  41.     public function remove(): void
  42.     {
  43.         $this->set();
  44.     }
  45.     /**
  46.      * Check if user has call log.
  47.      *
  48.      * @return bool
  49.      */
  50.     private function hasLog()
  51.     {
  52.         return $this->cookie->has(self::CALLLOG);
  53.     }
  54.     /**
  55.      * Get call log.
  56.      *
  57.      * @return []
  58.      */
  59.     private function getLog()
  60.     {
  61.         return json_decode($this->cookie->get(self::CALLLOG), true);
  62.     }
  63.     /**
  64.      * Set to cookies.
  65.      *
  66.      * @param string||null $cookie
  67.      */
  68.     private function set($cookie ''): void
  69.     {
  70.         $response = new Response();
  71.         $response->headers->setCookie(new Cookie(self::CALLLOG$cookie));
  72.         $response->send();
  73.     }
  74. }