Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

RenderService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 27.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 20
loc 74
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C renderMenuItemTemplate() 20 51 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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']) {
0 ignored issues
show
Duplication introduced by
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...
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
Duplication introduced by
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