src/EventSubscriber/KernelResponseListener.php line 42
<?php
namespace App\EventSubscriber;
use App\Controller\ReturnImpostazioniServiceController;
use App\Controller\ServiziController;
use Monolog\Logger;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Bundle\SecurityBundle\Security;
/**
* Class KernelRequestListener
* @package App\EventSubscriber
*/
class KernelResponseListener implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
private ServiziController $servizi;
private ReturnImpostazioniServiceController $imp;
private $tokenStorage;
private $logger;
private Security $security;
public function __construct(ServiziController $servizi, TokenStorageInterface $tokenStorage, Logger $logger, ReturnImpostazioniServiceController $imp, Security $security)
{
$this->servizi = $servizi;
$this->security = $security;
$this->logger = $logger;
$this->tokenStorage = $tokenStorage;
$this->imp = $imp;
}
public function onKernelResponse(ResponseEvent $event)
{
$cookie = new Cookie('secret_user', $this->servizi->randomString(), (new \DateTime('now'))->modify("+30 day"));
$cookieTrovato = $event->getRequest()->cookies->get('secret_user');
if(!$cookieTrovato)
$event->getResponse()->headers->setCookie($cookie);
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* The code must not depend on runtime state as it will only be called at compile time.
* All logic depending on runtime state must be put into the individual methods handling the events.
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::RESPONSE => array(array('onKernelResponse', 17)),
);
}
}