Completed
Pull Request — master (#290)
by Leny
07:15
created

ViewHelper::addTranslation()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 8.439
cc 5
eloc 21
nc 6
nop 3
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Helper;
4
5
use Doctrine\Orm\EntityManager;
6
use Victoire\Bundle\CoreBundle\Entity\View;
7
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
8
use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper;
9
use Victoire\Bundle\ViewReferenceBundle\Provider\ViewReferenceProvider;
10
11
/**
12
 * Page helper
13
 * ref: victoire_core.view_helper.
14
 */
15
class ViewHelper
16
{
17
    /**
18
     * @var EntityManager
19
     */
20
    private $entityManager;
21
    /**
22
     * @var ViewReferenceProvider
23
     */
24
    private $viewReferenceProvider;
25
    /**
26
     * @var ViewReferenceHelper
27
     */
28
    private $viewReferenceHelper;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @internal param $ViewManagerChain $$viewManagerChain
34
     *
35
     * @param EntityManager         $entityManager
36
     * @param ViewReferenceHelper   $viewReferenceHelper
37
     * @param ViewReferenceProvider $viewReferenceProvider
38
     */
39
    public function __construct(
40
        EntityManager $entityManager,
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. 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...
41
        ViewReferenceProvider $viewReferenceProvider,
42
        ViewReferenceHelper $viewReferenceHelper
43
    ) {
44
        $this->entityManager = $entityManager;
45
        $this->viewReferenceProvider = $viewReferenceProvider;
46
        $this->viewReferenceHelper = $viewReferenceHelper;
47
    }
48
49
    /**
50
     * @return WebViewInterface[]
51
     */
52
    public function buildViewsReferences()
53
    {
54
        $viewsHierarchy = $this->entityManager->getRepository('VictoireCoreBundle:View')->getRootNodes();
55
        $views = $this->viewReferenceProvider->getReferencableViews($viewsHierarchy, $this->entityManager);
56
57
        $this->viewReferenceHelper->buildViewReferenceRecursively($views, $this->entityManager);
58
59
        return $views;
60
    }
61
}
62