Completed
Push — master ( bcd255...1f6303 )
by Ruud
169:22 queued 157:52
created

ConfigBundle/Helper/Menu/ConfigMenuAdaptor.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\ConfigBundle\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 Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
10
11
class ConfigMenuAdaptor implements MenuAdaptorInterface
12
{
13
    /**
14
     * @var array
15
     */
16
    private $configuration;
17
18
    /**
19
     * @var AuthorizationCheckerInterface
20
     */
21
    private $authorizationChecker;
22
23
    /**
24
     * @param array                         $configuration
25
     * @param AuthorizationCheckerInterface $authorizationChecker
26
     */
27
    public function __construct($configuration, AuthorizationCheckerInterface $authorizationChecker)
28
    {
29
        $this->configuration = $configuration;
30
        $this->authorizationChecker = $authorizationChecker;
31
    }
32
33
    /**
34
     * In this method you can add children for a specific parent, but also remove and change the already created children
35
     *
36
     * @param MenuBuilder $menu      The MenuBuilder
37
     * @param MenuItem[]  &$children The current children
38
     * @param MenuItem    $parent    The parent Menu item
39
     * @param Request     $request   The Request
40
     */
41
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
42
    {
43
        if (!is_null($parent) && 'KunstmaanAdminBundle_settings' == $parent->getRoute()) {
44
            // Load all the kunstmaan_config entities and create a menu item for them.
45
            foreach ($this->configuration['entities'] as $class) {
46
                $entity = new $class();
47
48
                $hasAccess = false;
49
                foreach ($entity->getRoles() as $role) {
50
                    if ($this->authorizationChecker->isGranted($role)) {
51
                        $hasAccess = true;
52
                    }
53
                }
54
55
                if ($hasAccess) {
56
                    $menuItem = new MenuItem($menu);
57
                    $menuItem
58
                      ->setRoute('kunstmaanconfigbundle_default')
59
                      ->setRouteParams(array('internalName' => $entity->getInternalName()))
60
                      ->setLabel($entity->getLabel())
61
                      ->setUniqueId($entity->getInternalName())
62
                      ->setParent($parent);
63
64 View Code Duplication
                    if ($request->attributes->get('_route') === $menuItem->getRoute() && $request->attributes->get('internalName') === $entity->getInternalName()) {
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...
65
                        $menuItem->setActive(true);
66
                        $parent->setActive(true);
67
                    }
68
                    $children[] = $menuItem;
69
                }
70
            }
71
        }
72
    }
73
}
74