src/EventSubscriber/Club1895/RegistrationControllerSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Club1895;
  3. use App\Controller\Adherent\Club1895\AbstractClub1895Controller;
  4. use App\Controller\Adherent\Club1895\PaymentController;
  5. use App\Controller\Adherent\Club1895\RegistrationController;
  6. use App\Entity\Enum\RegistrationStatus;
  7. use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. /**
  12.  * Vérifie que l'utilisateur n'essaie pas d'accéder à une étape d'inscription indisponible
  13.  */
  14. class RegistrationControllerSubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
  17.     {
  18.     }
  19.     public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
  20.     {
  21.         $callable $event->getController();
  22.         if (is_array($callable) && null !== $callable[0] ?? null) {
  23.             $controller $callable[0];
  24.             if ($controller instanceof AbstractClub1895Controller) {
  25.                 $registration $controller->getRegistration();
  26.                 if ($controller instanceof RegistrationController &&
  27.                     $registration?->getStatus() === RegistrationStatus::Registered) {
  28.                     $event->setController(function () use ($event) {
  29.                         return (new RedirectController($this->urlGenerator))
  30.                             ->redirectAction($event->getRequest(), 'dashboard');
  31.                     });
  32.                     return;
  33.                 }
  34.                 if (null === $registration && $controller instanceof PaymentController) {
  35.                     $event->setController(function () use ($event) {
  36.                         return (new RedirectController($this->urlGenerator))
  37.                             ->redirectAction($event->getRequest(), 'club_1895');
  38.                     });
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             'kernel.controller_arguments' => 'onKernelControllerArguments',
  47.         ];
  48.     }
  49. }