Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

AdminBundle/Helper/Menu/SettingsMenuAdaptor.php (2 issues)

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 Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
6
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
7
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem;
8
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem;
9
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * SettingsMenuAdaptor to add the Settings MenuItem to the top menu and build the Settings tree
14
 */
15
class SettingsMenuAdaptor implements MenuAdaptorInterface
16
{
17
    /**
18
     * @var AuthorizationCheckerInterface
19
     */
20
    private $authorizationChecker;
21
22
    /**
23
     * @var boolean
24
     */
25
    private $isEnabledVersionChecker;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param AuthorizationCheckerInterface $container
31
     */
32
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, $isEnabledVersionChecker)
33
    {
34
        $this->authorizationChecker = $authorizationChecker;
35
        $this->isEnabledVersionChecker = (bool) $isEnabledVersionChecker;
36
    }
37
38
    /**
39
     * In this method you can add children for a specific parent, but also remove and change the already created children
40
     *
41
     * @param MenuBuilder $menu      The MenuBuilder
42
     * @param MenuItem[]  &$children The current children
43
     * @param MenuItem    $parent    The parent Menu item
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...
44
     * @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...
45
     */
46
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
47
    {
48
        if (is_null($parent)) {
49
            $menuItem = new TopMenuItem($menu);
50
            $menuItem
51
                ->setRoute('KunstmaanAdminBundle_settings')
52
                ->setLabel('settings.title')
53
                ->setUniqueId('settings')
54
                ->setParent($parent)
55
                ->setRole('settings');
56
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
57
                $menuItem->setActive(true);
58
            }
59
            $children[] = $menuItem;
60
        } elseif ('KunstmaanAdminBundle_settings' == $parent->getRoute()) {
61
            if ( $this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN') ) {
62 View Code Duplication
                if ( $this->isEnabledVersionChecker ) {
63
                    $menuItem = new MenuItem($menu);
64
                    $menuItem
65
                        ->setRoute('KunstmaanAdminBundle_settings_bundle_version')
66
                        ->setLabel('settings.version.bundle')
67
                        ->setUniqueId('bundle_versions')
68
                        ->setParent($parent);
69
                    if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
70
                        $menuItem->setActive(true);
71
                    }
72
                    $children[] = $menuItem;
73
                }
74
            }
75
        }
76
77
        if (!is_null($parent) &&'KunstmaanAdminBundle_settings' == $parent->getRoute()) {
78
            $menuItem = new MenuItem($menu);
79
            $menuItem
80
                ->setRoute('kunstmaanadminbundle_admin_exception')
81
                ->setLabel('settings.exceptions.title')
82
                ->setUniqueId('exceptions')
83
                ->setParent($parent);
84
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
85
                $menuItem->setActive(true);
86
                $parent->setActive(true);
87
            }
88
            $children[] = $menuItem;
89
        }
90
91
    }
92
}
93