<?php
namespace App\EventSubscriber;
use App\Controller\ActionRequiresCrmEnabledInterface;
use App\Service\Channel\ChannelConfigurationService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class EnabledCrmSubscriber implements EventSubscriberInterface
{
public function __construct(
private ChannelConfigurationService $channelConfigurationService,
)
{
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => ['__invoke', -10], // keep low priority to be called after ParamConverter
];
}
public function __invoke(ControllerEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$controller = $event->getController();
if (!$controller instanceof ActionRequiresCrmEnabledInterface) {
return;
}
$request = $event->getRequest();
$channel = $request->attributes->get('channel');
if (!$channel) {
return;
}
if (!$this->channelConfigurationService->isEnabledCrm($channel)) {
throw new AccessDeniedException('CRM is disabled for this channel.');
}
}
}