<?php
namespace App\EventListener;
use App\Constants\CompteClientConstants;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Security;
class RequestListener
{
private $security; // needed to get the user
private $router; // needed to generate the logout-url
public function __construct(Security $security, UrlGeneratorInterface $router)
{
$this->security = $security;
$this->router = $router;
}
public function __invoke(RequestEvent $event) : void
{
$user = $this->security->getUser();
if ($user === null) {
return;
}
if ($user->getEtat() === CompteClientConstants::ETAT_FERME) {
$url = $this->router->generate('app_logout');
$event->setResponse(new RedirectResponse($url));
}
}
}