Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

NodeBundle/EventListener/SlugListener.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\EventListener;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\NodeBundle\Controller\SlugActionInterface;
7
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
8
use Kunstmaan\NodeBundle\Event\Events;
9
use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
12
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
13
14
class SlugListener
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    protected $em;
20
21
    /**
22
     * @var ControllerResolverInterface
23
     */
24
    protected $resolver;
25
26
    /**
27
     * @var EventDispatcherInterface
28
     */
29
    protected $eventDispatcher;
30
31
    /**
32
     * SlugListener constructor.
33
     *
34
     * @param EntityManager               $em
35
     * @param ControllerResolverInterface $resolver
36
     * @param EventDispatcherInterface    $eventDispatcher
37
     */
38
    public function __construct(EntityManager $em, ControllerResolverInterface $resolver, EventDispatcherInterface $eventDispatcher)
39
    {
40
        $this->em = $em;
41
        $this->resolver = $resolver;
42
        $this->eventDispatcher = $eventDispatcher;
43
    }
44
45
    /**
46
     * @param FilterControllerEvent $event
47
     *
48
     * @throws \Exception
49
     */
50
    public function onKernelController(FilterControllerEvent $event)
51
    {
52
        $request = $event->getRequest();
53
54
        // Check if the event has a nodeTranslation, if not this method can be skipped
55
        if (!$request->attributes->has('_nodeTranslation')) {
56
            return;
57
        }
58
59
        $nodeTranslation = $request->attributes->get('_nodeTranslation');
60
        if (!($nodeTranslation instanceof NodeTranslation)) {
61
            throw new \Exception('Invalid _nodeTranslation value found in request attributes');
62
        }
63
        $entity = $nodeTranslation->getRef($this->em);
64
65
        // If the entity is an instance of the SlugActionInterface, change the controller
66
        if ($entity instanceof SlugActionInterface) {
67
            $request->attributes->set('_entity', $entity);
68
69
            // Do security check by firing an event that gets handled by the SlugSecurityListener
70
            $securityEvent = new SlugSecurityEvent();
71
            $securityEvent
72
                ->setNode($nodeTranslation->getNode())
73
                ->setEntity($entity)
74
                ->setRequest($request)
75
                ->setNodeTranslation($nodeTranslation);
76
77
            $this->eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);
78
79
            // Set the right controller
80
            $request->attributes->set('_controller', $entity->getControllerAction());
81
            $event->setController($this->resolver->getController($request));
0 ignored issues
show
It seems like $this->resolver->getController($request) targeting Symfony\Component\HttpKe...erface::getController() can also be of type false; however, Symfony\Component\HttpKe...rEvent::setController() does only seem to accept callable, did you maybe forget to handle an error condition?
Loading history...
82
        }
83
    }
84
}
85