Completed
Pull Request — master (#233)
by De Cramer
11:00
created

MenuItems::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Config\Plugins;
4
5
use eXpansion\Bundle\Menu\DataProviders\Listener\ListenerMenuItemProviderInterface;
6
use eXpansion\Bundle\Menu\Model\Menu\ChatCommandItem;
7
use eXpansion\Bundle\Menu\Model\Menu\ParentItem;
8
use eXpansion\Framework\Config\Services\ConfigManagerInterface;
9
10
/**
11
 * Class MenuItems
12
 *
13
 * @author    de Cramer Oliver<[email protected]>
14
 * @copyright 2018 eXpansion
15
 * @package eXpansion\Framework\Config\Plugins
16
 */
17
class MenuItems implements ListenerMenuItemProviderInterface
18
{
19
    /** @var ConfigManagerInterface */
20
    protected $configManager;
21
22
    /**
23
     * MenuItems constructor.
24
     *
25
     * @param ConfigManagerInterface $configManager
26
     */
27
    public function __construct(ConfigManagerInterface $configManager)
28
    {
29
        $this->configManager = $configManager;
30
    }
31
32
33
    /**
34
     * Register items tot he parent item.
35
     *
36
     * @param ParentItem $root
37
     *
38
     * @return mixed
39
     */
40
    public function registerMenuItems(ParentItem $root)
41
    {
42
        $root->addChild(
43
            ParentItem::class,
44
            'admin/config',
45
            'expansion_config.menu.label',
46
            null
47
        );
48
49
        $configTree = $this->configManager->getConfigDefinitionTree();
50
        $this->registerConfigItems($root, 'admin/config', $configTree->getArray());
51
    }
52
53
    /**
54
     * Register config items
55
     *
56
     * @param ParentItem $root
57
     * @param $parentId
58
     * @param $configItems
59
     */
60
    protected function registerConfigItems(ParentItem $root, $parentId, $configItems)
61
    {
62
        $firstElement = reset($configItems);
63
        if (is_array($firstElement)) {
64
            foreach ($configItems as $configId => $configItem) {
65
                $path = $parentId . '/' . $configId;
66
                if (!$root->getChild($path)) {
67
                    $root->addChild(
68
                        ParentItem::class,
69
                        $path,
70
                        'expansion_config.menu.' . $configId,
71
                        null
72
                    );
73
                }
74
75
                $this->registerConfigItems($root, $path, $configItem);
76
            }
77
        } else {
78
            $root->addChild(
79
                ChatCommandItem::class,
80
                $parentId,
81
                'expansion_config.menu.' . implode('.', explode('/', $parentId)),
82
                null,
83
                ['cmd' => '/admin config "' . $parentId . "'"]
84
            );
85
        }
86
    }
87
}
88