Passed
Push — master ( 80ddec...cae974 )
by Caen
03:33 queued 12s
created

AbstractPage::navigationMenuPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Models\FrontMatter;
8
use Hyde\Framework\Models\Metadata\Metadata;
9
use Hyde\Framework\Models\Route;
10
use Hyde\Framework\Services\DiscoveryService;
11
use Illuminate\Support\Collection;
12
13
/**
14
 * To ensure compatibility with the Hyde Framework, all Page Models should extend this class.
15
 *
16
 * Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers.
17
 *
18
 * To learn about what the methods do, see the PHPDocs in the PageContract.
19
 *
20
 * @see \Hyde\Framework\Contracts\PageContract
21
 * @see \Hyde\Framework\Contracts\AbstractMarkdownPage
22
 * @see \Hyde\Framework\Testing\Feature\AbstractPageTest
23
 */
24
abstract class AbstractPage implements PageContract, CompilableContract
25
{
26
    use PageSchema;
27
28
    public static string $sourceDirectory;
29
    public static string $outputDirectory;
30
    public static string $fileExtension;
31
    public static string $template;
32
33
    public string $identifier;
34
    public FrontMatter $matter;
35
    public Metadata $metadata;
36
37
    public function __construct(string $identifier = '', FrontMatter|array $matter = [])
38
    {
39
        $this->identifier = $identifier;
40
        $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...
41
        $this->constructPageSchemas();
42
        $this->metadata = new Metadata($this);
43
    }
44
45
    protected function constructPageSchemas(): void
46
    {
47
        $this->constructPageSchema();
48
    }
49
50
    /** @inheritDoc */
51
    final public static function getSourceDirectory(): string
52
    {
53
        return unslash(static::$sourceDirectory);
54
    }
55
56
    /** @inheritDoc */
57
    final public static function getOutputDirectory(): string
58
    {
59
        return unslash(static::$outputDirectory);
60
    }
61
62
    /** @inheritDoc */
63
    final public static function getFileExtension(): string
64
    {
65
        return '.'.ltrim(static::$fileExtension, '.');
66
    }
67
68
    /** @inheritDoc */
69
    public static function parse(string $slug): PageContract
70
    {
71
        return (new SourceFileParser(static::class, $slug))->get();
72
    }
73
74
    /** @inheritDoc */
75
    public static function files(): array|false
76
    {
77
        return DiscoveryService::getSourceFileListForModel(static::class);
78
    }
79
80
    /** @inheritDoc */
81
    public static function all(): Collection
82
    {
83
        $collection = new Collection();
84
85
        foreach (static::files() as $basename) {
86
            $collection->push(static::parse($basename));
87
        }
88
89
        return $collection;
90
    }
91
92
    /** @inheritDoc */
93
    public static function qualifyBasename(string $basename): string
94
    {
95
        return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension();
96
    }
97
98
    /** @inheritDoc */
99
    public static function getOutputLocation(string $basename): string
100
    {
101
        // Using the trim function we ensure we don't have a leading slash when the output directory is the root directory.
102
        return trim(
103
            static::getOutputDirectory().'/'.unslash($basename),
104
            '/'
105
        ).'.html';
106
    }
107
108
    /** @inheritDoc */
109
    public function get(string $key = null, mixed $default = null): mixed
110
    {
111
        if (property_exists($this, $key) && isset($this->$key)) {
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type null; however, parameter $property of property_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

111
        if (property_exists($this, /** @scrutinizer ignore-type */ $key) && isset($this->$key)) {
Loading history...
112
            return $this->$key;
113
        }
114
115
        return $this->matter($key, $default);
116
    }
117
118
    /** @inheritDoc */
119
    public function matter(string $key = null, mixed $default = null): mixed
120
    {
121
        return $this->matter->get($key, $default);
122
    }
123
124
    /** @inheritDoc */
125
    public function has(string $key, bool $strict = false): bool
126
    {
127
        if ($strict) {
128
            return property_exists($this, $key) || $this->matter->has($key);
129
        }
130
131
        return ! blank($this->get($key));
132
    }
133
134
    /** @inheritDoc */
135
    public function getIdentifier(): string
136
    {
137
        return $this->identifier;
138
    }
139
140
    /** @inheritDoc */
141
    public function getSourcePath(): string
142
    {
143
        return static::qualifyBasename($this->identifier);
144
    }
145
146
    /** @inheritDoc */
147
    public function getOutputPath(): string
148
    {
149
        return static::getCurrentPagePath().'.html';
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\Framework\Contracts...e::getCurrentPagePath() is not static, but was called statically. ( Ignorable by Annotation )

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

149
        return static::/** @scrutinizer ignore-call */ getCurrentPagePath().'.html';
Loading history...
150
    }
151
152
    /** @inheritDoc */
153
    public function getCurrentPagePath(): string
154
    {
155
        return trim(static::getOutputDirectory().'/'.$this->identifier, '/');
156
    }
157
158
    /** @inheritDoc */
159
    public function getRoute(): Route
160
    {
161
        return new Route($this);
162
    }
163
164
    /** @inheritDoc */
165
    public function htmlTitle(): string
166
    {
167
        return config('site.name', 'HydePHP').' - '.$this->title;
168
    }
169
170
    /** @inheritDoc */
171
    public function getBladeView(): string
172
    {
173
        return static::$template;
174
    }
175
176
    /** @inheritDoc */
177
    abstract public function compile(): string;
178
179
    public function renderPageMetadata(): string
180
    {
181
        return $this->metadata->render();
182
    }
183
184
    public function showInNavigation(): bool
185
    {
186
        return ! $this->navigation['hidden'];
187
    }
188
189
    public function navigationMenuPriority(): int
190
    {
191
        return $this->navigation['priority'];
192
    }
193
194
    public function navigationMenuTitle(): string
195
    {
196
        return $this->navigation['title'];
197
    }
198
199
    /**
200
     * Not yet implemented.
201
     *
202
     * If an item returns a route collection,
203
     * it will automatically be made into a dropdown.
204
     *
205
     * @return \Illuminate\Support\Collection<\Hyde\Framework\Models\Route>
206
     */
207
    // public function navigationMenuChildren(): Collection;
208
}
209