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

AbstractPage   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 13
Bugs 1 Features 0
Metric Value
eloc 46
c 13
b 1
f 0
dl 0
loc 177
rs 9.92
wmc 31

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutputPath() 0 3 1
A getFileExtension() 0 3 1
A getOutputLocation() 0 7 1
A getCurrentPagePath() 0 3 1
A getSourcePath() 0 3 1
A getOutputDirectory() 0 3 1
A qualifyBasename() 0 3 1
A constructPageSchemas() 0 3 1
A files() 0 3 1
A parse() 0 3 1
A getSourceDirectory() 0 3 1
A has() 0 7 3
A getIdentifier() 0 3 1
A all() 0 3 1
A __construct() 0 8 2
A get() 0 7 4
A matter() 0 3 1
A renderPageMetadata() 0 3 1
A navigationMenuTitle() 0 3 1
A htmlTitle() 0 3 1
A getRoute() 0 3 1
A navigationMenuPriority() 0 3 1
A showInNavigation() 0 3 1
A getBladeView() 0 3 1
A getRouteKey() 0 3 1
1
<?php
2
3
namespace Hyde\Framework\Contracts;
4
5
use Hyde\Framework\Actions\SourceFileParser;
6
use Hyde\Framework\Concerns\FrontMatter\Schemas\PageSchema;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\FrontMatter;
9
use Hyde\Framework\Models\Metadata\Metadata;
10
use Hyde\Framework\Models\Route;
11
use Hyde\Framework\PageCollection;
12
use Hyde\Framework\Services\DiscoveryService;
13
14
/**
15
 * To ensure compatibility with the Hyde Framework, all Page Models should extend this class.
16
 *
17
 * Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers.
18
 *
19
 * To learn about what the methods do, see the PHPDocs in the PageContract.
20
 *
21
 * @see \Hyde\Framework\Contracts\PageContract
22
 * @see \Hyde\Framework\Contracts\AbstractMarkdownPage
23
 * @see \Hyde\Framework\Testing\Feature\AbstractPageTest
24
 */
25
abstract class AbstractPage implements PageContract, CompilableContract
26
{
27
    use PageSchema;
28
29
    public static string $sourceDirectory;
30
    public static string $outputDirectory;
31
    public static string $fileExtension;
32
    public static string $template;
33
34
    public string $identifier;
35
    public string $routeKey;
36
37
    public FrontMatter $matter;
38
    public Metadata $metadata;
39
40
    public function __construct(string $identifier = '', FrontMatter|array $matter = [])
41
    {
42
        $this->identifier = $identifier;
43
        $this->routeKey = $this->getCurrentPagePath();
44
45
        $this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter);
0 ignored issues
show
introduced by
$matter is never a sub-type of Hyde\Framework\Models\FrontMatter.
Loading history...
46
        $this->constructPageSchemas();
47
        $this->metadata = new Metadata($this);
48
    }
49
50
    protected function constructPageSchemas(): void
51
    {
52
        $this->constructPageSchema();
53
    }
54
55
    /** @inheritDoc */
56
    final public static function getSourceDirectory(): string
57
    {
58
        return unslash(static::$sourceDirectory);
59
    }
60
61
    /** @inheritDoc */
62
    final public static function getOutputDirectory(): string
63
    {
64
        return unslash(static::$outputDirectory);
65
    }
66
67
    /** @inheritDoc */
68
    final public static function getFileExtension(): string
69
    {
70
        return '.'.ltrim(static::$fileExtension, '.');
71
    }
72
73
    /** @inheritDoc */
74
    public static function parse(string $slug): PageContract
75
    {
76
        return (new SourceFileParser(static::class, $slug))->get();
77
    }
78
79
    /** @inheritDoc */
80
    public static function files(): array|false
81
    {
82
        return DiscoveryService::getSourceFileListForModel(static::class);
83
    }
84
85
    /** @inheritDoc */
86
    public static function all(): PageCollection
87
    {
88
        return Hyde::pages()->getPages(static::class);
89
    }
90
91
    /** @inheritDoc */
92
    public static function qualifyBasename(string $basename): string
93
    {
94
        return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension();
95
    }
96
97
    /** @inheritDoc */
98
    public static function getOutputLocation(string $basename): string
99
    {
100
        // Using the trim function we ensure we don't have a leading slash when the output directory is the root directory.
101
        return trim(
102
            static::getOutputDirectory().'/'.unslash($basename),
103
            '/'
104
        ).'.html';
105
    }
106
107
    /** @inheritDoc */
108
    public function get(string $key = null, mixed $default = null): mixed
109
    {
110
        if ($key !== null && property_exists($this, $key) && isset($this->$key)) {
111
            return $this->$key;
112
        }
113
114
        return $this->matter($key, $default);
115
    }
116
117
    /** @inheritDoc */
118
    public function matter(string $key = null, mixed $default = null): mixed
119
    {
120
        return $this->matter->get($key, $default);
121
    }
122
123
    /** @inheritDoc */
124
    public function has(string $key, bool $strict = false): bool
125
    {
126
        if ($strict) {
127
            return property_exists($this, $key) || $this->matter->has($key);
128
        }
129
130
        return ! blank($this->get($key));
131
    }
132
133
    /** @inheritDoc */
134
    public function getIdentifier(): string
135
    {
136
        return $this->identifier;
137
    }
138
139
    /** @inheritDoc */
140
    public function getSourcePath(): string
141
    {
142
        return static::qualifyBasename($this->identifier);
143
    }
144
145
    /** @inheritDoc */
146
    public function getOutputPath(): string
147
    {
148
        return $this->getCurrentPagePath().'.html';
149
    }
150
151
    /** @inheritDoc */
152
    public function getCurrentPagePath(): string
153
    {
154
        return trim(static::getOutputDirectory().'/'.$this->identifier, '/');
155
    }
156
157
    /** @inheritDoc */
158
    public function getRouteKey(): string
159
    {
160
        return $this->routeKey;
161
    }
162
163
    /** @inheritDoc */
164
    public function getRoute(): Route
165
    {
166
        return new Route($this);
167
    }
168
169
    /** @inheritDoc */
170
    public function htmlTitle(): string
171
    {
172
        return config('site.name', 'HydePHP').' - '.$this->title;
173
    }
174
175
    /** @inheritDoc */
176
    public function getBladeView(): string
177
    {
178
        return static::$template;
179
    }
180
181
    /** @inheritDoc */
182
    abstract public function compile(): string;
183
184
    public function renderPageMetadata(): string
185
    {
186
        return $this->metadata->render();
187
    }
188
189
    public function showInNavigation(): bool
190
    {
191
        return ! $this->navigation['hidden'];
192
    }
193
194
    public function navigationMenuPriority(): int
195
    {
196
        return $this->navigation['priority'];
197
    }
198
199
    public function navigationMenuTitle(): string
200
    {
201
        return $this->navigation['title'];
202
    }
203
204
    /**
205
     * Not yet implemented.
206
     *
207
     * If an item returns a route collection,
208
     * it will automatically be made into a dropdown.
209
     *
210
     * @return \Illuminate\Support\Collection<\Hyde\Framework\Models\Route>
211
     */
212
    // public function navigationMenuChildren(): Collection;
213
}
214