DefaultRenderer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 52
ccs 32
cts 32
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initPreprocessors() 0 30 1
A preprocessValue() 0 4 1
A getPattern() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SeoHelper\Renderer;
6
7
class DefaultRenderer extends AbstractRenderer
8
{
9
    protected array $types = [
10
        'default' => '<meta name="{$key}" content="{$value}">',
11
        'title' => '<title>{$value}</title>',
12
        'canonical' => '<link rel="canonical" href="{$value}">',
13
        'next' => '<link rel="next" href="{$value}">',
14
        'prev' => '<link rel="prev" href="{$value}">',
15 27
    ];
16
17 27
    protected function getPattern($type): ?string
18
    {
19
        return parent::getPattern($type) ?: $this->types['default'];
20 9
    }
21
22
    protected function initPreprocessors(): void
23 4
    {
24 6
        $this->setPreprocessor('default', function ($value) {
25 6
            return array_map(function ($val) {
26 27
                return htmlspecialchars(strip_tags($val));
27 18
            }, (array)$value);
28 9
        });
29 27
        $this->setPreprocessor('title', function ($value) {
30 18
            return strip_tags(implode(' | ', array_reverse($value)));
31 9
        });
32 27
        $this->setPreprocessor('description', function ($value) {
33 18
            return htmlspecialchars(strip_tags(implode(' ', $value)));
34 3
        });
35 27
        $this->setPreprocessor('keywords', function ($value) {
36
            return htmlspecialchars(strip_tags(implode(', ', array_unique($value))));
37 2
        });
38 3
        $this->setPreprocessor('canonical', function ($value) {
39 3
            return array_map(function ($val) {
40 27
                return htmlspecialchars(strip_tags($val));
41
            }, $value);
42 2
        });
43 3
        $this->setPreprocessor('next', function ($value) {
44 3
            return array_map(function ($val) {
45 27
                return htmlspecialchars(strip_tags($val));
46
            }, $value);
47 3
        });
48 3
        $this->setPreprocessor('prev', function ($value) {
49 3
            return array_map(function ($val) {
50 27
                return htmlspecialchars(strip_tags($val));
51 27
            }, $value);
52
        });
53 27
    }
54
55 27
    protected function preprocessValue($key, $value)
56 27
    {
57
        $preprocessor = $this->preprocessors[$key] ?? $this->preprocessors['default'];
58
        return $preprocessor($value);
59
    }
60
}
61