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

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