Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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\ControllerEvent;
13
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
14
15
class SlugListener
16
{
17
    /**
18
     * @var EntityManager
19
     */
20
    protected $em;
21
22
    /**
23
     * @var ControllerResolverInterface
24
     */
25
    protected $resolver;
26
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    protected $eventDispatcher;
31
32
    /**
33
     * SlugListener constructor.
34
     */
35
    public function __construct(EntityManager $em, ControllerResolverInterface $resolver, EventDispatcherInterface $eventDispatcher)
36
    {
37
        $this->em = $em;
38
        $this->resolver = $resolver;
39
        $this->eventDispatcher = $eventDispatcher;
40
    }
41
42
    /**
43
     * @param FilterControllerEvent|ControllerEvent $event
44
     *
45
     * @throws \Exception
46
     */
47
    public function onKernelController($event)
48
    {
49 View Code Duplication
        if (!$event instanceof FilterControllerEvent && !$event instanceof ControllerEvent) {
50
            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)));
51
        }
52
53
        $request = $event->getRequest();
54
55
        // Check if the event has a nodeTranslation, if not this method can be skipped
56
        if (!$request->attributes->has('_nodeTranslation')) {
57
            return;
58
        }
59
60
        $nodeTranslation = $request->attributes->get('_nodeTranslation');
61
        if (!($nodeTranslation instanceof NodeTranslation)) {
62
            throw new \Exception('Invalid _nodeTranslation value found in request attributes');
63
        }
64
        $entity = $nodeTranslation->getRef($this->em);
65
66
        // If the entity is an instance of the SlugActionInterface, change the controller
67
        if ($entity instanceof SlugActionInterface) {
68
            $request->attributes->set('_entity', $entity);
69
70
            // Do security check by firing an event that gets handled by the SlugSecurityListener
71
            $securityEvent = new SlugSecurityEvent();
72
            $securityEvent
73
                ->setNode($nodeTranslation->getNode())
74
                ->setEntity($entity)
75
                ->setRequest($request)
76
                ->setNodeTranslation($nodeTranslation);
77
78
            $this->eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);
79
80
            // Set the right controller
81
            $request->attributes->set('_controller', $entity->getControllerAction());
82
            $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...
83
        }
84
    }
85
}
86