Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

KunstmaanNodeSearchTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Twig;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
7
use Kunstmaan\NodeBundle\Entity\Node;
8
use Kunstmaan\NodeSearchBundle\Helper\IndexablePagePartsService;
9
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
10
11
class KunstmaanNodeSearchTwigExtension extends \Twig_Extension
12
{
13
    /**
14
     * @var EntityManager
15
     */
16
    private $em;
17
18
    /**
19
     * @var IndexablePagePartsService
20
     */
21
    private $indexablePagePartsService;
22
23
    /**
24
     * @param EntityManager             $em
25
     * @param IndexablePagePartsService $indexablePagePartsService
26
     */
27
    public function __construct(EntityManager $em, IndexablePagePartsService $indexablePagePartsService)
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...
28
    {
29
        $this->em = $em;
30
        $this->indexablePagePartsService = $indexablePagePartsService;
31
    }
32
33
    /**
34
     * Returns a list of functions to add to the existing list.
35
     *
36
     * @return array An array of functions
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Twig_SimpleFunction[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
37
     */
38 View Code Duplication
    public function getFunctions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
    {
40
        return array(
41
            new \Twig_SimpleFunction('get_parent_page', array($this, 'getParentPage')),
42
            new \Twig_SimpleFunction('render_indexable_pageparts', array($this, 'renderIndexablePageParts'), array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('html'))),
43
        );
44
    }
45
46
    /**
47
     * @param HasNodeInterface $page
48
     * @param string           $locale
49
     *
50
     * @return HasNodeInterface
51
     */
52
    public function getParentPage(HasNodeInterface $page, $locale)
53
    {
54
        /** @var Node $node */
55
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page);
56
        $parentNode = $node->getParent();
57
        $nodeTranslation = $parentNode->getNodeTranslation($locale);
58
        $parentPage = $nodeTranslation->getRef($this->em);
59
60
        return $parentPage;
61
    }
62
63
    /**
64
     * @param \Twig_Environment     $env
65
     * @param array                 $twigContext The twig context
66
     * @param HasPagePartsInterface $page        The page
67
     * @param string                $contextName The pagepart context
68
     * @param array                 $parameters  Some extra parameters
69
     *
70
     * @return string
71
     */
72 View Code Duplication
    public function renderIndexablePageParts(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
73
        \Twig_Environment $env,
74
        array $twigContext,
75
        HasPagePartsInterface $page,
76
        $contextName = 'main',
77
        array $parameters = array()
78
    )
79
    {
80
        $template = $env->loadTemplate('KunstmaanNodeSearchBundle:PagePart:view.html.twig');
81
        $pageparts = $this->indexablePagePartsService->getIndexablePageParts($page, $contextName);
82
        $newTwigContext = array_merge(
83
            $parameters,
84
            array(
85
                'pageparts' => $pageparts
86
            )
87
        );
88
        $newTwigContext = array_merge($newTwigContext, $twigContext);
89
90
        return $template->render($newTwigContext);
91
    }
92
}
93