Passed
Push — master ( 0ff158...930e60 )
by Caen
14:22 queued 13s
created

NavigationMenu::shouldItemBeHidden()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use BadMethodCallException;
8
use function config;
9
use Hyde\Pages\DocumentationPage;
10
use Hyde\Pages\MarkdownPost;
11
use function in_array;
12
13
/**
14
 * @see \Hyde\Framework\Testing\Feature\NavigationMenuTest
15
 */
16
class NavigationMenu extends BaseNavigationMenu
17
{
18
    public function generate(): static
19
    {
20
        parent::generate();
21
22
        if ($this->dropdownsEnabled()) {
23
            $this->putGroupedItemsInDropdowns();
24
        }
25
26
        return $this;
27
    }
28
29
    protected function putGroupedItemsInDropdowns(): void
30
    {
31
        $dropdowns = [];
32
33
        /** @var \Hyde\Framework\Features\Navigation\NavItem $item */
34
        foreach ($this->items as $item) {
35
            if ($this->canBeInDropdown($item)) {
36
                // Buffer the item in the dropdowns array
37
                $dropdowns[$item->getGroup()][] = $item;
38
39
                // Remove the item from the main items collection
40
                $this->items->forget($item->route->getRouteKey());
41
            }
42
        }
43
44
        foreach ($dropdowns as $group => $items) {
45
            // Create a new dropdown item containing the buffered items
46
            $this->items->put("dropdown.$group", new DropdownNavItem($group, $items));
0 ignored issues
show
Bug introduced by
'dropdown.'.$group of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $this->items->put(/** @scrutinizer ignore-type */ "dropdown.$group", new DropdownNavItem($group, $items));
Loading history...
47
        }
48
    }
49
50
    public function hasDropdowns(): bool
51
    {
52
        if (! $this->dropdownsEnabled()) {
53
            return false;
54
        }
55
56
        return count($this->getDropdowns()) >= 1;
57
    }
58
59
    /** @return array<string, DropdownNavItem> */
60
    public function getDropdowns(): array
61
    {
62
        if (! $this->dropdownsEnabled()) {
63
            throw new BadMethodCallException('Dropdowns are not enabled. Enable it by setting `hyde.navigation.subdirectories` to `dropdown`.');
64
        }
65
66
        return $this->items->filter(function (NavItem $item): bool {
67
            return $item instanceof DropdownNavItem;
68
        })->all();
69
    }
70
71
    protected static function canBeInDropdown(NavItem $item): bool
72
    {
73
        return ($item->getGroup() !== null) && ! in_array($item->route->getPageClass(), [DocumentationPage::class, MarkdownPost::class]);
74
    }
75
76
    protected static function dropdownsEnabled(): bool
77
    {
78
        return config('hyde.navigation.subdirectories', 'hidden') === 'dropdown';
79
    }
80
81
    protected static function shouldItemBeHidden(NavItem $item): bool
82
    {
83
        return parent::shouldItemBeHidden($item) ||
84
            $item->getRoute()?->getPage() instanceof DocumentationPage && ! $item->getRoute()->is('docs/index');
85
    }
86
}
87