Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

AdminBundle/Helper/Menu/SimpleMenuAdaptor.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\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
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) {
58
                $menuItem->setActive(true);
59
                $parent->setActive(true);
60
            }
61
62
            $children[] = $menuItem;
63
        }
64
    }
65
66
    /**
67
     * @param MenuItem $parent
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