Passed
Push — master ( d0e934...b4bd91 )
by Caen
04:29 queued 12s
created

HasPageMetadata::makeRssFeedLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 4
b 0
f 1
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
use Hyde\Framework\Contracts\RouteContract;
6
use Hyde\Framework\Helpers\Features;
7
use Hyde\Framework\Helpers\Meta;
8
use Hyde\Framework\Hyde;
9
use Hyde\Framework\Models\Pages\MarkdownPost;
10
use Hyde\Framework\Services\RssFeedService;
11
12
/**
13
 * @todo Move logic into service class to make it easier to test.
14
 *
15
 * @see \Hyde\Framework\Testing\Feature\Concerns\HasPageMetadataTest
16
 */
17
trait HasPageMetadata
18
{
19
    abstract public function htmlTitle(?string $title = null): string;
20
21
    abstract public function getRoute(): RouteContract;
22
23
    public function getCanonicalUrl(): string
24
    {
25
        return $this->getRoute()->getQualifiedUrl();
26
    }
27
28
    /**
29
     * @return string[]
30
     *
31
     * @psalm-return list<string>
32
     */
33
    public function getDynamicMetadata(): array
34
    {
35
        $array = [];
36
37
        if ($this->canUseCanonicalUrl()) {
38
            $array[] = '<link rel="canonical" href="'.$this->getCanonicalUrl().'" />';
39
        }
40
41
        if (Features::sitemap()) {
42
            $array[] = '<link rel="sitemap" type="application/xml" title="Sitemap" href="'.Hyde::url('sitemap.xml').'" />';
43
        }
44
45
        if (Features::rss()) {
46
            $array[] = $this->makeRssFeedLink();
47
        }
48
49
        if (isset($this->title)) {
50
            if ($this->hasTwitterTitleInConfig()) {
51
                $array[] = '<meta name="twitter:title" content="'.$this->htmlTitle().'" />';
52
            }
53
            if ($this->hasOpenGraphTitleInConfig()) {
54
                $array[] = '<meta property="og:title" content="'.$this->htmlTitle().'" />';
55
            }
56
        }
57
58
        if ($this instanceof MarkdownPost) {
59
            $array[] = "\n<!-- Blog Post Meta Tags -->";
60
            foreach ($this->getMetadata() as $name => $content) {
61
                $array[] = Meta::name($name, $content);
62
            }
63
            foreach ($this->getMetaProperties() as $property => $content) {
64
                $array[] = Meta::property($property, $content);
65
            }
66
        }
67
68
        return $array;
69
    }
70
71
    public function renderPageMetadata(): string
72
    {
73
        return Meta::render(
74
            withMergedData: $this->getDynamicMetadata()
75
        );
76
    }
77
78
    public function canUseCanonicalUrl(): bool
79
    {
80
        return Hyde::hasSiteUrl() && isset($this->slug);
81
    }
82
83
    public function hasTwitterTitleInConfig(): bool
84
    {
85
        return str_contains(json_encode(config('hyde.meta', [])), 'twitter:title');
86
    }
87
88
    public function hasOpenGraphTitleInConfig(): bool
89
    {
90
        return str_contains(json_encode(config('hyde.meta', [])), 'og:title');
91
    }
92
93
    protected function makeRssFeedLink(): string
94
    {
95
        return sprintf(
96
            '<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />',
97
            RssFeedService::getDescription(),
98
            Hyde::url(RssFeedService::getDefaultOutputFilename())
99
        );
100
    }
101
}
102