src/EventSubscriber/LogoutSubscriber.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Throwable;
  4. use App\Adapter\AuthAdapter;
  5. use App\Model\Appointment;
  6. use App\Service\AppointmentService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Http\Event\LogoutEvent;
  12. class LogoutSubscriber implements EventSubscriberInterface
  13. {
  14.     private UrlGeneratorInterface $urlGenerator;
  15.     private AuthAdapter $authAdapter;
  16.     private Security $security;
  17.     private AppointmentService $appointmentService;
  18.     private ?Appointment $appointment null;
  19.     private ?bool $gon null;
  20.     /**
  21.      * @var mixed
  22.      */
  23.     private ?array $vehicles = [];
  24.     public function __construct(UrlGeneratorInterface $urlGeneratorAuthAdapter $authAdapterSecurity $securityAppointmentService $appointmentService)
  25.     {
  26.         $this->urlGenerator $urlGenerator;
  27.         $this->authAdapter $authAdapter;
  28.         $this->security $security;
  29.         $this->appointmentService $appointmentService;
  30.     }
  31.     public function onLogoutEvent(LogoutEvent $event): void
  32.     {
  33.         // get the security token of the session that is about to be logged out
  34.         $token $event->getToken();
  35.         $logoutOK true;
  36.         if ($this->security->isGranted('ROLE_CLIENT')) {
  37.             try {
  38.                 $client $token->getUser();
  39.                 $this->authAdapter->logout($client->getToken());
  40.             } catch (Throwable $exception) {
  41.                 $logoutOK false;
  42.             }
  43.         }
  44.         if ($logoutOK) {
  45.             $request $event->getRequest();
  46.             $session $request->getSession();
  47.             if ($this->appointment && !$this->gon) {
  48.                 $this->appointmentService->removeNextStep($this->appointment'garage');
  49.                 $session->set('logout'true);
  50.             }
  51.             $session->set('appointment'$this->appointment);
  52.             $session->set('vehicles'$this->vehicles);
  53.             $session->set('gon'$this->gon);
  54.             // configure a custom logout response to the homepage
  55.             $response = new RedirectResponse(
  56.                 $this->urlGenerator->generate('front_index_page'),
  57.                 RedirectResponse::HTTP_SEE_OTHER
  58.             );
  59.             $event->setResponse($response);
  60.         }
  61.     }
  62.     public function onPreLogoutEvent(LogoutEvent $event)
  63.     {
  64.         $request $event->getRequest();
  65.         $session $request->getSession();
  66.         $this->appointment $session->get('appointment');
  67.         $this->gon $session->get('gon'false);
  68.         $this->vehicles $session->get('vehicles', []);
  69.     }
  70.     public static function getSubscribedEvents(): array
  71.     {
  72.         return [
  73.             LogoutEvent::class => [
  74.                 ['onLogoutEvent'],
  75.                 ['onPreLogoutEvent'99],
  76.             ],
  77.         ];
  78.     }
  79. }