Completed
Pull Request — development (#820)
by
unknown
05:24
created

MenuSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 45.9 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 28
loc 61
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 6 1
B onSetupMenu() 28 41 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oc\Components\Subscriber;
6
7
use KevinPapst\AdminLTEBundle\Event\KnpMenuEvent;
8
use Oc\Entity\UserEntity;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\Security\Core\Security;
11
12
class MenuSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @var Security
16
     */
17
    private $security;
18
19
    public function __construct(Security $security)
20
    {
21
        $this->security = $security;
22
    }
23
24
    public static function getSubscribedEvents(): array
25
    {
26
        return [
27
            KnpMenuEvent::class => ['onSetupMenu', 100],
28
        ];
29
    }
30
31
    public function onSetupMenu(KnpMenuEvent $event)
32
    {
33
        $menu = $event->getMenu();
34
35
        $menu->addChild('MainNavigationMenuItem', [
36
            'label' => 'MAIN NAVIGATION',
37
            'childOptions' => $event->getChildOptions()
38
        ])->setAttribute('class', 'header');
39
40 View Code Duplication
        if ($this->security->isGranted("CAN_VIEW", UserEntity::class)) {
41
            $menu->addChild('cache', [
42
                'label' => 'Caches',
43
                'route' => 'backend_caches_index',
44
                'childOptions' => $event->getChildOptions(),
45
            ])->setLabelAttribute('icon', 'fas fa-map-marker-alt');
46
        }
47
48 View Code Duplication
        if ($this->security->isGranted("CAN_VIEW", UserEntity::class)) {
49
            $menu->addChild('coordinate', [
50
                'label' => 'Coordinates',
51
                'route' => 'backend_coordinates_index',
52
                'childOptions' => $event->getChildOptions(),
53
            ])->setLabelAttribute('icon', 'fas fa-map-pin');
54
        }
55
56 View Code Duplication
        if ($this->security->isGranted("CAN_VIEW", UserEntity::class)) {
57
            $menu->addChild('user', [
58
                'label' => 'Users',
59
                'route' => 'backend_user_index',
60
                'childOptions' => $event->getChildOptions(),
61
            ])->setLabelAttribute('icon', 'fas fa-users');
62
        }
63
64 View Code Duplication
        if ($this->security->isGranted("CAN_VIEW", UserEntity::class)) {
65
            $menu->addChild('kitchensink', [
66
                'label' => 'Kitchensink',
67
                'route' => 'app_kitchensink_index',
68
                'childOptions' => $event->getChildOptions(),
69
            ])->setLabelAttribute('icon', 'fab fa-css3');
70
        }
71
    }
72
}
73