Metadata::imageTag()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 7
nc 4
nop 0
crap 3
1
<?php
2
3
namespace Vssl\Render;
4
5
class Metadata
6
{
7
    /**
8
     * The render configuration.
9
     *
10
     * @var array
11
     */
12
    protected $config;
13
14
    /**
15
     * The page array data.
16
     *
17
     * @var array
18
     */
19
    protected $page;
20
21
    /**
22
     * List of all renderable tags.
23
     *
24
     * @var array
25
     */
26
    protected $tags = [
27
        'title',
28
        'description',
29
        'image'
30
    ];
31
32
    /**
33
     * Initialize our Metadata renderer.
34
     */
35 8
    public function __construct($config, $page)
36
    {
37 8
        $this->config = $config;
38 8
        $this->page = $page;
39 8
    }
40
41
    /**
42
     * Get the title metatag.
43
     *
44
     * @return string
45
     */
46 3 View Code Duplication
    public function titleTag()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 3
        $title = empty($this->page['title']) ? null : htmlspecialchars($this->page['title'], ENT_QUOTES);
49
        return $title
50 3
            ? '<meta property="og:title" content="' . $title . '" />'
51 3
            : null;
52
    }
53
54
    /**
55
     * Get the description metatag.
56
     *
57
     * @return string
58
     */
59 3 View Code Duplication
    public function descriptionTag()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61 3
        $summary = empty($this->page['summary']) ? null : htmlspecialchars($this->page['summary'], ENT_QUOTES);
62
        return $summary
63 3
            ? '<meta property="og:description" content="' . $summary . '" />'
64 3
            : null;
65
    }
66
67
    /**
68
     * Get the image metatag.
69
     *
70
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
71
     */
72 3
    public function imageTag()
73
    {
74 3
        $image = empty($this->page['image'])
75 3
            ? null
76 3
            : ltrim($this->config['base_uri'], '/') . "/images/" . $this->page['image'];
77
        return $image
78 3
            ? '<meta property="og:image" content="' . $image . '" />'
79 3
            : null;
80
    }
81
82
    /**
83
     * Get all available tags.
84
     *
85
     * @return array
86
     */
87
    public function getTags()
88
    {
89 2
        return array_filter(array_map(function ($tag) {
90 2
            return $this->{$tag . "Tag"}();
91 2
        }, $this->tags));
92
    }
93
94
95
    /**
96
     * Generate the metadata.
97
     *
98
     * @return string
99
     */
100 1
    public function __toString()
101
    {
102 1
        return implode("\n", $this->getTags());
103
    }
104
}
105