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

NavItem   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 112
rs 10
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroup() 0 3 1
A setDestination() 0 5 1
A __construct() 0 9 2
A __toString() 0 3 1
A setPriority() 0 5 1
A toRoute() 0 3 1
A toLink() 0 3 1
A normalizeGroupKey() 0 3 2
A fromRoute() 0 7 1
A isCurrent() 0 12 4
A resolveLink() 0 3 1
1
<?php
2
3
namespace Hyde\Framework\Models\Navigation;
4
5
use Hyde\Framework\Contracts\PageContract;
6
use Hyde\Framework\Contracts\RouteContract;
7
use Hyde\Framework\Hyde;
8
use Illuminate\Support\Str;
9
10
/**
11
 * Abstraction for a navigation menu item.
12
 *
13
 * You have a few options to construct a navigation menu item:
14
 *   1. You can supply a Route directly and explicit properties to the constructor
15
 *   2. You can use NavItem::fromRoute() to use data from the route
16
 *   3. You can use NavItem::leadsTo(URI, Title, ?priority) for an external or un-routed link
17
 */
18
class NavItem implements \Stringable
19
{
20
    public RouteContract $route;
21
    public string $href;
22
23
    public string $title;
24
    public int $priority;
25
    public bool $hidden;
26
27
    /**
28
     * Create a new navigation menu item.
29
     *
30
     * @param  \Hyde\Framework\Contracts\RouteContract|null  $route
31
     * @param  string  $title
32
     * @param  int  $priority
33
     * @param  bool  $hidden
34
     */
35
    public function __construct(?RouteContract $route, string $title, int $priority = 500, bool $hidden = false)
36
    {
37
        if ($route !== null) {
38
            $this->route = $route;
39
        }
40
41
        $this->title = $title;
42
        $this->priority = $priority;
43
        $this->hidden = $hidden;
44
    }
45
46
    /**
47
     * Create a new navigation menu item from a route.
48
     */
49
    public static function fromRoute(RouteContract $route): static
50
    {
51
        return new self(
52
            $route,
53
            $route->getSourceModel()->navigationMenuTitle(),
0 ignored issues
show
Bug introduced by
The method navigationMenuTitle() does not exist on Hyde\Framework\Contracts\PageContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\PageContract. ( Ignorable by Annotation )

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

53
            $route->getSourceModel()->/** @scrutinizer ignore-call */ navigationMenuTitle(),
Loading history...
54
            $route->getSourceModel()->navigationMenuPriority(),
0 ignored issues
show
Bug introduced by
The method navigationMenuPriority() does not exist on Hyde\Framework\Contracts\PageContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\PageContract. ( Ignorable by Annotation )

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

54
            $route->getSourceModel()->/** @scrutinizer ignore-call */ navigationMenuPriority(),
Loading history...
55
            ! $route->getSourceModel()->showInNavigation()
0 ignored issues
show
Bug introduced by
The method showInNavigation() does not exist on Hyde\Framework\Contracts\PageContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\PageContract. ( Ignorable by Annotation )

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

55
            ! $route->getSourceModel()->/** @scrutinizer ignore-call */ showInNavigation()
Loading history...
56
        );
57
    }
58
59
    /**
60
     * Create a new navigation menu item leading to an external URI.
61
     */
62
    public static function toLink(string $href, string $title, int $priority = 500): static
63
    {
64
        return (new self(null, $title, $priority, false))->setDestination($href);
65
    }
66
67
    /**
68
     * Create a new navigation menu item leading to a Route model.
69
     */
70
    public static function toRoute(RouteContract $route, string $title, int $priority = 500): static
71
    {
72
        return new self($route, $title, $priority, false);
73
    }
74
75
    /**
76
     * Resolve a link to the navigation item.
77
     */
78
    public function resolveLink(): string
79
    {
80
        return $this->href ?? $this->route->getLink();
81
    }
82
83
    /**
84
     * Resolve a link to the navigation item.
85
     */
86
    public function __toString(): string
87
    {
88
        return $this->resolveLink();
89
    }
90
91
    /**
92
     * Check if the NavItem instance is the current page.
93
     */
94
    public function isCurrent(?PageContract $current = null): bool
95
    {
96
        if ($current === null) {
97
            $current = Hyde::currentRoute()->getSourceModel();
98
        }
99
100
        if (! isset($this->route)) {
101
            return ($current->getRoute()->getRouteKey() === $this->href)
102
            || ($current->getRoute()->getRouteKey().'.html' === $this->href);
103
        }
104
105
        return $current->getRoute()->getRouteKey() === $this->route->getRouteKey();
106
    }
107
108
    protected function setDestination(string $href): static
109
    {
110
        $this->href = $href;
111
112
        return $this;
113
    }
114
115
    public function setPriority(int $priority): static
116
    {
117
        $this->priority = $priority;
118
119
        return $this;
120
    }
121
122
    public function getGroup(): ?string
123
    {
124
        return $this->normalizeGroupKey($this->route->getSourceModel()->get('category'));
125
    }
126
127
    protected function normalizeGroupKey(?string $group): ?string
128
    {
129
        return empty($group) ? null : Str::slug($group);
130
    }
131
}
132