src/Listener/WmApiListener.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Services\DTV\YamlConfig\YamlReader;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\Routing\RouterInterface;
  7. class WmApiListener
  8. {
  9. private RouterInterface $router;
  10. private YamlReader $yamlReader;
  11. public function __construct(
  12. RouterInterface $router,
  13. YamlReader $yamlReader
  14. )
  15. {
  16. $this->router = $router;
  17. $this->yamlReader = $yamlReader;
  18. }
  19. /**
  20. * @param RequestEvent $event
  21. *
  22. * @return void
  23. */
  24. public function onKernelRequest( RequestEvent $event ): void
  25. {
  26. $currentRoute = $event->getRequest()->get( '_route' );
  27. if ( !in_array(
  28. $currentRoute,
  29. [
  30. 'front_common_css_custom',
  31. 'front_common_document',
  32. 'front_common_document_html',
  33. 'front_user_accept_cgu_first',
  34. 'front_user_welcome_register',
  35. 'front_user_accept_cgu_only',
  36. ],
  37. TRUE )
  38. ) {
  39. if ( $this->yamlReader->getType() === ( 'api' ) ) {
  40. if ( preg_match( '/^front_(.*)/', $currentRoute ) ) {
  41. $url = $this->router->generate( 'back_dashboard' );
  42. $response = new RedirectResponse( $url );
  43. $event->setResponse( $response );
  44. }
  45. }
  46. }
  47. }
  48. }