<?php
namespace App\Listener;
use App\Entity\Configuracion;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface as Doctrine;
use Twig\Environment;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class MaintenanceListener
{
private $appEnv;
private $em;
private $twig;
public function __construct(Doctrine $em,Environment $twig, $app_env)
{
$this->em = $em;
$this->appEnv = $app_env;
$this->twig = $twig;
}
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMainRequest()) {
// don't do anything if it's not the main request
return;
}
$maintenance = $this->em -> getRepository(Configuracion::class) -> findOneBy(array('clave' => 'mantenimiento'));
if($maintenance){
$underMaintenance = $maintenance->getValor();
$maintenanceUntil = $maintenance->getParametros();
$debug = in_array($this->appEnv, array('test', 'dev'));
if ($underMaintenance =='1' && !$debug) {
//if ($underMaintenance =='1') {
$engine = $this->twig;
$content = $engine->render('theme/maintenance.html.twig', array('maintenanceUntil'=>$maintenanceUntil));
$event->setResponse(new Response($content, 503));
$event->stopPropagation();
}
}
}
}