Passed
Push — master ( c4a5b0...3b0e83 )
by Caen
03:01 queued 12s
created

MarkdownPost::getPostDescription()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models\Pages;
4
5
use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema;
6
use Hyde\Framework\Contracts\AbstractMarkdownPage;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\FrontMatter;
9
use Hyde\Framework\Models\Markdown;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * @see \Hyde\Framework\Testing\Feature\MarkdownPostTest
14
 */
15
class MarkdownPost extends AbstractMarkdownPage
16
{
17
    use BlogPostSchema;
18
19
    public static string $sourceDirectory = '_posts';
20
    public static string $outputDirectory = 'posts';
21
    public static string $template = 'hyde::layouts/post';
22
23
    public function __construct(string $identifier = '', ?FrontMatter $matter = null, ?Markdown $markdown = null)
24
    {
25
        parent::__construct($identifier, $matter, $markdown);
26
        $this->constructMetadata();
27
    }
28
29
    protected function constructPageSchemas(): void
30
    {
31
        parent::constructPageSchemas();
32
        $this->constructBlogPostSchema();
33
    }
34
35
    /** @deprecated v0.58.x-beta (may be moved to BlogPostSchema) */
36
    public function getCanonicalLink(): string
37
    {
38
        return Hyde::url($this->getCurrentPagePath().'.html');
39
    }
40
41
    /** @deprecated v0.58.x-beta (pull description instead) */
42
    public function getPostDescription(): string
43
    {
44
        return $this->description;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->description could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
45
    }
46
47
    public static function getLatestPosts(): Collection
48
    {
49
        return static::all()->sortByDesc('matter.date');
50
    }
51
52
    // HasArticleMetadata (Generates article metadata for a MarkdownPost)
53
54
    public array $metadata = [];
55
    public array $properties = [];
56
57
    protected function constructMetadata(): void
58
    {
59
        $this->parseFrontMatterMetadata();
60
61
        $this->makeOpenGraphPropertiesForArticle();
62
    }
63
64
    public function getMetadata(): array
65
    {
66
        return $this->metadata;
67
    }
68
69
    public function getMetaProperties(): array
70
    {
71
        return $this->properties;
72
    }
73
74
    /**
75
     * Generate metadata from the front matter that can be used in standard <meta> tags.
76
     * This helper is page type agnostic and works with any kind of model having front matter.
77
     */
78
    protected function parseFrontMatterMetadata(): void
79
    {
80
        if (! empty($this->description)) {
81
            $this->metadata['description'] = $this->description;
82
        }
83
84
        if ($this->author) {
85
            $this->metadata['author'] = $this->author->getName();
86
        }
87
88
        if ($this->category) {
89
            $this->metadata['keywords'] = $this->category;
90
        }
91
    }
92
93
    /**
94
     * Generate opengraph metadata from front matter for an og:article such as a blog post.
95
     */
96
    protected function makeOpenGraphPropertiesForArticle(): void
97
    {
98
        $this->properties['og:type'] = 'article';
99
        if (Hyde::hasSiteUrl()) {
100
            $this->properties['og:url'] = $this->getRoute()->getQualifiedUrl();
101
        }
102
103
        if ($this->title) {
104
            $this->properties['og:title'] = $this->title;
105
        }
106
107
        if ($this->matter('date') !== null) {
108
            $this->properties['og:article:published_time'] = $this->date->dateTimeObject->format('c');
109
        }
110
111
        if ($this->matter('image') !== null) {
112
            $this->setImageMetadata();
113
        }
114
    }
115
116
    protected function setImageMetadata(): void
117
    {
118
        if ($this->image) {
119
            $this->properties['og:image'] = $this->image->getLink();
120
        }
121
    }
122
}
123