<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Templating\EngineInterface;
/**
* Class MaintenanceListener
*
* @package App\EventListener
*/
class MaintenanceListener
{
/**
* @var string
*/
public const ENV_DEV = 'dev';
/**
* @var int
*/
private $maintenance;
/**
* @var string
*/
private $env;
/**
* @var EngineInterface
*/
private $templating;
/**
* MaintenanceListener constructor.
*
* @param EngineInterface $templating
*/
public function __construct(EngineInterface $templating)
{
$this->maintenance = getenv('MAINTENANCE');
$this->env = getenv('APP_ENV');
$this->templating = $templating;
}
/**
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event)
{
$debug = $this->env === self::ENV_DEV;
if ($this->maintenance && !$debug) {
$template = $this->templating->render('maintenance.html.twig');
$event->setResponse(new Response($template, Response::HTTP_SERVICE_UNAVAILABLE));
}
}
}