src/EventSubscriber/KernelControllerSubscriber.php line 66
<?php
namespace App\EventSubscriber;
use App\Service\SessionParticipationService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\RouterInterface;
class KernelControllerSubscriber implements EventSubscriberInterface
{
public const ARRAY_PARTICIPATION_SESSION = [
'odr_registration',
'odr_recap',
'odr_recap_modify',
'_wdt',
'ig_recap',
'ig_registration',
'ig_recap_modify',
'ig_participation_winner',
'ig_participation_lost',
];
//closed website
public const OPENDED_ROUTES = [
"app_terms_use",
"app_contact",
'app_legal_notices',
'app_cookie',
'app_rules'
];
private const ADMIN_ROUTENAMES = [
'admin',
'app_login'
];
private ParameterBagInterface $parameterBag;
private \DateTime $today;
private RouterInterface $router;
private SessionParticipationService $sessionParticipationService;
public function __construct(
ParameterBagInterface $parameterBag,
RouterInterface $router,
SessionParticipationService $sessionParticipationService
)
{
$this->parameterBag = $parameterBag;
$this->today = new \DateTime();
$this->router = $router;
$this->sessionParticipationService = $sessionParticipationService;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
public function onKernelController(ControllerEvent $event): void
{
$routeName = $event->getRequest()->get("_route");
if (str_contains($routeName, 'odr') || str_contains($routeName, 'asso')) {
$this->redirectToRoute($event, "app_home");
return;
}
if ('nutella_prod' === $_ENV['APP_ENV'] && is_null($routeName)) {
$this->redirectToRoute($event, 'home_404');
}
if ($event->isMainRequest() && !in_array($routeName, self::ARRAY_PARTICIPATION_SESSION)) {
$this->sessionParticipationService->toggleParticipationInSession(false);
}
$operationOptions = $this->parameterBag->get("operation_options");
$operationDates = $operationOptions["operation_dates"];
if (!$event->isMainRequest() || $operationDates["check_opening_date"] === "false") {
return;
}
if ($operationOptions["whitelist_ip"]["check_whitelist"] === "true") {
if (in_array(
$operationOptions["whitelist_ip"]["ip"],
$event->getRequest()->getClientIps())) {
return;
}
}
if ($operationOptions["maintenance_mode"] === "true") {
if ($routeName !== "home_maintenance") {
$this->redirectToRoute($event, "home_maintenance");
}
}
$dateStart = new \DateTime($operationDates["date_start"]);
$dateEnd = new \DateTime($operationDates["date_end"]);
if (!in_array($routeName, self::ADMIN_ROUTENAMES)) {
if ($this->today < $dateStart) {
if ($routeName !== "app_opening") {
$this->redirectToRoute($event, "app_opening");
}
} elseif ($this->today >= $dateEnd) {//close
if (!in_array($routeName, self::OPENDED_ROUTES) && !str_contains($routeName, 'followup')) {
if ($routeName !== "app_closing") {
$this->redirectToRoute($event, "app_closing");
}
}
}
}
}
private function redirectToRoute(ControllerEvent $event, string $routeName): void
{
$route = $this->router->generate($routeName);
$event->setController(function() use ($route) {
return new RedirectResponse($route);
});
}
}