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
|
|||
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 | |||
59 | return $nodeTranslation->getRef($this->em); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param \Twig_Environment $env |
||
64 | * @param array $twigContext The twig context |
||
65 | * @param HasPagePartsInterface $page The page |
||
66 | * @param string $contextName The pagepart context |
||
67 | * @param array $parameters Some extra parameters |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | View Code Duplication | public function renderIndexablePageParts( |
|
72 | \Twig_Environment $env, |
||
73 | array $twigContext, |
||
74 | HasPagePartsInterface $page, |
||
75 | $contextName = 'main', |
||
76 | array $parameters = array() |
||
77 | ) { |
||
78 | $template = $env->loadTemplate('KunstmaanNodeSearchBundle:PagePart:view.html.twig'); |
||
79 | $pageparts = $this->indexablePagePartsService->getIndexablePageParts($page, $contextName); |
||
80 | $newTwigContext = array_merge( |
||
81 | $parameters, |
||
82 | array( |
||
83 | 'pageparts' => $pageparts, |
||
84 | ) |
||
85 | ); |
||
86 | $newTwigContext = array_merge($newTwigContext, $twigContext); |
||
87 | |||
88 | return $template->render($newTwigContext); |
||
89 | } |
||
90 | } |
||
91 |
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.