vendor/kunstmaan/node-bundle/EventListener/SlugListener.php line 75

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\NodeBundle\EventListener;
  3. use Doctrine\ORM\EntityManager;
  4. use Kunstmaan\NodeBundle\Controller\SlugActionInterface;
  5. use Kunstmaan\NodeBundle\Entity\NodeTranslation;
  6. use Kunstmaan\NodeBundle\Event\Events;
  7. use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  12. class SlugListener
  13. {
  14.     /**
  15.      * @var EntityManager
  16.      */
  17.     protected $em;
  18.     /**
  19.      * @var ControllerResolverInterface
  20.      */
  21.     protected $resolver;
  22.     /**
  23.      * @var EventDispatcherInterface
  24.      */
  25.     protected $eventDispatcher;
  26.     /**
  27.      * SlugListener constructor.
  28.      *
  29.      * @param EntityManager               $em
  30.      * @param ControllerResolverInterface $resolver
  31.      * @param EventDispatcherInterface    $eventDispatcher
  32.      */
  33.     public function __construct(EntityManager $emControllerResolverInterface $resolverEventDispatcherInterface $eventDispatcher)
  34.     {
  35.         $this->em $em;
  36.         $this->resolver $resolver;
  37.         $this->eventDispatcher $eventDispatcher;
  38.     }
  39.     /**
  40.      * @param FilterControllerEvent|ControllerEvent $event
  41.      *
  42.      * @throws \Exception
  43.      */
  44.     public function onKernelController($event)
  45.     {
  46.         if (!$event instanceof FilterControllerEvent && !$event instanceof ControllerEvent) {
  47.             throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ControllerEvent::class) ? ControllerEvent::class : FilterControllerEvent::class, \is_object($event) ? \get_class($event) : \gettype($event)));
  48.         }
  49.         $request $event->getRequest();
  50.         // Check if the event has a nodeTranslation, if not this method can be skipped
  51.         if (!$request->attributes->has('_nodeTranslation')) {
  52.             return;
  53.         }
  54.         $nodeTranslation $request->attributes->get('_nodeTranslation');
  55.         if (!($nodeTranslation instanceof NodeTranslation)) {
  56.             throw new \Exception('Invalid _nodeTranslation value found in request attributes');
  57.         }
  58.         $entity $nodeTranslation->getRef($this->em);
  59.         // If the entity is an instance of the SlugActionInterface, change the controller
  60.         if ($entity instanceof SlugActionInterface) {
  61.             $request->attributes->set('_entity'$entity);
  62.             // Do security check by firing an event that gets handled by the SlugSecurityListener
  63.             $securityEvent = new SlugSecurityEvent();
  64.             $securityEvent
  65.                 ->setNode($nodeTranslation->getNode())
  66.                 ->setEntity($entity)
  67.                 ->setRequest($request)
  68.                 ->setNodeTranslation($nodeTranslation);
  69.             $this->eventDispatcher->dispatch(Events::SLUG_SECURITY$securityEvent);
  70.             // Set the right controller
  71.             $request->attributes->set('_controller'$entity->getControllerAction());
  72.             $event->setController($this->resolver->getController($request));
  73.         }
  74.     }
  75. }