Passed
Push — master ( a77e6b...67a175 )
by Caen
13:07 queued 12s
created

BaseNavigationMenu::shouldItemBeHidden()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Navigation;
6
7
use Hyde\Facades\Config;
8
use Hyde\Support\Models\Route;
9
use Hyde\Foundation\Facades\Routes;
10
use Illuminate\Support\Collection;
11
use function collect;
12
13
/**
14
 * @see \Hyde\Framework\Testing\Feature\NavigationMenuTest
15
 */
16
abstract class BaseNavigationMenu
17
{
18
    /** @var \Illuminate\Support\Collection<\Hyde\Framework\Features\Navigation\NavItem> */
19
    public Collection $items;
20
21
    final protected function __construct()
22
    {
23
        $this->items = new Collection();
24
    }
25
26
    public static function create(): static
27
    {
28
        $menu = new static();
29
30
        $menu->generate();
31
        $menu->removeDuplicateItems();
32
        $menu->sortByPriority();
33
34
        return $menu;
35
    }
36
37
    protected function generate(): void
38
    {
39
        Routes::each(function (Route $route): void {
0 ignored issues
show
Bug introduced by
The method each() does not exist on Hyde\Foundation\Facades\Routes. 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

39
        Routes::/** @scrutinizer ignore-call */ 
40
                each(function (Route $route): void {
Loading history...
40
            if ($this->canAddRoute($route)) {
41
                $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

41
                $this->items->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), NavItem::fromRoute($route));
Loading history...
42
            }
43
        });
44
45
        collect(Config::getArray('hyde.navigation.custom', []))->each(function (NavItem $item): void {
0 ignored issues
show
Bug introduced by
Hyde\Facades\Config::get...ation.custom', array()) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

45
        collect(/** @scrutinizer ignore-type */ Config::getArray('hyde.navigation.custom', []))->each(function (NavItem $item): void {
Loading history...
46
            // Since these were added explicitly by the user, we can assume they should always be shown
47
            $this->items->push($item);
48
        });
49
    }
50
51
    protected function canAddRoute(Route $route): bool
52
    {
53
        return $route->getPage()->showInNavigation();
54
    }
55
56
    protected function removeDuplicateItems(): void
57
    {
58
        $this->items = $this->items->unique(function (NavItem $item): string {
59
            // Filter using a combination of the group and label to allow duplicate labels in different groups
60
            return $item->getGroup().$item->label;
61
        });
62
    }
63
64
    protected function sortByPriority(): void
65
    {
66
        $this->items = $this->items->sortBy('priority')->values();
67
    }
68
}
69