Passed
Push — master ( f221b7...fb8663 )
by Caen
12:45 queued 09:03
created

AbstractMarkdownPage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A markdown() 0 3 1
A matter() 0 3 1
A compile() 0 6 1
1
<?php
2
3
namespace Hyde\Framework\Contracts;
4
5
use Hyde\Framework\Actions\MarkdownConverter;
6
use Hyde\Framework\Concerns\HasDynamicTitle;
7
use Hyde\Framework\Models\MarkdownDocument;
8
9
/**
10
 * The base class for all Markdown-based Page Models.
11
 *
12
 * @since 0.44.x replaces MarkdownDocument
13
 *
14
 * Extends the AbstractPage class to provide relevant
15
 * helpers for Markdown-based page model classes.
16
 * @see \Hyde\Framework\Models\Pages\MarkdownPage
17
 * @see \Hyde\Framework\Models\Pages\MarkdownPost
18
 * @see \Hyde\Framework\Models\Pages\DocumentationPage
19
 * @see \Hyde\Framework\Contracts\AbstractPage
20
 *
21
 * @test \Hyde\Framework\Testing\Feature\AbstractPageTest
22
 */
23
abstract class AbstractMarkdownPage extends AbstractPage implements MarkdownPageContract
24
{
25
    use HasDynamicTitle;
26
27
    public MarkdownDocument $markdown;
28
29
    public array $matter;
30
    public string $body;
31
    public string $title;
32
    public string $slug;
33
34
    public static string $fileExtension = '.md';
35
36
    public function __construct(array $matter = [], string $body = '', string $title = '', string $slug = '', ?MarkdownDocument $markdownDocument = null)
37
    {
38
        $this->matter = $matter;
39
        $this->body = $body;
40
        $this->title = $title;
41
        $this->slug = $slug;
42
43
        $this->markdown = $markdownDocument ?? new MarkdownDocument($matter, $body);
44
45
        $this->constructDynamicTitle();
46
    }
47
48
    public function markdown(): MarkdownDocument
49
    {
50
        return $this->markdown;
51
    }
52
53
    public function matter(string $key = null, mixed $default = null): mixed
54
    {
55
        return $this->markdown->matter($key, $default);
56
    }
57
58
    /** @inheritDoc */
59
    public function compile(): string
60
    {
61
        return view($this->getBladeView())->with([
62
            'title' => $this->title,
63
            'markdown' => MarkdownConverter::parse($this->body, static::class),
64
        ])->render();
65
    }
66
}
67