GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#17)
by Roelof Jan
10:43
created

Model   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 338
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 68
dl 0
loc 338
ccs 88
cts 88
cp 1
rs 10
c 3
b 0
f 0
wmc 30

25 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 7 1
A exists() 0 3 1
A matter() 0 3 1
A getFilePath() 0 9 2
A rawContent() 0 9 2
A body() 0 5 1
A setFileName() 0 7 1
A assertFilenameExists() 0 4 2
A getFileMatchFromDisk() 0 4 1
A addMatter() 0 5 1
A getFolderPath() 0 7 1
A extension() 0 3 1
A setMatter() 0 5 1
A rename() 0 11 1
A parseFile() 0 6 1
A assertRequiredMatterIsPresent() 0 5 3
A all() 0 9 1
A rawBody() 0 3 1
A delete() 0 3 1
A setExtension() 0 5 1
A __get() 0 3 1
A save() 0 13 1
A getModelFiles() 0 3 1
A setBody() 0 5 1
A filename() 0 3 1
1
<?php
2
3
4
namespace AloiaCms\Models;
5
6
use ContentParser\ContentParser;
7
use AloiaCms\InlineBlockParser;
8
use AloiaCms\Models\Contracts\ModelInterface;
9
use AloiaCms\Models\Contracts\StorableInterface;
10
use AloiaCms\Writer\FolderCreator;
11
use AloiaCms\Writer\FrontMatterCreator;
12
use Illuminate\Support\Arr;
13
use Illuminate\Support\Collection;
14
use Illuminate\Support\Facades\Config;
15
use Illuminate\Support\Facades\File;
16
use Spatie\YamlFrontMatter\YamlFrontMatter;
17
18
class Model implements ModelInterface, StorableInterface
19
{
20
    protected $folder = '';
21
22
    protected $file_name = null;
23
24
    protected $extension = 'md';
25
26
    protected $matter = [];
27
28
    protected $body = '';
29
30
    protected $required_fields = [];
31
32
    /**
33
     * Find a single model
34
     *
35
     * @param string $file_name
36
     * @return ModelInterface
37
     */
38 59
    public static function find(string $file_name): ModelInterface
39
    {
40 59
        $instance = new static();
41
42 59
        $instance->setFileName($file_name);
43
44 59
        return $instance;
45
    }
46
47
    /**
48
     * Return all instances of the model
49
     *
50
     * @return Collection|ModelInterface[]
51
     */
52 7
    public static function all(): Collection
53
    {
54 7
        $instance = new static();
55
56 7
        $files = File::allFiles($instance->getFolderPath());
57
58 7
        return Collection::make($files)
59
            ->map(function (\SplFileInfo $fileInfo): ModelInterface {
60 7
                return self::find(pathinfo($fileInfo->getFilename(), PATHINFO_FILENAME));
61 7
            });
62
    }
63
64
    /**
65
     * Rename this file to the given name
66
     *
67
     * @param string $new_name
68
     * @return Model
69
     */
70 1
    public function rename(string $new_name): ModelInterface
71
    {
72 1
        $old_file_path = $this->getFilePath();
73
74 1
        $this->file_name = $new_name;
75
76 1
        $new_file_path = $this->getFilePath();
77
78 1
        File::move($old_file_path, $new_file_path);
79
80 1
        return self::find($new_name);
81
    }
82
83
    /**
84
     * Get the raw content of the file + front matter
85
     *
86
     * @return string
87
     */
88 59
    public function rawContent(): string
89
    {
90 59
        $file_path = $this->getFilePath();
91
92 59
        if ($this->exists()) {
93 47
            return file_get_contents($file_path);
94
        }
95
96 58
        return "";
97
    }
98
99
    /**
100
     * Parse the file for this model into model variables
101
     */
102 59
    private function parseFile(): void
103
    {
104 59
        $parsed_file = YamlFrontMatter::parse($this->rawContent());
105
106 59
        $this->matter = $parsed_file->matter();
107 59
        $this->body = $parsed_file->body();
108 59
    }
109
110
    /**
111
     * Save this instance to file
112
     *
113
     * @return ModelInterface
114
     * @throws \Exception
115
     */
116 52
    public function save(): ModelInterface
117
    {
118 52
        $file_content = FrontMatterCreator::seed($this->matter, $this->body)->create();
119
120 52
        $this->assertFilenameExists();
121
122 51
        $this->assertRequiredMatterIsPresent();
123
124 49
        $file_path = $this->getFilePath();
125
126 49
        file_put_contents($file_path, $file_content);
127
128 49
        return $this;
129
    }
130
131
    /**
132
     * Get the file path for this instance
133
     *
134
     * @return string
135
     */
136 59
    private function getFilePath(): string
137
    {
138 59
        $folder_path = $this->getFolderPath();
139
140 59
        if (!is_null($matching_filepath = $this->getFileMatchFromDisk())) {
141 47
            $this->setExtension(pathinfo($matching_filepath, PATHINFO_EXTENSION));
142
        }
143
144 59
        return "{$folder_path}/{$this->file_name}.{$this->extension}";
145
    }
146
147
    /**
148
     * Get the folder path for this model
149
     *
150
     * @return string
151
     */
152 62
    public function getFolderPath(): string
153
    {
154 62
        $folder_path = Config::get('aloiacms.collections_path') . "/{$this->folder}";
155
156 62
        FolderCreator::forPath($folder_path);
157
158 62
        return $folder_path;
159
    }
160
161
    /**
162
     * Get the front matter
163
     *
164
     * @return array
165
     */
166 2
    public function matter(): array
167
    {
168 2
        return $this->matter;
169
    }
170
171
    /**
172
     * Add data to the front matter
173
     *
174
     * @param string $key
175
     * @param mixed $value
176
     * @return ModelInterface
177
     */
178 1
    public function addMatter(string $key, $value): ModelInterface
179
    {
180 1
        $this->matter[$key] = $value;
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * Set the front matter
187
     *
188
     * @param array $matter
189
     * @return ModelInterface
190
     */
191 44
    public function setMatter(array $matter): ModelInterface
192
    {
193 44
        $this->matter = $matter;
194
195 44
        return $this;
196
    }
197
198
    /**
199
     * Get the raw file body
200
     *
201
     * @return string
202
     */
203 21
    public function rawBody(): string
204
    {
205 21
        return $this->body;
206
    }
207
208
    /**
209
     * Get the parse file body
210
     *
211
     * @return string
212
     */
213 19
    public function body(): string
214
    {
215 19
        $content = new ContentParser($this->rawBody(), $this->extension());
216
217 19
        return (new InlineBlockParser)->parseHtmlString($content->parse());
218
    }
219
220
    /**
221
     * Set the file body
222
     *
223
     * @param string $body
224
     * @return ModelInterface
225
     */
226 38
    public function setBody(string $body): ModelInterface
227
    {
228 38
        $this->body = $body;
229
230 38
        return $this;
231
    }
232
233
    /**
234
     * Get the file extension
235
     *
236
     * @return string
237
     */
238 20
    public function extension(): string
239
    {
240 20
        return $this->extension;
241
    }
242
243
    /**
244
     * Set the file extension
245
     *
246
     * @param string $extension
247
     * @return ModelInterface
248
     */
249 47
    public function setExtension(string $extension): ModelInterface
250
    {
251 47
        $this->extension = $extension;
252
253 47
        return $this;
254
    }
255
256
    /**
257
     * Get the file name for this instance
258
     *
259
     * @return string
260
     */
261 1
    public function filename(): ?string
262
    {
263 1
        return $this->file_name;
264
    }
265
266
    /**
267
     * Set the file name for this instance
268
     *
269
     * @param string $file_name
270
     * @return ModelInterface
271
     */
272 59
    protected function setFileName(string $file_name): ModelInterface
273
    {
274 59
        $this->file_name = $file_name;
275
276 59
        $this->parseFile();
277
278 59
        return $this;
279
    }
280
281
    /**
282
     * Get all models for this type
283
     *
284
     * @return array
285
     */
286 59
    private function getModelFiles(): array
287
    {
288 59
        return File::allFiles($this->getFolderPath());
289
    }
290
291
    /**
292
     * Get the filename from disk
293
     *
294
     * @return string|null
295
     */
296 59
    private function getFileMatchFromDisk(): ?string
297
    {
298
        return Arr::first($this->getModelFiles(), function (string $file_name) {
299 47
            return strpos($file_name, "/{$this->file_name}.");
300 59
        });
301
    }
302
303
    /**
304
     * Determine whether the current model exists
305
     *
306
     * @return bool
307
     */
308 59
    public function exists(): bool
309
    {
310 59
        return !is_null($this->getFileMatchFromDisk());
311
    }
312
313
    /**
314
     * Delete the current model
315
     *
316
     * @return bool
317
     */
318 2
    public function delete(): bool
319
    {
320 2
        return File::delete($this->getFilePath());
321
    }
322
323
    /**
324
     * Get front matter information through an accessor
325
     *
326
     * @param $key
327
     * @return mixed|null
328
     */
329 12
    public function __get($key)
330
    {
331 12
        return $this->matter[$key] ?? null;
332
    }
333
334
    /**
335
     * Throw exception when the file name is not set for this instance
336
     *
337
     * @throws \Exception
338
     */
339 52
    private function assertFilenameExists()
340
    {
341 52
        if (is_null($this->file_name)) {
342 1
            throw new \Exception("Filename is required");
343
        }
344 51
    }
345
346
    /**
347
     * Throw exception if at least one required matter attribute is not present
348
     *
349
     * @throws \Exception
350
     */
351 51
    private function assertRequiredMatterIsPresent()
352
    {
353 51
        foreach ($this->required_fields as $required_field) {
354 43
            if (!isset($this->matter[$required_field])) {
355 43
                throw new \Exception("Attribute {$required_field} is required");
356
            }
357
        }
358 49
    }
359
}
360