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

NavigationMenu   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 58
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 6 1
A sort() 0 5 1
A generate() 0 11 1
A create() 0 3 1
A filterHiddenItems() 0 5 1
A __construct() 0 3 1
A filterDuplicateItems() 0 4 1
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