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

Twig/KunstmaanNodeSearchTwigExtension.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\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)
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
37
     */
38 View Code Duplication
    public function getFunctions()
0 ignored issues
show
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(
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