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

SimpleMenuAdaptor::adaptChildren()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 26

Duplication

Lines 4
Ratio 15.38 %

Importance

Changes 0
Metric Value
dl 4
loc 26
rs 8.5706
c 0
b 0
f 0
cc 7
nc 5
nop 4
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\Menu;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
7
8
class SimpleMenuAdaptor implements MenuAdaptorInterface
9
{
10
    /**
11
     * @var AuthorizationCheckerInterface
12
     */
13
    private $authorizationChecker;
14
15
    /**
16
     * @var array
17
     */
18
    private $menuItems;
19
20
    /**
21
     * @param AuthorizationCheckerInterface $authorizationChecker
22
     * @param array $menuItems
23
     */
24
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, array $menuItems)
25
    {
26
        $this->authorizationChecker = $authorizationChecker;
27
        $this->menuItems = $menuItems;
28
    }
29
30
    /**
31
     * In this method you can add children for a specific parent, but also remove and change the already created
32
     * children
33
     *
34
     * @param MenuBuilder $menu The MenuBuilder
35
     * @param MenuItem[] &$children The current children
36
     * @param MenuItem|null $parent The parent Menu item
37
     * @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...
38
     */
39
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
40
    {
41
        foreach ($this->menuItems as $item) {
42
            if (false === $this->parentMatches($parent, $item)) {
43
                continue;
44
            }
45
46
            if ($item['role'] && false === $this->authorizationChecker->isGranted($item['role'])) {
47
                continue;
48
            }
49
50
            $menuItem = new TopMenuItem($menu);
51
            $menuItem
52
                ->setRoute($item['route'], $item['params'])
53
                ->setLabel($item['label'])
54
                ->setUniqueId($item['route'])
55
                ->setParent($parent);
56
57 View Code Duplication
            if ($request && stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
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...
58
                $menuItem->setActive(true);
59
                $parent->setActive(true);
0 ignored issues
show
Bug introduced by
It seems like $parent is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
60
            }
61
62
            $children[] = $menuItem;
63
        }
64
    }
65
66
    /**
67
     * @param MenuItem $parent
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...
68
     * @param array    $item
69
     *
70
     * @return bool
71
     */
72
    private function parentMatches(MenuItem $parent = null, $item)
73
    {
74
        if (null === $parent) {
75
            return null === $item['parent'];
76
        }
77
78
        return $item['parent'] === $parent->getRoute();
79
    }
80
}
81