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

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