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
Push — master ( f717a4...f2d01f )
by Roelof Jan
01:23 queued 11s
created

Model::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 63
    public static function find(string $file_name): ModelInterface
39
    {
40 63
        $instance = new static();
41
42 63
        $instance->setFileName($file_name);
43
44 63
        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 7
            ->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 63
    public function rawContent(): string
89
    {
90 63
        $file_path = $this->getFilePath();
91
92 63
        if ($this->exists()) {
93 47
            return file_get_contents($file_path);
94
        }
95
96 62
        return "";
97
    }
98
99
    /**
100
     * Parse the file for this model into model variables
101
     */
102 63
    private function parseFile(): void
103
    {
104 63
        $parsed_file = YamlFrontMatter::parse($this->rawContent());
105
106 63
        $this->matter = $parsed_file->matter();
107 63
        $this->body = $parsed_file->body();
108 63
    }
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 63
    private function getFilePath(): string
137
    {
138 63
        $folder_path = $this->getFolderPath();
139
140 63
        if (!is_null($matching_filepath = $this->getFileMatchFromDisk())) {
141 47
            $this->setExtension(pathinfo($matching_filepath, PATHINFO_EXTENSION));
142
        }
143
144 63
        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 66
    public function getFolderPath(): string
153
    {
154 66
        $folder_path = Config::get('aloiacms.collections_path') . "/{$this->folder}";
155
156 66
        FolderCreator::forPath($folder_path);
157
158 66
        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
     * Set a value on the specified key in the configuration
173
     *
174
     * Kept around for backward compatibility
175
     *
176
     * @param string $key
177
     * @param $value
178
     * @return ModelInterface
179
     *
180
     * @deprecated since 3.2.0
181
     */
182 1
    public function addMatter(string $key, $value): ModelInterface
183
    {
184 1
        return $this->set($key, $value);
185
    }
186
    
187
    /**
188
     * Set data in the front matter, but only for the keys specified in the input array
189
     *
190
     * @param array $matter
191
     * @return ModelInterface
192
     */
193 46
    public function setMatter(array $matter): ModelInterface
194
    {
195 46
        foreach (array_keys($matter) as $key) {
196 46
            $this->matter[$key] = $matter[$key];
197
        }
198
199 46
        return $this;
200
    }
201
202
    /**
203
     * Determine whether a key is present in the configuration
204
     *
205
     * @param string $key
206
     * @return bool
207
     */
208 2
    public function has(string $key): bool
209
    {
210 2
        return isset($this->matter[$key]);
211
    }
212
213
    /**
214
     * Get the value of the specified key, return null if it doesn't exist
215
     *
216
     * @param string $key
217
     * @return mixed|null
218
     */
219 15
    public function get(string $key)
220
    {
221 15
        return $this->matter[$key] ?? null;
222
    }
223
224
    /**
225
     * Set a value on the specified key in the configuration
226
     *
227
     * @param string $key
228
     * @param $value
229
     * @return $this|ModelInterface
230
     */
231 3
    public function set(string $key, $value): ModelInterface
232
    {
233 3
        $this->matter[$key] = $value;
234
235 3
        return $this;
236
    }
237
238
    /**
239
     * Remove a key from the configuration
240
     *
241
     * @param string $key
242
     * @return $this|ModelInterface
243
     */
244 2
    public function remove(string $key): ModelInterface
245
    {
246 2
        if ($this->has($key)) {
247 2
            unset($this->matter[$key]);
248
        }
249
250 2
        return $this;
251
    }
252
253
    /**
254
     * Get the raw file body
255
     *
256
     * @return string
257
     */
258 21
    public function rawBody(): string
259
    {
260 21
        return $this->body;
261
    }
262
263
    /**
264
     * Get the parse file body
265
     *
266
     * @return string
267
     */
268 19
    public function body(): string
269
    {
270 19
        $content = new ContentParser($this->rawBody(), $this->extension());
271
272 19
        return (new InlineBlockParser)->parseHtmlString($content->parse());
273
    }
274
275
    /**
276
     * Set the file body
277
     *
278
     * @param string $body
279
     * @return ModelInterface
280
     */
281 38
    public function setBody(string $body): ModelInterface
282
    {
283 38
        $this->body = $body;
284
285 38
        return $this;
286
    }
287
288
    /**
289
     * Get the file extension
290
     *
291
     * @return string
292
     */
293 20
    public function extension(): string
294
    {
295 20
        return $this->extension;
296
    }
297
298
    /**
299
     * Set the file extension
300
     *
301
     * @param string $extension
302
     * @return ModelInterface
303
     */
304 47
    public function setExtension(string $extension): ModelInterface
305
    {
306 47
        $this->extension = $extension;
307
308 47
        return $this;
309
    }
310
311
    /**
312
     * Get the file name for this instance
313
     *
314
     * @return string
315
     */
316 1
    public function filename(): ?string
317
    {
318 1
        return $this->file_name;
319
    }
320
321
    /**
322
     * Set the file name for this instance
323
     *
324
     * @param string $file_name
325
     * @return ModelInterface
326
     */
327 63
    protected function setFileName(string $file_name): ModelInterface
328
    {
329 63
        $this->file_name = $file_name;
330
331 63
        $this->parseFile();
332
333 63
        return $this;
334
    }
335
336
    /**
337
     * Get all models for this type
338
     *
339
     * @return array
340
     */
341 63
    private function getModelFiles(): array
342
    {
343 63
        return File::allFiles($this->getFolderPath());
344
    }
345
346
    /**
347
     * Get the filename from disk
348
     *
349
     * @return string|null
350
     */
351 63
    private function getFileMatchFromDisk(): ?string
352
    {
353 63
        return Arr::first($this->getModelFiles(), function (string $file_name) {
354 47
            return strpos($file_name, "/{$this->file_name}.");
355 63
        });
356
    }
357
358
    /**
359
     * Determine whether the current model exists
360
     *
361
     * @return bool
362
     */
363 63
    public function exists(): bool
364
    {
365 63
        return !is_null($this->getFileMatchFromDisk());
366
    }
367
368
    /**
369
     * Delete the current model
370
     *
371
     * @return bool
372
     */
373 2
    public function delete(): bool
374
    {
375 2
        return File::delete($this->getFilePath());
376
    }
377
378
    /**
379
     * Get front matter information through an accessor
380
     *
381
     * @param $key
382
     * @return mixed|null
383
     */
384 12
    public function __get($key)
385
    {
386 12
        return $this->get($key);
387
    }
388
389
    /**
390
     * Throw exception when the file name is not set for this instance
391
     *
392
     * @throws \Exception
393
     */
394 52
    private function assertFilenameExists()
395
    {
396 52
        if (is_null($this->file_name)) {
397 1
            throw new \Exception("Filename is required");
398
        }
399 51
    }
400
401
    /**
402
     * Throw exception if at least one required matter attribute is not present
403
     *
404
     * @throws \Exception
405
     */
406 51
    private function assertRequiredMatterIsPresent()
407
    {
408 51
        foreach ($this->required_fields as $required_field) {
409 43
            if (!isset($this->matter[$required_field])) {
410 43
                throw new \Exception("Attribute {$required_field} is required");
411
            }
412
        }
413 49
    }
414
}
415