Passed
Push — master ( c41583...31e373 )
by Caen
04:00 queued 14s
created

PageMetadataBag::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Metadata;
6
7
use Hyde\Facades\Meta;
8
use Hyde\Pages\Concerns\HydePage;
9
use Hyde\Pages\MarkdownPost;
10
use Hyde\Foundation\Kernel\Hyperlinks;
11
12
use function substr_count;
13
use function str_repeat;
14
15
class PageMetadataBag extends MetadataBag
16
{
17
    protected HydePage $page;
18
19
    public function __construct(HydePage $page)
20
    {
21
        $this->page = $page;
22
23
        $this->generate();
24
    }
25
26
    protected function generate(): void
27
    {
28
        $this->addDynamicPageMetadata($this->page);
29
    }
30
31
    protected function addDynamicPageMetadata(HydePage $page): void
32
    {
33
        if ($page->getCanonicalUrl()) {
34
            $this->add(Meta::link('canonical', $page->getCanonicalUrl()));
35
        }
36
37
        if ($page->has('description')) {
38
            $this->add(Meta::name('description', $page->data('description')));
39
            $this->add(Meta::property('description', $page->data('description')));
40
        }
41
42
        if ($page->has('title')) {
43
            $this->add(Meta::name('twitter:title', $page->title()));
44
            $this->add(Meta::property('title', $page->title()));
45
        }
46
47
        if ($page instanceof MarkdownPost) {
48
            $this->addMetadataForMarkdownPost($page);
49
        }
50
    }
51
52
    protected function addMetadataForMarkdownPost(MarkdownPost $page): void
53
    {
54
        $this->addPostMetadataIfExists($page, 'author');
55
        $this->addPostMetadataIfExists($page, 'category', 'keywords');
56
57
        if ($page->getCanonicalUrl()) {
58
            $this->add(Meta::name('url', $page->getCanonicalUrl()));
59
            $this->add(Meta::property('url', $page->getCanonicalUrl()));
60
        }
61
62
        if ($page->has('date')) {
63
            $this->add(Meta::property('og:article:published_time', $page->date->datetime));
64
        }
65
66
        if ($page->has('image')) {
67
            $this->add(Meta::property('image', $this->resolveImageLink((string) $page->data('image'))));
68
        }
69
70
        $this->add(Meta::property('type', 'article'));
71
    }
72
73
    protected function addPostMetadataIfExists(MarkdownPost $page, string $property, ?string $name = null): void
74
    {
75
        if ($page->has($property)) {
76
            $this->add(Meta::name($name ?? $property, (string) $page->data($property)));
77
        }
78
    }
79
80
    protected function resolveImageLink(string $image): string
81
    {
82
        // Since this is run before the page is rendered, we don't have the currentPage property.
83
        // So we need to run some of the same calculations here to resolve the image path link.
84
        return Hyperlinks::isRemote($image) ? $image : $this->calculatePathTraversal().$image;
85
    }
86
87
    private function calculatePathTraversal(): string
88
    {
89
        return str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/'));
90
    }
91
}
92