Passed
Push — master ( 850f54...8ab35d )
by Caen
02:55 queued 02:55
created

MarkdownPost::getAuthorName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
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
27
        $this->constructBlogPostSchema();
28
        $this->constructMetadata();
29
    }
30
31
    /** @deprecated v0.58.x-beta (may be moved to BlogPostSchema) */
32
    public function getCanonicalLink(): string
33
    {
34
        return Hyde::url($this->getCurrentPagePath().'.html');
35
    }
36
37
    /** @deprecated v0.58.x-beta (pull description instead) */
38
    public function getPostDescription(): string
39
    {
40
        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...
41
    }
42
43
    public static function getLatestPosts(): Collection
44
    {
45
        return static::all()->sortByDesc('matter.date');
46
    }
47
48
    // HasArticleMetadata (Generates article metadata for a MarkdownPost)
49
50
    public array $metadata = [];
51
    public array $properties = [];
52
53
    protected function constructMetadata(): void
54
    {
55
        $this->parseFrontMatterMetadata();
56
57
        $this->makeOpenGraphPropertiesForArticle();
58
    }
59
60
    public function getMetadata(): array
61
    {
62
        return $this->metadata;
63
    }
64
65
    public function getMetaProperties(): array
66
    {
67
        return $this->properties;
68
    }
69
70
    /**
71
     * Generate metadata from the front matter that can be used in standard <meta> tags.
72
     * This helper is page type agnostic and works with any kind of model having front matter.
73
     */
74
    protected function parseFrontMatterMetadata(): void
75
    {
76
        if (! empty($this->description)) {
77
            $this->metadata['description'] = $this->description;
78
        }
79
80
        if ($this->author) {
81
            $this->metadata['author'] = $this->author->getName();
82
        }
83
84
        if ($this->category) {
85
            $this->metadata['keywords'] = $this->category;
86
        }
87
    }
88
89
    /**
90
     * Generate opengraph metadata from front matter for an og:article such as a blog post.
91
     */
92
    protected function makeOpenGraphPropertiesForArticle(): void
93
    {
94
        $this->properties['og:type'] = 'article';
95
        if (Hyde::hasSiteUrl()) {
96
            $this->properties['og:url'] = $this->getRoute()->getQualifiedUrl();
97
        }
98
99
        if ($this->title) {
100
            $this->properties['og:title'] = $this->title;
101
        }
102
103
        if ($this->matter('date') !== null) {
104
            $this->properties['og:article:published_time'] = $this->date->dateTimeObject->format('c');
105
        }
106
107
        if ($this->matter('image') !== null) {
108
            $this->setImageMetadata();
109
        }
110
    }
111
112
    protected function setImageMetadata(): void
113
    {
114
        if ($this->image) {
115
            $this->properties['og:image'] = $this->image->getLink();
116
        }
117
    }
118
}
119