Completed
Push — master ( 6b77c2...0b4fa8 )
by Andrii
04:43
created

RenderedPage::setMetaData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace hiqdev\yii2\modules\pages\models;
4
5
class RenderedPage extends AbstractPage
6
{
7
    /** @var null|string */
8
    private $featuredImageUrl;
9
10
    /** @var null|string */
11
    private $slug;
12
13
    /** @var null|string */
14
    private $keywords;
15
16
    /** @var null|string */
17
    private $description;
18
19
    /** @var null|string */
20
    private $canonical;
21
22
    const META_DATA = ['keywords', 'description', 'canonical'];
23
24
    public function render(array $params = [])
25
    {
26
        $this->setMetaData();
27
28
        return $this->text;
29
    }
30
31
    /**
32
     * Renders miniature version of page for list
33
     * @return string
34
     */
35
    public function renderMiniature(): string
36
    {
37
        $img = $this->featuredImageUrl ?
38
               "<img src=\"$this->featuredImageUrl\" alt=\"$this->slug\">" :
39
               '';
40
41
        return <<<HTML
42
<h1 class="post-title"><a href="$this->url">$this->title</a></h1>
43
$img
44
$this->text
45
HTML;
46
    }
47
48
    private function setMetaData(): void
49
    {
50
        foreach (self::META_DATA as $tag) {
51
            if (is_null($this->{$tag})) {
52
                continue;
53
            }
54
            $this->view->registerMetaTag([
55
                'name' => $tag,
56
                'content' => $this->{$tag},
57
            ]);
58
        }
59
    }
60
61
    /**
62
     * @param null|string $slug
63
     */
64
    public function setSlug(?string $slug): void
65
    {
66
        $this->slug = $slug;
67
    }
68
69
    /**
70
     * @param null|string $keywords
71
     */
72
    public function setKeywords(?string $keywords): void
73
    {
74
        $this->keywords = $keywords;
75
    }
76
77
    /**
78
     * @param null|string $description
79
     */
80
    public function setDescription(?string $description): void
81
    {
82
        $this->description = $description;
83
    }
84
85
    /**
86
     * @param null|string $featuredImageUrl
87
     */
88
    public function setFeaturedImageUrl(?string $featuredImageUrl): void
89
    {
90
        $this->featuredImageUrl = $featuredImageUrl;
91
    }
92
93
    /**
94
     * @param null|string $canonical
95
     */
96
    public function setCanonical(?string $canonical): void
97
    {
98
        $this->canonical = $canonical;
99
    }
100
}
101