Passed
Push — master ( 1e39e8...4523d8 )
by Caen
03:01 queued 12s
created

SourceFileParser::parseBladeMatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\FrontMatter;
9
use Hyde\Framework\Models\Pages\BladePage;
10
use Hyde\Framework\Modules\Markdown\MarkdownFileParser;
11
12
/**
13
 * Parses a source file and returns a new page model instance for it.
14
 *
15
 * Page Parsers are responsible for parsing a source file into a Page object,
16
 * and may also conduct pre-processing and/or data validation/assembly.
17
 *
18
 * Note that the Page Parsers do not compile any HTML or Markdown.
19
 *
20
 * @see \Hyde\Framework\Testing\Feature\SourceFileParserTest
21
 */
22
class SourceFileParser
23
{
24
    use ValidatesExistence;
25
26
    protected string $slug;
27
    protected PageContract $page;
28
29
    public function __construct(string $pageClass, string $slug)
30
    {
31
        $this->validateExistence($pageClass, $slug);
32
        $this->slug = $slug;
33
34
        $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

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