Passed
Push — master ( dff5c5...cec494 )
by Caen
03:44 queued 11s
created

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