<?php
/*
* This file is part of Moonshot Project 2019.
*
* @author Omar Makled <omar.makled@aqarmap.com>
*/
namespace Aqarmap\Bundle\ListingBundle\Service\CallLog\Drivers;
use Aqarmap\Bundle\ListingBundle\Service\CallLog\LoggerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
class Cookies implements LoggerInterface
{
/**
* Log key name.
*/
public const CALLLOG = 'calllog';
/**
* Cookie instance.
*
* @var Cookie
*/
protected $cookie;
/**
* @param Request $request
*/
public function __construct(RequestStack $request)
{
$this->cookie = $request->getCurrentRequest()->cookies;
}
public function add($listingId): void
{
$cookie = $this->hasLog() ? $this->getLog() : [];
$cookie[] = $listingId;
$this->set(json_encode($cookie));
}
public function get()
{
return $this->hasLog() ? $this->getLog() : [];
}
public function remove(): void
{
$this->set();
}
/**
* Check if user has call log.
*
* @return bool
*/
private function hasLog()
{
return $this->cookie->has(self::CALLLOG);
}
/**
* Get call log.
*
* @return []
*/
private function getLog()
{
return json_decode($this->cookie->get(self::CALLLOG), true);
}
/**
* Set to cookies.
*
* @param string||null $cookie
*/
private function set($cookie = ''): void
{
$response = new Response();
$response->headers->setCookie(new Cookie(self::CALLLOG, $cookie));
$response->send();
}
}