Passed
Pull Request — master (#1)
by David
01:40
created

MenuRegistry::getRootMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\StaticRegistry\Menu;
5
6
7
class MenuRegistry
8
{
9
    /**
10
     * @var MenuItem
11
     */
12
    private $rootMenu;
13
14
    public function __construct()
15
    {
16
        $this->rootMenu = new MenuItem('root');
17
    }
18
19
    /**
20
     * @return MenuItem
21
     */
22
    public function getRootMenu(): MenuItem
23
    {
24
        return $this->rootMenu;
25
    }
26
27
    /**
28
     * @param string[] $items The path from the root menu to the leaf we are interested in.
29
     * @param null|string $url
30
     * @param float $priority
31
     * @param string|null $cssClass
32
     */
33
    public function registerMenuItem(array $items, ?string $url, float $priority = 0.0, ?string $cssClass = null): void
34
    {
35
        $this->registerSubMenuItem($this->rootMenu, $items, $url, $priority, $cssClass);
36
    }
37
38
    /**
39
     * @param MenuItem $menuItem
40
     * @param string[] $items The path from $menuItem to the leaf we are interested in.
41
     * @param null|string $url
42
     * @param float $priority
43
     * @param string|null $cssClass
44
     */
45
    private function registerSubMenuItem(MenuItem $menuItem, array $items, ?string $url, float $priority = 0.0, ?string $cssClass): void
46
    {
47
        $label = array_shift($items);
48
49
        if (\count($items) > 0) {
50
            $childMenuItem = $menuItem->findChild($label);
51
            if ($childMenuItem === null) {
52
                $childMenuItem = new MenuItem($label);
53
                $menuItem->addMenuItem($childMenuItem, 0);
54
            }
55
            $this->registerSubMenuItem($childMenuItem, $items, $url, $priority, $cssClass);
56
            return;
57
        }
58
59
        $childMenuItem = new MenuItem($label, $url);
60
        $childMenuItem->setCssClass($cssClass);
61
        $menuItem->addMenuItem($childMenuItem, $priority);
62
    }
63
}
64