src/EventSubscriber/KernelControllerSubscriber.php line 70

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\SessionParticipationService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\Routing\RouterInterface;
  10. class KernelControllerSubscriber implements EventSubscriberInterface
  11. {
  12.     public const ARRAY_PARTICIPATION_SESSION = [
  13.         'odr_registration',
  14.         'odr_recap',
  15.         'odr_recap_modify',
  16.         '_wdt',
  17.         'ig_recap',
  18.         'ig_registration',
  19.         'ig_recap_modify',
  20.         'ig_participation_winner',
  21.         'ig_participation_lost',
  22.     ];
  23.     //closed website
  24.     public const OPENDED_ROUTES = [
  25.         "app_terms_use",
  26.         "app_contact",
  27.         'app_legal_notices',
  28.         'app_cookie',
  29.         'app_rules'
  30.     ];
  31.     private const ADMIN_ROUTENAMES = [
  32.         'admin',
  33.         'app_login'
  34.     ];
  35.     private ParameterBagInterface $parameterBag;
  36.     private \DateTime $today;
  37.     private RouterInterface $router;
  38.     private SessionParticipationService $sessionParticipationService;
  39.     public function __construct(
  40.         ParameterBagInterface $parameterBag,
  41.         RouterInterface $router,
  42.         SessionParticipationService $sessionParticipationService
  43.     )
  44.     {
  45.         $this->parameterBag $parameterBag;
  46.         $this->today = new \DateTime();
  47.         $this->router $router;
  48.         $this->sessionParticipationService $sessionParticipationService;
  49.     }
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             KernelEvents::CONTROLLER => 'onKernelController',
  54.         ];
  55.     }
  56.     public function onKernelController(ControllerEvent $event): void
  57.     {
  58.         $routeName $event->getRequest()->get("_route");
  59.         if (str_contains($routeName'odr') || str_contains($routeName'asso')) {
  60.             $this->redirectToRoute($event"app_home");
  61.             return;
  62.         }
  63.         if ('nutella_prod' === $_ENV['APP_ENV'] && is_null($routeName)) {
  64.             $this->redirectToRoute($event'home_404');
  65.         }
  66.         if ($event->isMainRequest() && !in_array($routeNameself::ARRAY_PARTICIPATION_SESSION)) {
  67.             $this->sessionParticipationService->toggleParticipationInSession(false);
  68.         }
  69.         $operationOptions $this->parameterBag->get("operation_options");
  70.         $operationDates $operationOptions["operation_dates"];
  71.         if (!$event->isMainRequest() || $operationDates["check_opening_date"] === "false") {
  72.             return;
  73.         }
  74.         if ($operationOptions["whitelist_ip"]["check_whitelist"] === "true") {
  75.             if (in_array(
  76.                 $operationOptions["whitelist_ip"]["ip"],
  77.                 $event->getRequest()->getClientIps())) {
  78.                 return;
  79.             }
  80.         }
  81.         if ($operationOptions["maintenance_mode"] === "true") {
  82.             if ($routeName !== "home_maintenance") {
  83.                 $this->redirectToRoute($event"home_maintenance");
  84.             }
  85.         }
  86.         $dateStart = new \DateTime($operationDates["date_start"]);
  87.         $dateEnd = new \DateTime($operationDates["date_end"]);
  88.         if (!in_array($routeNameself::ADMIN_ROUTENAMES)) {
  89.             if ($this->today $dateStart) {
  90.                 if ($routeName !== "app_opening") {
  91.                     $this->redirectToRoute($event"app_opening");
  92.                 }
  93.             } elseif ($this->today >= $dateEnd) {//close
  94.                 if (!in_array($routeNameself::OPENDED_ROUTES) && !str_contains($routeName'followup')) {
  95.                     if ($routeName !== "app_closing") {
  96.                         $this->redirectToRoute($event"app_closing");
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     private function redirectToRoute(ControllerEvent $eventstring $routeName): void
  103.     {
  104.         $route $this->router->generate($routeName);
  105.         $event->setController(function() use ($route) {
  106.             return new RedirectResponse($route);
  107.         });
  108.     }
  109. }