vendor/flagception/flagception-bundle/src/Listener/RoutingMetadataSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace Flagception\Bundle\FlagceptionBundle\Listener;
  3. use Flagception\Manager\FeatureManagerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. /**
  9. * Class RoutingMetadataSubscriber
  10. *
  11. * @author Michel Chowanski <michel.chowanski@bestit-online.de>
  12. * @package Flagception\Bundle\FlagceptionBundle\Listener
  13. */
  14. class RoutingMetadataSubscriber implements EventSubscriberInterface
  15. {
  16. /**
  17. * Feature key for routing annotation
  18. *
  19. * @var string
  20. */
  21. public const FEATURE_KEY = '_feature';
  22. /**
  23. * The feature manager
  24. *
  25. * @var FeatureManagerInterface
  26. */
  27. private $manager;
  28. /**
  29. * RoutingMetadataSubscriber constructor.
  30. *
  31. * @param FeatureManagerInterface $manager
  32. */
  33. public function __construct(FeatureManagerInterface $manager)
  34. {
  35. $this->manager = $manager;
  36. }
  37. /**
  38. * Filter by routing metadata
  39. *
  40. * @param ControllerEvent $event
  41. *
  42. * @return void
  43. * @throws NotFoundHttpException
  44. */
  45. public function onKernelController(ControllerEvent $event)
  46. {
  47. if (!$event->getRequest()->attributes->has(static::FEATURE_KEY)) {
  48. return;
  49. }
  50. $featureNames = (array) $event->getRequest()->attributes->get(static::FEATURE_KEY);
  51. foreach ($featureNames as $featureName) {
  52. if (!$this->manager->isActive($featureName)) {
  53. throw new NotFoundHttpException('Feature for this class is not active.');
  54. }
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public static function getSubscribedEvents(): array
  61. {
  62. return [
  63. KernelEvents::CONTROLLER => 'onKernelController',
  64. ];
  65. }
  66. }