Passed
Push — master ( 4a483b...d568b2 )
by Caen
03:07 queued 12s
created

BaseMarkdownPage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 44
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 5 1
A compile() 0 6 1
A make() 0 3 1
A markdown() 0 3 1
A __construct() 0 7 1
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
use Hyde\Framework\Contracts\MarkdownDocumentContract;
6
use Hyde\Framework\Contracts\MarkdownPageContract;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\FrontMatter;
9
use Hyde\Framework\Models\Markdown;
10
11
/**
12
 * The base class for all Markdown-based page models.
13
 *
14
 * @see \Hyde\Framework\Models\Pages\MarkdownPage
15
 * @see \Hyde\Framework\Models\Pages\MarkdownPost
16
 * @see \Hyde\Framework\Models\Pages\DocumentationPage
17
 * @see \Hyde\Framework\Concerns\HydePage
18
 * @see \Hyde\Framework\Testing\Feature\HydePageTest
19
 */
20
abstract class BaseMarkdownPage extends HydePage implements MarkdownDocumentContract, MarkdownPageContract
21
{
22
    public string $identifier;
23
    public Markdown $markdown;
24
25
    public static string $fileExtension = '.md';
26
27
    /** @interitDoc */
28
    public static function make(string $identifier = '', array $matter = [], string $body = ''): static
29
    {
30
        return new static($identifier, new FrontMatter($matter), new Markdown($body));
31
    }
32
33
    /** @interitDoc */
34
    public function __construct(string $identifier = '', ?FrontMatter $matter = null, ?Markdown $markdown = null)
35
    {
36
        $this->identifier = $identifier;
37
        $this->matter = $matter ?? new FrontMatter();
38
        $this->markdown = $markdown ?? new Markdown();
39
40
        parent::__construct($this->identifier, $this->matter);
41
    }
42
43
    /** @inheritDoc */
44
    public function markdown(): Markdown
45
    {
46
        return $this->markdown;
47
    }
48
49
    /** @inheritDoc */
50
    public function compile(): string
51
    {
52
        return view($this->getBladeView())->with([
53
            'title' => $this->title,
54
            'markdown' => $this->markdown->compile(static::class),
55
        ])->render();
56
    }
57
58
    /** @inheritDoc */
59
    public function save(): static
60
    {
61
        file_put_contents(Hyde::path($this->getSourcePath()), ltrim("$this->matter\n$this->markdown"));
62
63
        return $this;
64
    }
65
}
66