Passed
Push — master ( 600cae...0a2876 )
by jelmer
05:21 queued 11s
created

MetaLink::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Frontend\Core\Header;
4
5
use InvalidArgumentException;
6
7
final class MetaLink
8
{
9
    /** @var array */
10
    private $attributes;
11
12
    /** @var string */
13
    private $uniqueKey;
14
15
    /**
16
     * @param string $href since we always need a href we've added it as a separate parameter
17
     * @param string[] $attributes
18
     * @param string[] $uniqueAttributeKeys
19
     *
20
     * @throws InvalidArgumentException when the content is empty
21
     */
22 11
    public function __construct(
23
        string $href,
24
        array $attributes,
25
        array $uniqueAttributeKeys = ['rel', 'hreflang', 'type', 'title']
26
    ) {
27 11
        if (empty($href)) {
28 1
            throw new InvalidArgumentException('The href can not be empty');
29
        }
30
31 10
        $this->attributes = ['href' => $href] + $attributes;
32 10
        $this->createUniqueKey($uniqueAttributeKeys);
33 10
    }
34
35
    /**
36
     * @param string[] $uniqueAttributeKeys
37
     */
38 10
    private function createUniqueKey(array $uniqueAttributeKeys): void
39
    {
40
        // make sure the keys are sorted alphabetically
41 10
        sort($uniqueAttributeKeys);
42
43 10
        $this->uniqueKey = implode(
44 10
            '|',
45 10
            array_filter(
46 10
                array_map(
47 10
                    function (string $attributeKey) {
48 10
                        return $this->attributes[$attributeKey] ?? '';
49 10
                    },
50 10
                    $uniqueAttributeKeys
51
                )
52
            )
53
        );
54 10
    }
55
56 8
    public function getUniqueKey(): string
57
    {
58 8
        return $this->uniqueKey;
59
    }
60
61 6
    public function hasAttributeWithValue(string $attributeKey, string $attributeValue): bool
62
    {
63 6
        return isset($this->attributes[$attributeKey]) && $this->attributes[$attributeKey] === $attributeValue;
64
    }
65
66 3
    public static function canonical(string $href): self
67
    {
68 3
        return new self(
69 3
            $href,
70 3
            ['rel' => 'canonical']
71
        );
72
    }
73
74 1
    public static function rss(string $href, string $title): self
75
    {
76 1
        return new self(
77 1
            $href,
78 1
            ['rel' => 'alternate', 'type' => 'application/rss+xml', 'title' => $title]
79
        );
80
    }
81
82 1
    public static function alternateLanguage(string $href, string $language): self
83
    {
84 1
        return new self(
85 1
            $href,
86 1
            ['rel' => 'alternate', 'hreflang' => $language]
87
        );
88
    }
89
90 1
    public static function next(string $href): self
91
    {
92 1
        return new self(
93 1
            $href,
94 1
            ['rel' => 'next']
95
        );
96
    }
97
98 1
    public static function previous(string $href): self
99
    {
100 1
        return new self(
101 1
            $href,
102 1
            ['rel' => 'prev']
103
        );
104
    }
105
106 2
    public function __toString(): string
107
    {
108 2
        $html = '<link ';
109 2
        $html .= implode(
110 2
            ' ',
111 2
            array_map(
112 2
                function (string $parameterKey, string $parameterValue) {
113 2
                    return $parameterKey . '="' . $parameterValue . '"';
114 2
                },
115
                array_keys($this->attributes),
116
                $this->attributes
117
            )
118
        );
119
        $html .= '>';
120
121
        return $html;
122
    }
123
}
124