src/EventSubscriber/EnabledCrmSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\ActionRequiresCrmEnabledInterface;
  4. use App\Service\Channel\ChannelConfigurationService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  9. class EnabledCrmSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private ChannelConfigurationService $channelConfigurationService,
  13.     )
  14.     {
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             KernelEvents::CONTROLLER => ['__invoke', -10], // keep low priority to be called after ParamConverter
  20.         ];
  21.     }
  22.     public function __invoke(ControllerEvent $event): void
  23.     {
  24.         if (!$event->isMainRequest()) {
  25.             return;
  26.         }
  27.         $controller $event->getController();
  28.         if (!$controller instanceof ActionRequiresCrmEnabledInterface) {
  29.             return;
  30.         }
  31.         $request $event->getRequest();
  32.         $channel $request->attributes->get('channel');
  33.         if (!$channel) {
  34.             return;
  35.         }
  36.         if (!$this->channelConfigurationService->isEnabledCrm($channel)) {
  37.             throw new AccessDeniedException('CRM is disabled for this channel.');
  38.         }
  39.     }
  40. }