<?php
namespace App\EventListener\Restrictions;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
class OpeningControlListener
{
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMainRequest()) {
// don't do anything if it's not the master request
return;
}
$request = $event->getRequest();
$routeName = $request->attributes->get('_route');
if (in_array($routeName, ['maintenance_page', 'closing_page'])) {
return;
}
$allowedIp = [];
$clientIp = $request->getClientIp();
if (isset($_ENV['ALLOWED_IP']) && $_ENV['ALLOWED_IP']) {
$allowedIp = explode(',', $_ENV['ALLOWED_IP']);
}
if (false === (bool) $_ENV['RESTRICTION_ACCESS']) {
return;
}
if (in_array($clientIp, $allowedIp)) {
return;
}
if (true === (bool) $_ENV['CHECK_OPENING_DATE']) {
$now = new \DateTime();
if (new \DateTime($_ENV['OPENING_DATE']) > $now) {
$event->setResponse(new RedirectResponse('/maintenance'));
return;
}
if (new \DateTime($_ENV['ENDING_DATE']) < $now) {
$event->setResponse(new RedirectResponse('/fermeture'));
return;
}
}
$event->setResponse(new RedirectResponse('/maintenance'));
}
}