Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

EventListener/ConfigureActionsMenuListener.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\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;
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