Passed
Push — master ( dff5c5...cec494 )
by Caen
03:44 queued 11s
created

HasArticleMetadata::constructMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
use Hyde\Framework\Hyde;
6
use Hyde\Framework\Models\Pages\MarkdownPost;
7
8
/**
9
 * Generates article metadata for a MarkdownPost.
10
 *
11
 * @see \Hyde\Framework\Models\Metadata
12
 * @see \Hyde\Framework\Testing\Feature\Concerns\HasArticleMetadataTest
13
 */
14
trait HasArticleMetadata
15
{
16
    public array $metadata = [];
17
    public array $properties = [];
18
19
    public function constructMetadata(): void
20
    {
21
        $this->parseFrontMatterMetadata();
22
23
        $this->makeOpenGraphPropertiesForArticle();
24
    }
25
26
    public function getMetadata(): array
27
    {
28
        return $this->metadata;
29
    }
30
31
    public function getMetaProperties(): array
32
    {
33
        return $this->properties;
34
    }
35
36
    /**
37
     * Generate metadata from the front matter that can be used in standard <meta> tags.
38
     * This helper is page type agnostic and works with any kind of model having front matter.
39
     */
40
    protected function parseFrontMatterMetadata(): void
41
    {
42
        if (isset($this->matter['description'])) {
43
            $this->metadata['description'] = $this->matter['description'];
44
        }
45
46
        if (isset($this->matter['author'])) {
47
            $this->metadata['author'] = $this->getAuthorName($this->matter['author']);
48
        }
49
50
        if (isset($this->matter['category'])) {
51
            $this->metadata['keywords'] = $this->matter['category'];
52
        }
53
    }
54
55
    /**
56
     * Generate opengraph metadata from front matter for an og:article such as a blog post.
57
     */
58
    protected function makeOpenGraphPropertiesForArticle(): void
59
    {
60
        $this->properties['og:type'] = 'article';
61
        if (Hyde::hasSiteUrl()) {
62
            $this->properties['og:url'] = $this->getRoute()->getQualifiedUrl();
0 ignored issues
show
Bug introduced by
It seems like getRoute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            $this->properties['og:url'] = $this->/** @scrutinizer ignore-call */ getRoute()->getQualifiedUrl();
Loading history...
63
        }
64
65
        if (isset($this->matter['title'])) {
66
            $this->properties['og:title'] = $this->matter['title'];
67
        }
68
69
        if (isset($this->matter['date'])) {
70
            $this->properties['og:article:published_time'] = date('c', strtotime($this->matter['date']));
71
        }
72
73
        if (isset($this->matter['image'])) {
74
            $this->setImageMetadata();
75
        }
76
    }
77
78
    /**
79
     * Parse the author name string from front matter with support for both flat and array notation.
80
     *
81
     * @param  string|array  $author
82
     * @return string
83
     */
84
    protected function getAuthorName(string|array $author): string
85
    {
86
        if (is_string($author)) {
0 ignored issues
show
introduced by
The condition is_string($author) is always false.
Loading history...
87
            return $author;
88
        }
89
90
        return $author['name'] ?? $author['username'] ?? 'Guest';
91
    }
92
93
    protected function setImageMetadata(): void
94
    {
95
        if (is_string($this->matter['image'])) {
96
            $this->properties['og:image'] = $this->matter['image'];
97
        } else {
98
            if (isset($this->matter['image']['path'])) {
99
                $this->properties['og:image'] = $this->matter['image']['path'];
100
            }
101
            if (isset($this->matter['image']['uri'])) {
102
                $this->properties['og:image'] = $this->matter['image']['uri'];
103
            }
104
        }
105
    }
106
}
107