Passed
Push — master ( 558174...f2f8c0 )
by Caen
02:58 queued 12s
created

NavigationMenu::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Hyde\Framework\Models\Navigation;
4
5
use Hyde\Framework\Contracts\RouteContract;
6
use Hyde\Framework\Hyde;
7
use Hyde\Framework\Models\Route;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * @see \Hyde\Framework\Testing\Feature\NavigationMenuTest
12
 * @phpstan-consistent-constructor
13
 */
14
class NavigationMenu
15
{
16
    public RouteContract $currentRoute;
17
18
    public Collection $items;
19
20
    public function __construct()
21
    {
22
        $this->items = new Collection();
23
    }
24
25
    public static function create(): static
26
    {
27
        return (new static())->generate()->filter()->sort();
28
    }
29
30
    /** @return $this */
31
    public function generate(): static
32
    {
33
        Hyde::routes()->each(function (Route $route) {
34
            $this->items->push(NavItem::fromRoute($route));
35
        });
36
37
        collect(config('hyde.navigation.custom', []))->each(function (NavItem $item) {
38
            $this->items->push($item);
39
        });
40
41
        return $this;
42
    }
43
44
    /** @return $this */
45
    public function filter(): static
46
    {
47
        $this->items = $this->filterHiddenItems();
48
        $this->items = $this->filterDuplicateItems();
49
50
        return $this;
51
    }
52
53
    /** @return $this */
54
    public function sort(): static
55
    {
56
        $this->items = $this->items->sortBy('priority')->values();
57
58
        return $this;
59
    }
60
61
    protected function filterHiddenItems(): Collection
62
    {
63
        return $this->items->reject(function (NavItem $item) {
64
            return $item->hidden;
65
        })->values();
66
    }
67
68
    protected function filterDuplicateItems(): Collection
69
    {
70
        return $this->items->unique(function (NavItem $item) {
71
            return $item->resolveLink();
72
        });
73
    }
74
}
75