Passed
Push — master ( a0607b...d46171 )
by Caen
03:11 queued 19s
created

SourceFileParser::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Concerns\BaseMarkdownPage;
6
use Hyde\Framework\Concerns\HydePage;
7
use Hyde\Framework\Concerns\ValidatesExistence;
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 $identifier;
26
    protected HydePage $page;
27
28
    public function __construct(string $pageClass, string $identifier)
29
    {
30
        $this->validateExistence($pageClass, $identifier);
31
        $this->identifier = $identifier;
32
33
        $this->page = $this->constructPage($pageClass);
0 ignored issues
show
Bug introduced by
$pageClass of type Hyde\Framework\Concerns\HydePage is incompatible with the type string expected by parameter $pageClass of Hyde\Framework\Actions\S...Parser::constructPage(). ( 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->constructPage(/** @scrutinizer ignore-type */ $pageClass);
Loading history...
34
    }
35
36
    protected function parseBladePage(): BladePage
37
    {
38
        return new BladePage(
39
            $this->identifier,
40
            BladeMatterParser::parseFile(BladePage::sourcePath($this->identifier))
41
        );
42
    }
43
44
    protected function parseMarkdownPage(string $pageClass): BaseMarkdownPage
45
    {
46
        /** @var \Hyde\Framework\Concerns\BaseMarkdownPage $pageClass */
47
        $document = MarkdownFileParser::parse(
48
            $pageClass::sourcePath($this->identifier)
49
        );
50
51
        return new $pageClass(
52
            identifier: $this->identifier,
53
            matter: $document->matter,
54
            markdown: $document->markdown
55
        );
56
    }
57
58
    public function get(): HydePage
59
    {
60
        return $this->page;
61
    }
62
63
    protected function constructPage(string $pageClass): BladePage|BaseMarkdownPage
64
    {
65
        if ($pageClass === BladePage::class) {
66
            return $this->parseBladePage();
67
        }
68
69
        return $this->parseMarkdownPage($pageClass);
70
    }
71
}
72