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

Route::getFromKey()   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
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Concerns\JsonSerializesArrayable;
6
use Hyde\Framework\Contracts\PageContract;
7
use Hyde\Framework\Contracts\RouteContract;
8
use Hyde\Framework\Contracts\RouteFacadeContract;
9
use Hyde\Framework\Exceptions\RouteNotFoundException;
10
use Hyde\Framework\Hyde;
11
use Hyde\Framework\RouteCollection;
12
use Illuminate\Contracts\Support\Arrayable;
13
14
/**
15
 * @see \Hyde\Framework\Testing\Feature\RouteTest
16
 */
17
class Route implements RouteContract, RouteFacadeContract, \Stringable, \JsonSerializable, Arrayable
18
{
19
    use JsonSerializesArrayable;
20
21
    /**
22
     * The source model for the route.
23
     *
24
     * @var \Hyde\Framework\Contracts\PageContract
25
     */
26
    protected PageContract $sourceModel;
27
28
    /**
29
     * The unique route key for the route.
30
     *
31
     * @var string The route key. Generally <output-directory/slug>.
32
     */
33
    protected string $routeKey;
34
35
    /** @inheritDoc */
36
    public function __construct(PageContract $sourceModel)
37
    {
38
        $this->sourceModel = $sourceModel;
39
        $this->routeKey = $sourceModel->getRouteKey();
40
    }
41
42
    /** @inheritDoc */
43
    public function __toString(): string
44
    {
45
        return $this->getLink();
46
    }
47
48
    /** @inheritDoc */
49
    public function toArray(): array
50
    {
51
        return [
0 ignored issues
show
introduced by
The expression return array('routeKey' ...is->sourceModel::class) returns an array which contains values of type string which are incompatible with the return type Illuminate\Contracts\Support\TValue mandated by Illuminate\Contracts\Support\Arrayable::toArray().
Loading history...
52
            'routeKey' => $this->routeKey,
53
            'sourceModelPath' => $this->sourceModel->getSourcePath(),
54
            'sourceModelType' => $this->sourceModel::class,
55
        ];
56
    }
57
58
    /** @inheritDoc */
59
    public function getLink(): string
60
    {
61
        return Hyde::relativeLink($this->getOutputFilePath());
62
    }
63
64
    /** @inheritDoc */
65
    public function getPageType(): string
66
    {
67
        return $this->sourceModel::class;
68
    }
69
70
    /** @inheritDoc */
71
    public function getSourceModel(): PageContract
72
    {
73
        return $this->sourceModel;
74
    }
75
76
    /** @inheritDoc */
77
    public function getRouteKey(): string
78
    {
79
        return $this->routeKey;
80
    }
81
82
    /** @inheritDoc */
83
    public function getSourceFilePath(): string
84
    {
85
        return $this->sourceModel->getSourcePath();
86
    }
87
88
    /** @inheritDoc */
89
    public function getOutputFilePath(): string
90
    {
91
        return $this->sourceModel->getOutputPath();
92
    }
93
94
    /** @inheritDoc */
95
    public function getQualifiedUrl(): string
96
    {
97
        return Hyde::url($this->getOutputFilePath());
98
    }
99
100
    /** @inheritDoc */
101
    public static function get(string $routeKey): static
102
    {
103
        return static::getFromKey($routeKey);
104
    }
105
106
    /** @inheritDoc */
107
    public static function getFromKey(string $routeKey): static
108
    {
109
        return Hyde::routes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
110
    }
111
112
    /** @inheritDoc */
113
    public static function getFromSource(string $sourceFilePath): static
114
    {
115
        return Hyde::routes()->first(function (RouteContract $route) use ($sourceFilePath) {
116
            return $route->getSourceFilePath() === $sourceFilePath;
117
        }) ?? throw new RouteNotFoundException($sourceFilePath);
118
    }
119
120
    /** @inheritDoc */
121
    public static function getFromModel(PageContract $page): RouteContract
122
    {
123
        return $page->getRoute();
124
    }
125
126
    /** @inheritDoc */
127
    public static function all(): RouteCollection
128
    {
129
        return Hyde::routes();
130
    }
131
132
    /** @inheritDoc */
133
    public static function current(): RouteContract
134
    {
135
        return Hyde::currentRoute() ?? throw new RouteNotFoundException('current');
136
    }
137
138
    /** @inheritDoc */
139
    public static function home(): RouteContract
140
    {
141
        return static::getFromKey('index');
142
    }
143
144
    /** @inheritDoc */
145
    public static function exists(string $routeKey): bool
146
    {
147
        return Hyde::routes()->has($routeKey);
148
    }
149
}
150