Passed
Pull Request — master (#175)
by Arnaud
08:30 queued 02:17
created

MenuProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Bridge\KnpMenu\Provider;
4
5
use Knp\Menu\FactoryInterface;
6
use Knp\Menu\ItemInterface;
7
use Knp\Menu\Provider\MenuProviderInterface;
8
use LAG\AdminBundle\Factory\ConfigurationFactory;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
class MenuProvider implements MenuProviderInterface
12
{
13
    /**
14
     * @var FactoryInterface
15
     */
16
    private $factory;
17
18
    /**
19
     * @var ConfigurationFactory
20
     */
21
    private $configurationFactory;
22
23
    /**
24
     * @var array
25
     */
26
    private $menuConfigurations;
27
28
    /**
29
     * @var RequestStack
30
     */
31
    private $requestStack;
32
33
    public function __construct(
34
        array $menuConfigurations,
35
        FactoryInterface $factory,
36
        ConfigurationFactory $configurationFactory,
37
        RequestStack $requestStack
38
    ) {
39
        $this->factory = $factory;
40
        $this->configurationFactory = $configurationFactory;
41
        $this->menuConfigurations = $menuConfigurations;
42
        $this->requestStack = $requestStack;
43
    }
44
45
    public function get(string $name, array $options = []): ItemInterface
46
    {
47
        $menuConfiguration = $this->configurationFactory->createMenuConfiguration($name, $options)->all();
48
        $menu = $this->factory->createItem('root', [
49
            'attributes' => $menuConfiguration['attributes'],
50
        ]);
51
        $currentRoute = $this->requestStack->getMasterRequest()->get('_route');
52
53
        foreach ($menuConfiguration['children'] as $itemConfiguration) {
54
            $child = $menu->addChild($itemConfiguration['text'], $itemConfiguration);
55
56
            if (!empty($itemConfiguration['icon'])) {
57
                $child->setExtra('icon', $itemConfiguration['icon']);
58
            }
59
60
            if (!empty($itemConfiguration['route']) && $itemConfiguration['route'] === $currentRoute) {
61
                $class = $child->setCurrent(true)->getAttribute('class');
62
                $child->setAttribute('class', $class.' current');
63
            }
64
            // TODO move in the bootstrap theme
65
            $class = $child->getAttribute('class');
66
            $child->setAttribute('class', $class.' nav-item');
67
68
            $linkClass = $child->getLinkAttribute('class');
69
            $child->setLinkAttribute('class', $linkClass.' nav-link');
70
        }
71
72
        return $menu;
73
    }
74
75
    public function has(string $name, array $options = []): bool
76
    {
77
        return key_exists($name, $this->menuConfigurations);
78
    }
79
80
    /**
81
     * @return ItemInterface[]
82
     */
83
    public function all(): array
84
    {
85
        $menus = [];
86
87
        foreach ($this->menuConfigurations as $menuName => $menuConfiguration) {
88
            if (!is_array($menuConfiguration)) {
89
                $menuConfiguration = [];
90
            }
91
            $menus[$menuName] = $this->get($menuName, $menuConfiguration);
92
        }
93
94
        return $menus;
95
    }
96
}
97