<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class MaintenanceListener
{
private $twig;
private $maintenance;
private $security;
public function __construct(Environment $twig, bool $maintenance, Security $security)
{
$this->twig = $twig;
$this->maintenance = $maintenance;
$this->security = $security;
}
public function onKernelRequest(RequestEvent $event)
{
// Permitir siempre el acceso a la ruta de inicio de sesión
if ($event->getRequest()->get('_route') === 'app_login') {
return;
}
if ($event->getRequest()->get('_route') === 'stripe_api_webhook_listener') {
return;
}
if ($this->maintenance) {
$user = $this->security->getUser();
// Comprobar si el usuario está autenticado y tiene el rol ROLE_ADM
if (!$user || !$this->security->isGranted('ROLE_ADM')) {
$response = new Response(
$this->twig->render('mantenimiento.html.twig'),
Response::HTTP_SERVICE_UNAVAILABLE
);
$event->setResponse($response);
}
}
}
}