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

MenuItems   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 84
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B registerMenuItems() 0 24 1
B registerConfigItems() 0 28 3
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',
45
            'expansion_menu.admin.label',
46
            null
47
        );
48
        $root->addChild(
49
            ParentItem::class,
50
            'admin/server',
51
            'expansion_admin.menu.server',
52
            null
53
        );
54
        $root->addChild(
55
            ParentItem::class,
56
            'admin/server/config',
57
            'expansion_config.menu.label',
58
            null
59
        );
60
61
        $configTree = $this->configManager->getConfigDefinitionTree();
62
        $this->registerConfigItems($root, 'admin/server/config', $configTree->getArray());
63
    }
64
65
    /**
66
     * Register config items
67
     *
68
     * @param ParentItem $root
69
     * @param $parentId
70
     * @param $configItems
71
     */
72
    protected function registerConfigItems(ParentItem $root, $parentId, $configItems)
73
    {
74
        foreach ($configItems as $configId => $configItem) {
75
            $subItems = reset($configItem);
76
            $path = $parentId . '/' . $configId;
77
            $configPath = str_replace("admin/server/config/",'', $path);
78
            $translationKey = 'expansion_config.menu.' . implode('.', explode('/', $configPath)) . '.label';
79
80
            if (is_array($subItems)) {
81
                $root->addChild(
82
                    ParentItem::class,
83
                    $path,
84
                    $translationKey,
85
                    null
86
                );
87
88
                $this->registerConfigItems($root, $path, $configItem);
89
            } else {
90
                $root->addChild(
91
                    ChatCommandItem::class,
92
                    $path,
93
                    $translationKey,
94
                    null,
95
                    ['cmd' => '/admin config "' . $configPath . '"']
96
                );
97
            }
98
        }
99
    }
100
}
101