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

MenuSubscriber::buildMenus()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 3
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Event\Subscriber;
4
5
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
6
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage;
7
use LAG\AdminBundle\Event\Events;
8
use LAG\AdminBundle\Event\Events\OldBuildMenuEvent;
9
use LAG\AdminBundle\Event\Menu\MenuConfigurationEvent;
10
use LAG\AdminBundle\Factory\MenuFactory;
0 ignored issues
show
Bug introduced by
The type LAG\AdminBundle\Factory\MenuFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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