<?php
namespace App\Listener;
use App\Services\DTV\YamlConfig\YamlReader;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\RouterInterface;
class WmApiListener
{
private RouterInterface $router;
private YamlReader $yamlReader;
public function __construct(
RouterInterface $router,
YamlReader $yamlReader
)
{
$this->router = $router;
$this->yamlReader = $yamlReader;
}
/**
* @param RequestEvent $event
*
* @return void
*/
public function onKernelRequest( RequestEvent $event ): void
{
$currentRoute = $event->getRequest()->get( '_route' );
if ( !in_array(
$currentRoute,
[
'front_common_css_custom',
'front_common_document',
'front_common_document_html',
'front_user_accept_cgu_first',
'front_user_welcome_register',
'front_user_accept_cgu_only',
],
TRUE )
) {
if ( $this->yamlReader->getType() === ( 'api' ) ) {
if ( preg_match( '/^front_(.*)/', $currentRoute ) ) {
$url = $this->router->generate( 'back_dashboard' );
$response = new RedirectResponse( $url );
$event->setResponse( $response );
}
}
}
}
}