<?php
namespace App\Listeners;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Twig\Environment;
class MaintenanceListener
{
private $container;
private $engine;
private $maintenance;
private $ipAuthorized;
public function __construct($maintenance, ContainerInterface $container, Environment $engine)
{
$this->container = $container;
$this->engine = $engine;
$this->maintenance = $maintenance["statut"];
$this->ipAuthorized = $maintenance["ipAuthorized"];
}
public function onKernelRequest(RequestEvent $event)
{
$debug = false;
$maintenance = $this->maintenance === 'true' ? $this->maintenance : false;
$currentIP = $_SERVER['REMOTE_ADDR'];
// If maintenance is active and in prod environment
if (!$debug && $maintenance && !in_array($currentIP, $this->ipAuthorized)) {
$template = $this->engine->render('maintenance/maintenance.html.twig');
$event->setResponse(new Response($template, 503));
$event->stopPropagation();
}
}
}