Passed
Branch master (4048f5)
by Caen
03:01
created

Route::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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\Services\RoutingService;
12
use Illuminate\Contracts\Support\Arrayable;
13
use Illuminate\Support\Collection;
14
15
/**
16
 * @see \Hyde\Framework\Testing\Feature\RouteTest
17
 */
18
class Route implements RouteContract, RouteFacadeContract, \Stringable, \JsonSerializable, Arrayable
19
{
20
    use JsonSerializesArrayable;
21
22
    /**
23
     * The source model for the route.
24
     *
25
     * @var \Hyde\Framework\Contracts\PageContract
26
     */
27
    protected PageContract $sourceModel;
28
29
    /**
30
     * The unique route key for the route.
31
     *
32
     * @var string The route key. Generally <output-directory/slug>.
33
     */
34
    protected string $routeKey;
35
36
    /** @inheritDoc */
37
    public function __construct(PageContract $sourceModel)
38
    {
39
        $this->sourceModel = $sourceModel;
40
        $this->routeKey = $this->constructRouteKey();
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Framework\Models\Route::constructRouteKey() has been deprecated: Use the route key property ( Ignorable by Annotation )

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

40
        $this->routeKey = /** @scrutinizer ignore-deprecated */ $this->constructRouteKey();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
41
    }
42
43
    /** @inheritDoc */
44
    public function toArray()
45
    {
46
        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...
47
            'routeKey' => $this->routeKey,
48
            'sourceModelPath' => $this->sourceModel->getSourcePath(),
49
            'sourceModelType' => $this->sourceModel::class,
50
        ];
51
    }
52
53
    /** @inheritDoc */
54
    public function getPageType(): string
55
    {
56
        return $this->sourceModel::class;
57
    }
58
59
    /** @inheritDoc */
60
    public function getSourceModel(): PageContract
61
    {
62
        return $this->sourceModel;
63
    }
64
65
    /** @inheritDoc */
66
    public function getRouteKey(): string
67
    {
68
        return $this->routeKey;
69
    }
70
71
    /** @inheritDoc */
72
    public function getSourceFilePath(): string
73
    {
74
        return $this->sourceModel->getSourcePath();
75
    }
76
77
    /** @inheritDoc */
78
    public function getOutputFilePath(): string
79
    {
80
        return $this->sourceModel->getOutputPath();
81
    }
82
83
    /** @inheritDoc */
84
    public function getQualifiedUrl(): string
85
    {
86
        return Hyde::url($this->getOutputFilePath());
87
    }
88
89
    /** @inheritDoc */
90
    public function getLink(): string
91
    {
92
        return Hyde::relativeLink($this->getOutputFilePath());
93
    }
94
95
    /** @inheritDoc */
96
    public function __toString(): string
97
    {
98
        return $this->getLink();
99
    }
100
101
    /** @deprecated Use the route key property */
102
    protected function constructRouteKey(): string
103
    {
104
        return $this->sourceModel->getCurrentPagePath();
105
    }
106
107
    /** @inheritDoc */
108
    public static function get(string $routeKey): static
109
    {
110
        return static::getFromKey($routeKey);
111
    }
112
113
    /** @inheritDoc */
114
    public static function getFromKey(string $routeKey): static
115
    {
116
        return RoutingService::getInstance()->getRoutes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Framework\Services\...gService::getInstance() has been deprecated. ( Ignorable by Annotation )

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

116
        return /** @scrutinizer ignore-deprecated */ RoutingService::getInstance()->getRoutes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
Loading history...
117
    }
118
119
    /** @inheritDoc */
120
    public static function getFromSource(string $sourceFilePath): static
121
    {
122
        return RoutingService::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Framework\Services\...gService::getInstance() has been deprecated. ( Ignorable by Annotation )

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

122
        return /** @scrutinizer ignore-deprecated */ RoutingService::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
Loading history...
123
            return $route->getSourceFilePath() === $sourceFilePath;
124
        }) ?? throw new RouteNotFoundException($sourceFilePath);
125
    }
126
127
    /** @inheritDoc */
128
    public static function getFromModel(PageContract $page): RouteContract
129
    {
130
        return $page->getRoute();
131
    }
132
133
    /** @inheritDoc */
134
    public static function all(): Collection
135
    {
136
        return RoutingService::getInstance()->getRoutes();
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Framework\Services\...gService::getInstance() has been deprecated. ( Ignorable by Annotation )

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

136
        return /** @scrutinizer ignore-deprecated */ RoutingService::getInstance()->getRoutes();
Loading history...
137
    }
138
139
    /** @inheritDoc */
140
    public static function current(): RouteContract
141
    {
142
        return Hyde::currentRoute() ?? throw new RouteNotFoundException('current');
143
    }
144
145
    /** @inheritDoc */
146
    public static function home(): RouteContract
147
    {
148
        return static::getFromKey('index');
149
    }
150
151
    /** @todo add to contract */
152
    public static function exists(string $routeKey): bool
153
    {
154
        return RoutingService::getInstance()->getRoutes()->has($routeKey);
0 ignored issues
show
Deprecated Code introduced by
The function Hyde\Framework\Services\...gService::getInstance() has been deprecated. ( Ignorable by Annotation )

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

154
        return /** @scrutinizer ignore-deprecated */ RoutingService::getInstance()->getRoutes()->has($routeKey);
Loading history...
155
    }
156
}
157