Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
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 $authorizationChecker
27
     * @param bool                          $isEnabledVersionChecker
28
     */
29 6
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, $isEnabledVersionChecker)
30
    {
31 6
        $this->authorizationChecker = $authorizationChecker;
32 6
        $this->isEnabledVersionChecker = (bool) $isEnabledVersionChecker;
33 6
    }
34
35
    /**
36
     * In this method you can add children for a specific parent, but also remove and change the already created children
37
     *
38
     * @param MenuBuilder $menu      The MenuBuilder
39
     * @param MenuItem[]  &$children The current children
40
     * @param MenuItem    $parent    The parent Menu item
41
     * @param Request     $request   The Request
42
     */
43 5
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
44
    {
45 5 View Code Duplication
        if (is_null($parent)) {
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...
46 1
            $menuItem = new TopMenuItem($menu);
47
            $menuItem
48 1
                ->setRoute('KunstmaanAdminBundle_settings')
49 1
                ->setLabel('settings.title')
50 1
                ->setUniqueId('settings')
51 1
                ->setParent($parent)
52 1
                ->setRole('settings');
53 1
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
54 1
                $menuItem->setActive(true);
55
            }
56 1
            $children[] = $menuItem;
57
        }
58
59 5
        if (!is_null($parent) && 'KunstmaanAdminBundle_settings' == $parent->getRoute()) {
60 3
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN') && $this->isEnabledVersionChecker) {
61 2
                $menuItem = new MenuItem($menu);
62
                $menuItem
63 2
                    ->setRoute('KunstmaanAdminBundle_settings_bundle_version')
64 2
                    ->setLabel('settings.version.bundle')
65 2
                    ->setUniqueId('bundle_versions')
66 2
                    ->setParent($parent);
67 2
                if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
68 1
                    $menuItem->setActive(true);
69
                }
70 2
                $children[] = $menuItem;
71
            }
72
73 3
            $menuItem = new MenuItem($menu);
74
            $menuItem
75 3
                ->setRoute('kunstmaanadminbundle_admin_exception')
76 3
                ->setLabel('settings.exceptions.title')
77 3
                ->setUniqueId('exceptions')
78 3
                ->setParent($parent);
79 3
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
80 2
                $menuItem->setActive(true);
81 2
                $parent->setActive(true);
82
            }
83 3
            $children[] = $menuItem;
84
        }
85 5
    }
86
}
87