Completed
Pull Request — master (#154)
by Arnaud
03:49
created

MenuSubscriber::defineMenuConfiguration()   C

Complexity

Conditions 15
Paths 67

Size

Total Lines 50
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
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
rs 5.9166
ccs 19
cts 19
cp 1
crap 15

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
    public static function getSubscribedEvents()
31
    {
32
        return [
33
            Events::MENU_CONFIGURATION => 'defineMenuConfiguration',
34
        ];
35
    }
36
37
    public function __construct(bool $menuEnabled, ResourceRegistryInterface $registry, array $menuConfigurations = [])
38
    {
39 6
        $this->menuEnabled = $menuEnabled;
40
        $this->registry = $registry;
41
        $this->menuConfigurations = $menuConfigurations;
42 6
    }
43
44
    public function defineMenuConfiguration(MenuConfigurationEvent $event)
45
    {
46
        if (!$this->menuEnabled || !key_exists($event->getMenuName(), $this->menuConfigurations)) {
47
            return;
48
        }
49 4
        $menuConfiguration = $this->menuConfigurations[$event->getMenuName()];
50
51
        if (!is_array($menuConfiguration)) {
52
            $menuConfiguration = [];
53
        }
54
        $menuConfiguration = array_merge_recursive($menuConfiguration, $event->getMenuConfiguration());
55 4
        $resourceNames = $this->registry->keys();
56 4
57 4
        if (!key_exists('children', $menuConfiguration) || !is_array($menuConfiguration['children'])) {
58 4
            $menuConfiguration['children'] = [];
59 4
60
            if ('left' === $event->getMenuName()) {
61
                foreach ($resourceNames as $resourceName) {
62
                    $menuConfiguration['children'][$resourceName] = [];
63
                }
64 4
            }
65
        }
66 4
67 2
        foreach ($menuConfiguration['children'] as $itemName => $itemConfiguration) {
68
            if (null === $itemConfiguration) {
69 2
                $itemConfiguration = [];
70 2
            }
71 2
72
            // When an url is set, nothing to add, the item menu can be build
73 2
            if (key_exists('url', $itemConfiguration)) {
74
                $menuConfiguration[$itemName] = $itemConfiguration;
75
76
                continue;
77 2
            }
78 2
79
            // If the key "admin' is missing, we try to find an admin resource with the same name
80 2
            if (!key_exists('admin', $itemConfiguration) && in_array($itemName, $resourceNames)) {
81
                $itemConfiguration['admin'] = $itemName;
82 2
            }
83 2
84
            // The default admins action is list
85 2
            if (key_exists('admin', $itemConfiguration) && !key_exists('action', $itemConfiguration)) {
86
                $itemConfiguration['action'] = 'list';
87
            }
88
89
            $menuConfiguration['children'][$itemName] = $itemConfiguration;
90
        }
91
92
        // Set defaults menu configuration to be build
93
        $event->setMenuConfiguration($menuConfiguration);
94
    }
95
}
96