Completed
Push — master ( e8bfce...313e31 )
by Arnaud
13s queued 11s
created

MenuSubscriber::defineMenuConfiguration()   C

Complexity

Conditions 15
Paths 67

Size

Total Lines 50
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 15

Importance

Changes 0
Metric Value
cc 15
eloc 24
c 0
b 0
f 0
nc 67
nop 1
dl 0
loc 50
ccs 25
cts 25
cp 1
crap 15
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LAG\AdminBundle\Event\Subscriber;
4
5
use LAG\AdminBundle\Event\Events;
6
use LAG\AdminBundle\Event\Menu\MenuConfigurationEvent;
7
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
class MenuSubscriber implements EventSubscriberInterface
11
{
12
    /**
13
     * @var bool
14
     */
15
    private $menuEnabled;
16
17
    /**
18
     * @var ResourceRegistryInterface
19
     */
20
    private $registry;
21
22
    /**
23
     * @var array
24
     */
25
    private $menuConfigurations;
26
27
    /**
28
     * @return array
29
     */
30 4
    public static function getSubscribedEvents()
31
    {
32
        return [
33 4
            Events::MENU_CONFIGURATION => 'defineMenuConfiguration',
34
        ];
35
    }
36
37 8
    public function __construct(bool $menuEnabled, ResourceRegistryInterface $registry, array $menuConfigurations = [])
38
    {
39 8
        $this->menuEnabled = $menuEnabled;
40 8
        $this->registry = $registry;
41 8
        $this->menuConfigurations = $menuConfigurations;
42 8
    }
43
44 6
    public function defineMenuConfiguration(MenuConfigurationEvent $event)
45
    {
46 6
        if (!$this->menuEnabled || !key_exists($event->getMenuName(), $this->menuConfigurations)) {
47 2
            return;
48
        }
49 4
        $menuConfiguration = $this->menuConfigurations[$event->getMenuName()];
50
51 4
        if (!is_array($menuConfiguration)) {
52 2
            $menuConfiguration = [];
53
        }
54 4
        $menuConfiguration = array_merge_recursive($menuConfiguration, $event->getMenuConfiguration());
55 4
        $resourceNames = $this->registry->keys();
56
57 4
        if (!key_exists('children', $menuConfiguration) || !is_array($menuConfiguration['children'])) {
58 2
            $menuConfiguration['children'] = [];
59
60 2
            if ('left' === $event->getMenuName()) {
61 2
                foreach ($resourceNames as $resourceName) {
62 2
                    $menuConfiguration['children'][$resourceName] = [];
63
                }
64
            }
65
        }
66
67 4
        foreach ($menuConfiguration['children'] as $itemName => $itemConfiguration) {
68 4
            if (null === $itemConfiguration) {
69 2
                $itemConfiguration = [];
70
            }
71
72
            // When an url is set, nothing to add, the item menu can be build
73 4
            if (key_exists('url', $itemConfiguration)) {
74 2
                $menuConfiguration[$itemName] = $itemConfiguration;
75
76 2
                continue;
77
            }
78
79
            // If the key "admin' is missing, we try to find an admin resource with the same name
80 4
            if (!key_exists('admin', $itemConfiguration) && in_array($itemName, $resourceNames)) {
81 2
                $itemConfiguration['admin'] = $itemName;
82
            }
83
84
            // The default admins action is list
85 4
            if (key_exists('admin', $itemConfiguration) && !key_exists('action', $itemConfiguration)) {
86 2
                $itemConfiguration['action'] = 'list';
87
            }
88
89 4
            $menuConfiguration['children'][$itemName] = $itemConfiguration;
90
        }
91
92
        // Set defaults menu configuration to be build
93 4
        $event->setMenuConfiguration($menuConfiguration);
94 4
    }
95
}
96