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

AdminBundle/Helper/Menu/SimpleMenuAdaptor.php (1 issue)

Check that @param annotations have the correct type.

Documentation Informational

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 3
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, array $menuItems)
25
    {
26 3
        $this->authorizationChecker = $authorizationChecker;
27 3
        $this->menuItems = $menuItems;
28 3
    }
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
38
     */
39 2
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
40
    {
41 2
        foreach ($this->menuItems as $item) {
42 2
            if (false === $this->parentMatches($parent, $item)) {
43 2
                continue;
44
            }
45
46 2
            if ($item['role'] && false === $this->authorizationChecker->isGranted($item['role'])) {
47 2
                continue;
48
            }
49
50 2
            $menuItem = new TopMenuItem($menu);
51
            $menuItem
52 2
                ->setRoute($item['route'], $item['params'])
53 2
                ->setLabel($item['label'])
54 2
                ->setUniqueId($item['route'])
55 2
                ->setParent($parent);
56
57 2
            if ($request && null !== $parent && stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
58 1
                $menuItem->setActive(true);
59 1
                $parent->setActive(true);
60
            }
61
62 2
            $children[] = $menuItem;
63
        }
64 2
    }
65
66
    /**
67
     * @param MenuItem $parent
0 ignored issues
show
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 2
    private function parentMatches(MenuItem $parent = null, $item)
73
    {
74 2
        if (null === $parent) {
75 1
            return null === $item['parent'];
76
        }
77
78 1
        return $item['parent'] === $parent->getRoute();
79
    }
80
}
81