Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
BaseNavigationMenu
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 generate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 canAddRoute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeDuplicateItems
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 sortByPriority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItems
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Features\Navigation;
6
7use Hyde\Facades\Config;
8use Hyde\Support\Models\Route;
9use Hyde\Foundation\Facades\Routes;
10use Illuminate\Support\Collection;
11
12use function collect;
13
14abstract class BaseNavigationMenu
15{
16    /** @var \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Navigation\NavItem> */
17    public Collection $items;
18
19    final protected function __construct()
20    {
21        $this->items = new Collection();
22    }
23
24    public static function create(): static
25    {
26        $menu = new static();
27
28        $menu->generate();
29        $menu->sortByPriority();
30        $menu->removeDuplicateItems();
31
32        return $menu;
33    }
34
35    protected function generate(): void
36    {
37        Routes::each(function (Route $route): void {
38            if ($this->canAddRoute($route)) {
39                $this->items->put($route->getRouteKey(), NavItem::fromRoute($route));
40            }
41        });
42
43        collect(Config::getArray('hyde.navigation.custom', []))->each(function (NavItem $item): void {
44            // Since these were added explicitly by the user, we can assume they should always be shown
45            $this->items->push($item);
46        });
47    }
48
49    protected function canAddRoute(Route $route): bool
50    {
51        return $route->getPage()->showInNavigation();
52    }
53
54    protected function removeDuplicateItems(): void
55    {
56        $this->items = $this->items->unique(function (NavItem $item): string {
57            // Filter using a combination of the group and label to allow duplicate labels in different groups
58            return $item->getGroup().$item->label;
59        });
60    }
61
62    protected function sortByPriority(): void
63    {
64        $this->items = $this->items->sortBy('priority')->values();
65    }
66
67    /** @return \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavItem> */
68    public function getItems(): Collection
69    {
70        return $this->items;
71    }
72}