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

src/Kunstmaan/MenuBundle/Service/RenderService.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\MenuBundle\Service;
4
5
use Kunstmaan\MenuBundle\Entity\MenuItem;
6
use Symfony\Component\Routing\RouterInterface;
7
use Twig\Environment;
8
9
class RenderService
10
{
11
    /**
12
     * @var RouterInterface
13
     */
14
    private $router;
15
16
    /**
17
     * @param RouterInterface $router
18
     */
19
    public function __construct(RouterInterface $router)
20
    {
21
        $this->router = $router;
22
    }
23
24
    /**
25
     * @param Environment $environment
26
     * @param $node
27
     * @param array $options
28
     *
29
     * @return string
30
     */
31
    public function renderMenuItemTemplate(Environment $environment, $node, $options = array())
32
    {
33
        $template = isset($options['template']) ? $options['template'] : false;
34
        if ($template === false) {
35
            $template = '@KunstmaanMenu/menu-item.html.twig';
36
        }
37
38
        $hasActiveChild = false;
39 View Code Duplication
        if ($node['__children']) {
40
            foreach ($node['__children'] as $childNode) {
41
                if ($childNode['type'] == MenuItem::TYPE_PAGE_LINK) {
42
                    $childUrl = $this->router->generate('_slug', array('url' => $childNode['nodeTranslation']['url']));
43
                    if ($this->router->getContext()->getPathInfo() == $childUrl) {
44
                        $hasActiveChild = true;
45
46
                        break;
47
                    }
48
                }
49
            }
50
        }
51
52
        $active = false;
53 View Code Duplication
        if ($node['type'] == MenuItem::TYPE_PAGE_LINK) {
0 ignored issues
show
This code seems to be duplicated across 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...
54
            $url = $this->router->generate('_slug', array('url' => $node['nodeTranslation']['url']));
55
56
            if ($this->router->getContext()->getPathInfo() == $url) {
57
                $active = true;
58
            }
59
        } else {
60
            $url = $node['url'];
61
        }
62
63
        if ($node['type'] == MenuItem::TYPE_PAGE_LINK) {
64
            if ($node['title']) {
65
                $title = $node['title'];
66
            } else {
67
                $title = $node['nodeTranslation']['title'];
68
            }
69
        } else {
70
            $title = $node['title'];
71
        }
72
73
        return $environment->render($template, array(
74
            'menuItem' => $node,
75
            'url' => $url,
76
            'options' => $options,
77
            'title' => $title,
78
            'active' => $active,
79
            'hasActiveChild' => $hasActiveChild,
80
        ));
81
    }
82
}
83