<?php
namespace App\EventListener\Web;
use App\Application\Service\Helper\ConfigurationService;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Twig\Environment;
class MaintenanceModeListener
{
/** @var Environment */
private $twig;
/** @var bool */
private $maintenanceMode;
/** @var ConfigurationService */
private $configurationService;
public function __construct(bool $maintenanceMode, Environment $twig, ConfigurationService $configurationService)
{
$this->maintenanceMode = $maintenanceMode;
$this->twig = $twig;
$this->configurationService = $configurationService;
}
public function onKernelRequest(RequestEvent $requestEvent): void
{
if ($this->maintenanceMode) {
$allowedIps = explode(';', $this->configurationService->getCachedValue('MAINTENANCE_IPS'));
$requestIp = $requestEvent->getRequest()->getClientIp();
if (!in_array($requestIp, $allowedIps, true)) {
$renderedTemplate = $this->twig->render('front/maintenance/maintenance.html.twig');
$response = new Response($renderedTemplate);
$requestEvent->setResponse($response);
}
}
}
}