src/EventSubscriber/ReportModeSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Security\ReportMode;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. final class ReportModeSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * {@inheritdoc}
  12.      */
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             KernelEvents::CONTROLLER => 'onController',
  17.             KernelEvents::RESPONSE => 'onResponse',
  18.         ];
  19.     }
  20.     /**
  21.      * @param ControllerEvent $event
  22.      */
  23.     public function onController(ControllerEvent $event): void
  24.     {
  25.         $route = (string) $event->getRequest()->attributes->get('_route');
  26.         if (str_starts_with($route'admin_reports') || str_starts_with($route'admin_api_report_')) {
  27.             ReportMode::enable();
  28.         }
  29.     }
  30.     /**
  31.      * @param ResponseEvent $event
  32.      */
  33.     public function onResponse(ResponseEvent $event): void
  34.     {
  35.         // ensure flag is reset at end of request
  36.         ReportMode::disable();
  37.     }
  38. }