Completed
Pull Request — 5.0 (#2067)
by Jeroen
31:56 queued 20:07
created

ConfigMenuAdaptor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 6.35 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 4
loc 63
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B adaptChildren() 4 32 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\ConfigBundle\Helper\Menu;
4
5
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
6
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
7
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
10
11
class ConfigMenuAdaptor implements MenuAdaptorInterface
12
{
13
    /**
14
     * @var array $configuration
15
     */
16
    private $configuration;
17
18
    /**
19
     * @var AuthorizationCheckerInterface
20
     */
21
    private $authorizationChecker;
22
23
    /**
24
     * @param array $configuration
25
     * @param AuthorizationCheckerInterface $authorizationChecker
26
     */
27
    public function __construct($configuration, AuthorizationCheckerInterface $authorizationChecker)
28
    {
29
        $this->configuration = $configuration;
30
        $this->authorizationChecker = $authorizationChecker;
31
    }
32
33
    /**
34
     * In this method you can add children for a specific parent, but also remove and change the already created children
35
     *
36
     * @param MenuBuilder $menu The MenuBuilder
37
     * @param MenuItem[] &$children The current children
38
     * @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...
39
     * @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...
40
     */
41
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
42
    {
43
        if (!is_null($parent) && 'KunstmaanAdminBundle_settings' == $parent->getRoute()) {
44
            // Load all the kunstmaan_config entities and create a menu item for them.
45
            foreach ($this->configuration['entities'] as $class) {
46
                $entity = new $class;
47
48
                $hasAccess = false;
49
                foreach ($entity->getRoles() as $role) {
50
                    if ($this->authorizationChecker->isGranted($role)) {
51
                        $hasAccess = true;
52
                    }
53
                }
54
55
                if ($hasAccess) {
56
                    $menuItem = new MenuItem($menu);
57
                    $menuItem
58
                      ->setRoute('kunstmaanconfigbundle_default')
59
                      ->setRouteParams(array('internalName' => $entity->getInternalName()))
60
                      ->setLabel($entity->getLabel())
61
                      ->setUniqueId($entity->getInternalName())
62
                      ->setParent($parent);
63
64 View Code Duplication
					if ($request->attributes->get('_route') === $menuItem->getRoute() && $request->attributes->get('internalName') === $entity->getInternalName()) {
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...
65
                        $menuItem->setActive(true);
66
                        $parent->setActive(true);
67
                    }
68
                    $children[] = $menuItem;
69
                }
70
            }
71
        }
72
    }
73
}
74