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

BaseNavigationMenu::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use function collect;
8
use function config;
9
use Hyde\Foundation\Facades\Router;
10
use Hyde\Support\Models\Route;
11
use Illuminate\Support\Collection;
12
13
/**
14
 * @see \Hyde\Framework\Testing\Feature\NavigationMenuTest
15
 */
16
abstract class BaseNavigationMenu
17
{
18
    public Collection $items;
19
20
    final 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
        Router::each(function (Route $route): void {
0 ignored issues
show
Bug introduced by
The method each() does not exist on Hyde\Foundation\Facades\Router. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

33
        Router::/** @scrutinizer ignore-call */ 
34
                each(function (Route $route): void {
Loading history...
34
            $this->items->put($route->getRouteKey(), NavItem::fromRoute($route));
0 ignored issues
show
Bug introduced by
$route->getRouteKey() 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

34
            $this->items->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), NavItem::fromRoute($route));
Loading history...
35
        });
36
37
        collect(config('hyde.navigation.custom', []))->each(function (NavItem $item): void {
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): bool {
64
            return $this->shouldItemBeHidden($item);
65
        })->values();
66
    }
67
68
    protected function filterDuplicateItems(): Collection
69
    {
70
        return $this->items->unique(function (NavItem $item): string {
71
            return $item->label;
72
        });
73
    }
74
75
    protected static function shouldItemBeHidden(NavItem $item): bool
76
    {
77
        return $item->hidden;
78
    }
79
}
80