Passed
Push — master ( 118b88...850f54 )
by Caen
03:01 queued 11s
created

SourceFileParser::get()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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 $identifier;
26
    protected PageContract $page;
27
28
    public function __construct(string $pageClass, string $identifier)
29
    {
30
        $this->validateExistence($pageClass, $identifier);
31
        $this->identifier = $identifier;
32
33
        $this->page = $pageClass === BladePage::class
0 ignored issues
show
introduced by
The condition $pageClass === Hyde\Fram...\Pages\BladePage::class is always false.
Loading history...
34
            ? $this->parseBladePage()
35
            : $this->parseMarkdownPage($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...er::parseMarkdownPage(). ( Ignorable by Annotation )

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

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