<?php
namespace App\EventListener;
use App\Repository\CommandeRepository;
use App\Service\CommandeService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Twig\Environment;
class KernelRequestListener
{
protected $token;
protected $router;
protected $twig;
protected $commandeService;
public function __construct(RouterInterface $router, TokenStorageInterface $token_storage, EntityManagerInterface $em, SessionInterface $session, Environment $twig, CommandeService $commandeService)
{
$this->token = $token_storage;
$this->router = $router;
$this->twig = $twig;
$this->commandeService = $commandeService;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($event->isMasterRequest() && $this->token->getToken() && $this->token->getToken()->getUser() != 'anon.') {
if ($this->token->getToken()->getUser()->isTemporaryPassword() && $request->attributes->get('_route') != 'change_password') {
$event->setResponse(new RedirectResponse($this->router->generate('change_password')));
$this->twig->addGlobal('panierTotal', 0);
} else {
$panier = $this->commandeService->getPanier($this->token->getToken()->getUser());
$this->twig->addGlobal('panierTotal', (($panier) ? count($panier) : 0 ));
}
}
return;
}
}