Completed
Push — 5.3 ( d18aef...16c32d )
by Jeroen
15:04 queued 08:59
created

SettingsMenuAdaptor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 17.11 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 13
loc 76
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B adaptChildren() 13 43 9

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\AdminBundle\Helper\Menu;
4
5
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * SettingsMenuAdaptor to add the Settings MenuItem to the top menu and build the Settings tree
10
 */
11
class SettingsMenuAdaptor implements MenuAdaptorInterface
12
{
13
    /**
14
     * @var AuthorizationCheckerInterface
15
     */
16
    private $authorizationChecker;
17
18
    /**
19
     * @var bool
20
     */
21
    private $isEnabledVersionChecker;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param AuthorizationCheckerInterface $authorizationChecker
27
     * @param bool                          $isEnabledVersionChecker
28
     */
29 6
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, $isEnabledVersionChecker)
30
    {
31 6
        $this->authorizationChecker = $authorizationChecker;
32 6
        $this->isEnabledVersionChecker = (bool) $isEnabledVersionChecker;
33 6
    }
34
35
    /**
36
     * In this method you can add children for a specific parent, but also remove and change the already created children
37
     *
38
     * @param MenuBuilder $menu      The MenuBuilder
39
     * @param MenuItem[]  &$children The current children
40
     * @param MenuItem    $parent    The parent Menu item
0 ignored issues
show
Documentation introduced by
Should the type for parameter $parent not be null|MenuItem?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     * @param Request     $request   The Request
0 ignored issues
show
Documentation introduced by
Should the type for parameter $request not be null|Request?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
42
     */
43 5
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
44
    {
45 5 View Code Duplication
        if (is_null($parent)) {
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...
46 1
            $menuItem = new TopMenuItem($menu);
47
            $menuItem
48 1
                ->setRoute('KunstmaanAdminBundle_settings')
49 1
                ->setLabel('settings.title')
50 1
                ->setUniqueId('settings')
51 1
                ->setParent($parent)
52 1
                ->setRole('settings');
53 1
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
54 1
                $menuItem->setActive(true);
55
            }
56 1
            $children[] = $menuItem;
57
        }
58
59 5
        if (!is_null($parent) && 'KunstmaanAdminBundle_settings' == $parent->getRoute()) {
60 3
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN') && $this->isEnabledVersionChecker) {
61 2
                $menuItem = new MenuItem($menu);
62
                $menuItem
63 2
                    ->setRoute('KunstmaanAdminBundle_settings_bundle_version')
64 2
                    ->setLabel('settings.version.bundle')
65 2
                    ->setUniqueId('bundle_versions')
66 2
                    ->setParent($parent);
67 2
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
68 1
                    $menuItem->setActive(true);
69
                }
70 2
                $children[] = $menuItem;
71
            }
72
73 3
            $menuItem = new MenuItem($menu);
74
            $menuItem
75 3
                ->setRoute('kunstmaanadminbundle_admin_exception')
76 3
                ->setLabel('settings.exceptions.title')
77 3
                ->setUniqueId('exceptions')
78 3
                ->setParent($parent);
79 3
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
80 2
                $menuItem->setActive(true);
81 2
                $parent->setActive(true);
82
            }
83 3
            $children[] = $menuItem;
84
        }
85 5
    }
86
}
87