Passed
Pull Request — master (#259)
by Arnaud
08:22
created

MenuFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 23
c 1
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 20 2
A __construct() 0 10 1
1
<?php
2
3
namespace LAG\AdminBundle\Menu\Factory;
4
5
use Knp\Menu\FactoryInterface;
6
use Knp\Menu\ItemInterface;
7
use LAG\AdminBundle\Event\Events\MenuEvent;
8
use LAG\AdminBundle\Event\MenuEvents;
9
use LAG\AdminBundle\Factory\Configuration\ConfigurationFactoryInterface;
10
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
11
12
class MenuFactory implements MenuFactoryInterface
13
{
14
    private EventDispatcherInterface $eventDispatcher;
15
    private FactoryInterface $factory;
16
    private ConfigurationFactoryInterface $configurationFactory;
17
    private MenuItemFactoryInterface $menuItemFactory;
18
19
    public function __construct(
20
        EventDispatcherInterface $eventDispatcher,
21
        FactoryInterface $factory,
22
        ConfigurationFactoryInterface $configurationFactory,
23
        MenuItemFactoryInterface $menuItemFactory
24
    ) {
25
        $this->eventDispatcher = $eventDispatcher;
26
        $this->factory = $factory;
27
        $this->configurationFactory = $configurationFactory;
28
        $this->menuItemFactory = $menuItemFactory;
29
    }
30
31
    public function create(string $name, array $options = []): ItemInterface
32
    {
33
        $menuConfiguration = $this->configurationFactory->createMenuConfiguration($name, $options)->toArray();
34
        $menu = $this->factory->createItem('root', [
35
            'attributes' => $menuConfiguration['attributes'],
36
            'extras' => $menuConfiguration['extras'],
37
        ]);
38
        $event = new MenuEvent($name, $menu);
39
        $this->eventDispatcher->dispatch($event, MenuEvents::MENU_CREATE);
40
        $this->eventDispatcher->dispatch($event, sprintf(MenuEvents::MENU_CREATE_SPECIFIC, $name));
41
        $children = $menuConfiguration['children'] ?? [];
42
43
        foreach ($children as $child) {
44
            $child = $this->menuItemFactory->create($child['text'], $child);
45
            $menu->addChild($child);
46
        }
47
        $this->eventDispatcher->dispatch($event, MenuEvents::MENU_CREATED);
48
        $this->eventDispatcher->dispatch($event, sprintf(MenuEvents::MENU_CREATED_SPECIFIC, $name));
49
50
        return $menu;
51
    }
52
}
53