Completed
Push — master ( 13edef...d5b56b )
by Jeroen
85:52 queued 71:18
created

SlugListener::onKernelController()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 38

Duplication

Lines 3
Ratio 7.89 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 3
loc 38
ccs 0
cts 23
cp 0
rs 8.0675
c 0
b 0
f 0
cc 8
nc 5
nop 1
crap 72
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
     * @param EntityManager               $em
36
     * @param ControllerResolverInterface $resolver
37
     * @param EventDispatcherInterface    $eventDispatcher
38
     */
39
    public function __construct(EntityManager $em, ControllerResolverInterface $resolver, EventDispatcherInterface $eventDispatcher)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
40
    {
41
        $this->em = $em;
42
        $this->resolver = $resolver;
43
        $this->eventDispatcher = $eventDispatcher;
44
    }
45
46
    /**
47
     * @param FilterControllerEvent|ControllerEvent $event
48
     *
49
     * @throws \Exception
50
     */
51
    public function onKernelController($event)
52
    {
53 View Code Duplication
        if (!$event instanceof FilterControllerEvent && !$event instanceof ControllerEvent) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            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)));
55
        }
56
57
        $request = $event->getRequest();
58
59
        // Check if the event has a nodeTranslation, if not this method can be skipped
60
        if (!$request->attributes->has('_nodeTranslation')) {
61
            return;
62
        }
63
64
        $nodeTranslation = $request->attributes->get('_nodeTranslation');
65
        if (!($nodeTranslation instanceof NodeTranslation)) {
66
            throw new \Exception('Invalid _nodeTranslation value found in request attributes');
67
        }
68
        $entity = $nodeTranslation->getRef($this->em);
69
70
        // If the entity is an instance of the SlugActionInterface, change the controller
71
        if ($entity instanceof SlugActionInterface) {
72
            $request->attributes->set('_entity', $entity);
73
74
            // Do security check by firing an event that gets handled by the SlugSecurityListener
75
            $securityEvent = new SlugSecurityEvent();
76
            $securityEvent
77
                ->setNode($nodeTranslation->getNode())
78
                ->setEntity($entity)
79
                ->setRequest($request)
80
                ->setNodeTranslation($nodeTranslation);
81
82
            $this->eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);
83
84
            // Set the right controller
85
            $request->attributes->set('_controller', $entity->getControllerAction());
86
            $event->setController($this->resolver->getController($request));
0 ignored issues
show
Security Bug introduced by
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...
87
        }
88
    }
89
}
90