Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

EventListener/ConfigureActionsMenuListener.php (2 issues)

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\FormBundle\EventListener;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\FormBundle\Entity\AbstractFormPage;
7
use Kunstmaan\NodeBundle\Event\ConfigureActionMenuEvent;
8
use Symfony\Component\Routing\RouterInterface;
9
10
/**
11
 * An event listener to add a formsubmissions link to the submenu of nodes.
12
 */
13
class ConfigureActionsMenuListener
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    private $em;
19
20
    /**
21
     * @var Router
22
     */
23
    private $router;
24
25
    /**
26
     * @param EntityManager   $em     The entity manager
27
     * @param RouterInterface $router The router
28
     */
29
    public function __construct(EntityManager $em, RouterInterface $router)
0 ignored issues
show
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...
30
    {
31
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
It seems like $router of type object<Symfony\Component\Routing\RouterInterface> is incompatible with the declared type object<Kunstmaan\FormBundle\EventListener\Router> of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        $this->em = $em;
33
    }
34
35
    /**
36
     * Configure the form submissions link on top of the form in the sub action menu
37
     *
38
     * @param ConfigureActionMenuEvent $event
39
     */
40
    public function onSubActionMenuConfigure(ConfigureActionMenuEvent $event)
41
    {
42
        $menu = $event->getMenu();
43
        $activeNodeVersion = $event->getActiveNodeVersion();
44
45
        if (!is_null($activeNodeVersion)) {
46
            $page = $activeNodeVersion->getRef($this->em);
47
            if ($page instanceof AbstractFormPage) {
48
                $activeNodeTranslation = $activeNodeVersion->getNodeTranslation();
49
                $menu->addChild('subaction.formsubmissions', array('uri' => $this->router->generate('KunstmaanFormBundle_formsubmissions_list', array('nodeTranslationId' => $activeNodeTranslation->getId()))));
50
            }
51
        }
52
    }
53
}
54