src/EventListener/RequestListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Constants\CompteClientConstants;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  8. use Symfony\Component\Security\Core\Security;
  9. class RequestListener
  10. {
  11.     private $security// needed to get the user
  12.     private $router// needed to generate the logout-url
  13.     public function __construct(Security $securityUrlGeneratorInterface $router
  14.     {
  15.         $this->security $security;
  16.         $this->router $router;
  17.     }
  18.     public function __invoke(RequestEvent $event) : void
  19.     {
  20.         $user $this->security->getUser();
  21.         if ($user === null) {
  22.             return;
  23.         }
  24.         if ($user->getEtat() === CompteClientConstants::ETAT_FERME) {
  25.             $url $this->router->generate('app_logout');
  26.             $event->setResponse(new RedirectResponse($url));
  27.         }
  28.     }
  29. }