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

HasPageMetadata   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 98
rs 10
c 7
b 0
f 0
wmc 24

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hasOpenGraphTitleInConfig() 0 3 1
A canUseRssFeedLink() 0 17 6
B getDynamicMetadata() 0 36 10
A getCanonicalUrl() 0 3 1
A hasTwitterTitleInConfig() 0 3 1
A canUseSitemapLink() 0 3 1
A makeRssFeedLink() 0 5 1
A renderPageMetadata() 0 4 1
A canUseCanonicalUrl() 0 3 2
1
<?php
2
3
namespace Hyde\Framework\Concerns;
4
5
use Hyde\Framework\Helpers\Meta;
6
use Hyde\Framework\Hyde;
7
use Hyde\Framework\Models\Pages\MarkdownPost;
8
use Hyde\Framework\Services\RssFeedService;
9
use Hyde\Framework\Services\SitemapService;
10
11
/**
12
 * @todo Move logic into service class to make it easier to test.
13
 *
14
 * @see \Hyde\Framework\Testing\Feature\Concerns\HasPageMetadataTest
15
 */
16
trait HasPageMetadata
17
{
18
    abstract public function htmlTitle(?string $title = null): string;
19
20
    public function getCanonicalUrl(): string
21
    {
22
        return $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

22
        return $this->/** @scrutinizer ignore-call */ getRoute()->getQualifiedUrl();
Loading history...
23
    }
24
25
    public function getDynamicMetadata(): array
26
    {
27
        $array = [];
28
29
        if ($this->canUseCanonicalUrl()) {
30
            $array[] = '<link rel="canonical" href="'.$this->getCanonicalUrl().'" />';
31
        }
32
33
        if ($this->canUseSitemapLink()) {
34
            $array[] = '<link rel="sitemap" type="application/xml" title="Sitemap" href="'.Hyde::url('sitemap.xml').'" />';
35
        }
36
37
        if ($this->canUseRssFeedLink()) {
38
            $array[] = $this->makeRssFeedLink();
39
        }
40
41
        if (isset($this->title)) {
42
            if ($this->hasTwitterTitleInConfig()) {
43
                $array[] = '<meta name="twitter:title" content="'.$this->htmlTitle().'" />';
44
            }
45
            if ($this->hasOpenGraphTitleInConfig()) {
46
                $array[] = '<meta property="og:title" content="'.$this->htmlTitle().'" />';
47
            }
48
        }
49
50
        if ($this instanceof MarkdownPost) {
51
            $array[] = "\n<!-- Blog Post Meta Tags -->";
52
            foreach ($this->getMetadata() as $name => $content) {
53
                $array[] = Meta::name($name, $content);
54
            }
55
            foreach ($this->getMetaProperties() as $property => $content) {
56
                $array[] = Meta::property($property, $content);
57
            }
58
        }
59
60
        return $array;
61
    }
62
63
    public function renderPageMetadata(): string
64
    {
65
        return Meta::render(
66
            withMergedData: $this->getDynamicMetadata()
67
        );
68
    }
69
70
    public function canUseCanonicalUrl(): bool
71
    {
72
        return Hyde::hasSiteUrl() && isset($this->slug);
73
    }
74
75
    public function canUseSitemapLink(): bool
76
    {
77
        return SitemapService::canGenerateSitemap();
78
    }
79
80
    public function canUseRssFeedLink(): bool
81
    {
82
        if (RssFeedService::canGenerateFeed() && isset($this->slug)) {
83
            if ($this instanceof MarkdownPost) {
84
                return true;
85
            }
86
87
            if (str_starts_with($this->getCurrentPagePath(), 'post')) {
0 ignored issues
show
Bug introduced by
It seems like getCurrentPagePath() 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

87
            if (str_starts_with($this->/** @scrutinizer ignore-call */ getCurrentPagePath(), 'post')) {
Loading history...
88
                return true;
89
            }
90
91
            if ($this->getCurrentPagePath() === 'index') {
92
                return true;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    public function hasTwitterTitleInConfig(): bool
100
    {
101
        return str_contains(json_encode(config('hyde.meta', [])), 'twitter:title');
102
    }
103
104
    public function hasOpenGraphTitleInConfig(): bool
105
    {
106
        return str_contains(json_encode(config('hyde.meta', [])), 'og:title');
107
    }
108
109
    protected function makeRssFeedLink(): string
110
    {
111
        return sprintf('<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />',
112
            RssFeedService::getDescription(),
113
            Hyde::url(RssFeedService::getDefaultOutputFilename())
114
        );
115
    }
116
}
117