<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CookieSubscriber implements EventSubscriberInterface
{
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
public function onRequestEvent(RequestEvent $event)
{
$cookieVisitorTagName = 'visitor_tag';
if (!isset($_COOKIE[$cookieVisitorTagName])) {
$visitorTag = uniqid();
setcookie($cookieVisitorTagName, $visitorTag);
$_COOKIE[$cookieVisitorTagName] = $visitorTag;
}
}
public static function getSubscribedEvents()
{
return [
RequestEvent::class => 'onRequestEvent',
];
}
}