Passed
Push — master ( df324d...7773d7 )
by Caen
03:06 queued 14s
created

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