vendor/damienharper/auditor-bundle/src/Event/ViewerEventSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace DH\AuditorBundle\Event;
  3. use DH\Auditor\Auditor;
  4. use DH\Auditor\Configuration as AuditorConfiguration;
  5. use DH\Auditor\Provider\Doctrine\Configuration as DoctrineProviderConfiguration;
  6. use DH\Auditor\Provider\Doctrine\DoctrineProvider;
  7. use DH\AuditorBundle\Controller\ViewerController;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\KernelEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class ViewerEventSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var Auditor
  16.      */
  17.     private $auditor;
  18.     public function __construct(Auditor $auditor)
  19.     {
  20.         $this->auditor $auditor;
  21.     }
  22.     public function onKernelController(KernelEvent $event): void
  23.     {
  24.         // Symfony 3.4+ compatibility (no ControllerEvent typehint)
  25.         if (!method_exists($event'getController')) {
  26.             throw new NotFoundHttpException();
  27.         }
  28.         $controller $event->getController();
  29.         // when a controller class defines multiple action methods, the controller
  30.         // is returned as [$controllerInstance, 'methodName']
  31.         if (\is_array($controller)) {
  32.             $controller $controller[0];
  33.         }
  34.         /** @var AuditorConfiguration $auditorConfiguration */
  35.         $auditorConfiguration $this->auditor->getConfiguration();
  36.         /** @var DoctrineProviderConfiguration $providerConfiguration */
  37.         $providerConfiguration $this->auditor->getProvider(DoctrineProvider::class)->getConfiguration();
  38.         $isAuditorEnabled $auditorConfiguration->isEnabled();
  39.         $isViewerEnabled $providerConfiguration->isViewerEnabled();
  40.         if ($controller instanceof ViewerController && (!$isAuditorEnabled || !$isViewerEnabled)) {
  41.             throw new NotFoundHttpException();
  42.         }
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             KernelEvents::CONTROLLER => 'onKernelController',
  48.         ];
  49.     }
  50. }