Passed
Push — master ( 421bfc...8e1cd9 )
by Caen
03:38 queued 13s
created

SourceFileParser::constructDynamicData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Concerns\ValidatesExistence;
6
use Hyde\Framework\Contracts\AbstractMarkdownPage;
7
use Hyde\Framework\Contracts\PageContract;
8
use Hyde\Framework\Models\Pages\BladePage;
9
use Hyde\Framework\Modules\Markdown\MarkdownFileParser;
10
11
/**
12
 * Parses a source file and returns a new page model instance for it.
13
 *
14
 * Page Parsers are responsible for parsing a source file into a Page object,
15
 * and may also conduct pre-processing and/or data validation/assembly.
16
 *
17
 * Note that the Page Parsers do not compile any HTML or Markdown.
18
 *
19
 * @see \Hyde\Framework\Testing\Feature\SourceFileParserTest
20
 */
21
class SourceFileParser
22
{
23
    use ValidatesExistence;
24
25
    protected string $slug;
26
    protected PageContract $page;
27
28
    public function __construct(string $pageClass, string $slug)
29
    {
30
        $this->validateExistence($pageClass, $slug);
31
        $this->slug = $slug;
32
33
        $this->page = $this->constructBaseModel($pageClass);
0 ignored issues
show
Bug introduced by
$pageClass of type Hyde\Framework\Contracts\AbstractPage is incompatible with the type string expected by parameter $pageClass of Hyde\Framework\Actions\S...r::constructBaseModel(). ( Ignorable by Annotation )

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

33
        $this->page = $this->constructBaseModel(/** @scrutinizer ignore-type */ $pageClass);
Loading history...
34
        $this->page = PageModelConstructor::run($this->page);
35
    }
36
37
    protected function constructBaseModel(string $pageClass): BladePage|AbstractMarkdownPage
38
    {
39
        return $pageClass === BladePage::class
40
            ? $this->parseBladePage()
41
            : $this->parseMarkdownPage($pageClass);
42
    }
43
44
    protected function parseBladePage(): BladePage
45
    {
46
        return new BladePage($this->slug);
47
    }
48
49
    protected function parseMarkdownPage(string $pageClass): AbstractMarkdownPage
50
    {
51
        /** @var AbstractMarkdownPage $pageClass */
52
        $document = MarkdownFileParser::parse(
53
            $pageClass::qualifyBasename($this->slug)
54
        );
55
56
        $matter = $document->matter;
57
        $markdown = $document->markdown;
58
59
        return new $pageClass(
60
            identifier: $this->slug,
61
            matter: $matter,
62
            markdown: $markdown
63
        );
64
    }
65
66
    public function get(): PageContract
67
    {
68
        return $this->page;
69
    }
70
}
71