Completed
Push — master ( 1fe708...6360f1 )
by Arnaud
16s queued 12s
created

MenuItemConfigurationListener::__invoke()   B

Complexity

Conditions 11
Paths 55

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 22
cp 0
rs 7.3166
cc 11
nc 55
nop 1
crap 132

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
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Event\Listener\Menu;
6
7
use LAG\AdminBundle\Admin\Resource\Registry\ResourceRegistryInterface;
8
use LAG\AdminBundle\Event\Events\Configuration\MenuConfigurationEvent;
9
use LAG\AdminBundle\Exception\Exception;
10
11
class MenuItemConfigurationListener
12
{
13
    private ResourceRegistryInterface $registry;
14
15
    public function __construct(ResourceRegistryInterface $registry)
16
    {
17
        $this->registry = $registry;
18
    }
19
20
    public function __invoke(MenuConfigurationEvent $event): void
21
    {
22
        $menuConfiguration = $event->getMenuConfiguration();
23
        $menuName = $event->getMenuName();
24
        $menuConfiguration['children'] = $menuConfiguration['children'] ?? [];
25
26
        foreach ($menuConfiguration['children'] as $itemName => $itemConfiguration) {
27
            if (null === $itemConfiguration) {
28
                $itemConfiguration = [];
29
            }
30
31
            if ($menuName === 'top') {
32
                if (empty($itemConfiguration['attributes']['class'])) {
33
                    $itemConfiguration['attributes']['class'] = '';
34
                }
35
                $itemConfiguration['attributes']['class'] .= '';
36
                $itemConfiguration['linkAttributes'] = [];
37
            }
38
            // When an url is set, nothing to add, the item menu can be build
39
            if (\array_key_exists('url', $itemConfiguration)) {
40
                continue;
41
            }
42
43
            // If the key "admin' is missing, we try to find an admin resource with the same name
44
            if (!\array_key_exists('admin', $itemConfiguration) && $this->registry->has($itemName)) {
45
                $itemConfiguration['admin'] = $itemName;
46
            }
47
48
            // The default admins action is list
49
            if (\array_key_exists('admin', $itemConfiguration) && empty($itemConfiguration['action'])) {
50
                $itemConfiguration['action'] = 'list';
51
            }
52
53
            // At this point, an pair admin/action or an url or an admin should be defined
54
            if (!\array_key_exists('admin', $itemConfiguration)) {
55
                throw new Exception(sprintf('The configuration of the children "%s" in the menu "%s" is invalid : no admin/action nor url configured, and no admin with the name "%s" exists', $itemName, $menuName, $itemName));
56
            }
57
            $menuConfiguration['children'][$itemName] = $itemConfiguration;
58
        }
59
        $event->setMenuConfiguration($menuConfiguration);
60
    }
61
}
62