Passed
Push — master ( a0ff31...47a9f8 )
by Caen
03:50 queued 14s
created

SourceFileParser::parseMarkdownPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.9332
1
<?php
2
3
namespace Hyde\Framework\Actions;
4
5
use Hyde\Framework\Concerns\ValidatesExistence;
6
use Hyde\Framework\Contracts\PageContract;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\Pages\BladePage;
9
use Hyde\Framework\Models\Pages\DocumentationPage;
10
use Hyde\Framework\Models\Pages\MarkdownPage;
11
use Hyde\Framework\Models\Pages\MarkdownPost;
12
use Hyde\Framework\Modules\Markdown\MarkdownFileParser;
13
use Illuminate\Support\Str;
14
15
/**
16
 * Parses a source file and returns a new page model instance for it.
17
 *
18
 * Page Parsers are responsible for parsing a source file into a Page object,
19
 * and may also conduct pre-processing and/or data validation/assembly.
20
 *
21
 * Note that the Page Parsers do not compile any HTML or Markdown.
22
 *
23
 * @see \Hyde\Framework\Testing\Feature\SourceFileParserTest
24
 */
25
class SourceFileParser
26
{
27
    use ValidatesExistence;
28
29
    protected string $slug;
30
    protected PageContract $page;
31
32
    public function __construct(string $pageClass, string $slug)
33
    {
34
        $this->validateExistence($pageClass, $slug);
35
36
        $this->slug = $slug;
37
38
        $this->page = match ($pageClass) {
39
            BladePage::class => $this->parseBladePage(),
40
            MarkdownPage::class => $this->parseMarkdownPage(),
41
            MarkdownPost::class => $this->parseMarkdownPost(),
42
            DocumentationPage::class => $this->parseDocumentationPage(),
43
        };
44
    }
45
46
    protected function parseBladePage(): BladePage
47
    {
48
        return new BladePage($this->slug);
49
    }
50
51
    protected function parseMarkdownPage(): MarkdownPage
52
    {
53
        $document = (new MarkdownFileParser(
54
            Hyde::getMarkdownPagePath($this->slug.MarkdownPage::$fileExtension)
55
        ))->get();
56
57
        $matter = $document->matter;
58
        $body = $document->body;
59
60
        return new MarkdownPage(
61
            matter: $matter,
62
            body: $body,
63
            title: FindsTitleForDocument::get($this->slug, $matter, $body),
64
            slug: $this->slug
65
        );
66
    }
67
68
    protected function parseMarkdownPost(): MarkdownPost
69
    {
70
        $document = (new MarkdownFileParser(
71
            Hyde::getMarkdownPostPath($this->slug.MarkdownPost::$fileExtension)
72
        ))->get();
73
74
        $matter = $document->matter;
75
        $body = $document->body;
76
77
        return new MarkdownPost(
78
            matter: $matter,
79
            body: $body,
80
            title: FindsTitleForDocument::get($this->slug, $matter, $body),
81
            slug: $this->slug
82
        );
83
    }
84
85
    protected function parseDocumentationPage(): DocumentationPage
86
    {
87
        $document = (new MarkdownFileParser(
88
            Hyde::getDocumentationPagePath($this->slug.DocumentationPage::$fileExtension)
89
        ))->get();
90
91
        $matter = array_merge($document->matter, [
92
            'slug' => $this->slug,
93
        ]);
94
95
        $body = $document->body;
96
97
        return new DocumentationPage(
98
            matter: $matter,
99
            body: $body,
100
            title: FindsTitleForDocument::get($this->slug, $matter, $body),
101
            slug: basename($this->slug),
102
            category: $this->getDocumentationPageCategory($matter),
103
            localPath: $this->slug
104
        );
105
    }
106
107
    protected function getDocumentationPageCategory(array $matter): ?string
108
    {
109
        if (str_contains($this->slug, '/')) {
110
            return Str::before($this->slug, '/');
111
        }
112
113
        return $matter['category'] ?? null;
114
    }
115
116
    public function get(): PageContract
117
    {
118
        return $this->page;
119
    }
120
}
121