Passed
Push — master ( 575989...3d2d69 )
by Caen
04:17 queued 28s
created

hasGroupExplicitlySetInFrontMatter()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use Hyde\Facades\Config;
8
use Hyde\Support\Models\Route;
9
use Hyde\Pages\DocumentationPage;
10
use BadMethodCallException;
11
12
class NavigationMenu extends BaseNavigationMenu
13
{
14
    private bool $hasDropdowns;
15
16
    protected function generate(): void
17
    {
18
        parent::generate();
19
20
        if ($this->dropdownsEnabled()) {
21
            $this->moveGroupedItemsIntoDropdowns();
22
        }
23
    }
24
25
    public function hasDropdowns(): bool
26
    {
27
        return $this->dropdownsEnabled() && count($this->getDropdowns()) >= 1;
28
    }
29
30
    /** @return array<string, DropdownNavItem> */
31
    public function getDropdowns(): array
32
    {
33
        if (! $this->dropdownsEnabled()) {
34
            throw new BadMethodCallException('Dropdowns are not enabled. Enable it by setting `hyde.navigation.subdirectories` to `dropdown`.');
35
        }
36
37
        return $this->items->filter(function (NavItem $item): bool {
38
            return $item instanceof DropdownNavItem;
39
        })->values()->all();
40
    }
41
42
    protected function moveGroupedItemsIntoDropdowns(): void
43
    {
44
        $dropdowns = [];
45
46
        foreach ($this->items as $key => $item) {
47
            if ($this->canAddItemToDropdown($item)) {
48
                // Buffer the item in the dropdowns array
49
                $dropdowns[$item->getGroup()][] = $item;
50
51
                // Remove the item from the main items collection
52
                $this->items->forget($key);
53
            }
54
        }
55
56
        foreach ($dropdowns as $group => $items) {
57
            // Create a new dropdown item containing the buffered items
58
            $this->items->add(new DropdownNavItem($group, $items));
59
        }
60
    }
61
62
    protected function canAddRoute(Route $route): bool
63
    {
64
        return parent::canAddRoute($route) && (! $route->getPage() instanceof DocumentationPage || $route->is(DocumentationPage::homeRouteName()));
65
    }
66
67
    protected function canAddItemToDropdown(NavItem $item): bool
68
    {
69
        return $item->getGroup() !== null;
70
    }
71
72
    protected function dropdownsEnabled(): bool
73
    {
74
        return (Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown') || $this->hasGroupExplicitlySetInFrontMatter();
75
    }
76
77
    private function hasGroupExplicitlySetInFrontMatter(): bool
78
    {
79
        return $this->hasDropdowns ??= $this->items->contains(function (NavItem $item): bool {
80
            return ($item->getGroup() !== null) && ($item->destination !== (string) DocumentationPage::home());
81
        });
82
    }
83
}
84