AbstractLinkHandler::getAttributeForHeader()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 2
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\LaminasLinkHeadersModule\Listener;
6
7
use function addslashes;
8
use function array_map;
9
use ArrayIterator;
10
use function count;
11
use function implode;
12
use function in_array;
13
use function iterator_to_array;
14
use Laminas\Http\Header\GenericMultiHeader;
15
use Laminas\Http\Header\HeaderInterface;
16
use Laminas\Http\Header\HeaderValue;
17
use Laminas\Http\PhpEnvironment\Response;
18
use function sprintf;
19
use function strtolower;
20
21
abstract class AbstractLinkHandler implements LinkHandlerInterface
22
{
23
    protected const RFC_PARAMS = [
24
        'rel',
25
        'anchor',
26
        'rev',
27
        'hreflang',
28
        'media',
29
        'title',
30
        'type',
31
    ];
32
33
    /**
34
     * Get the attribute string for the header
35
     *
36
     * @param string $name
37
     * @param string $value
38
     *
39
     * @return string
40
     */
41 12
    protected function getAttributeForHeader(string $name, $value): string
42
    {
43 12
        $name = strtolower($name);
44
        // all RFC params must have a value, but not link-extensions
45 12
        if (null === $value && ! in_array($name, self::RFC_PARAMS, true)) {
46 7
            return $name;
47
        }
48
49 12
        return sprintf('%s="%s"', $name, addslashes(HeaderValue::filter($value ?: null)));
50
    }
51
52
    /**
53
     * Returns the link header
54
     *
55
     * @param array $attributes
56
     *
57
     * @return string
58
     */
59 12
    protected function createLinkHeaderValue(array $attributes): string
60
    {
61 12
        $href = $attributes['href'];
62 12
        unset($attributes['href']);
63
64 12
        $attributeLines = [];
65 12
        foreach ($attributes as $name => $value) {
66 12
            $attributeLines[] = $this->getAttributeForHeader($name, $value);
67
        }
68
69 12
        return sprintf('<%s>; %s', $href, implode('; ', $attributeLines));
70
    }
71
72
    /**
73
     * @param Response $response
74
     * @param string[] $values
75
     */
76 15
    protected function addLinkHeader(Response $response, array $values): void
77
    {
78 15
        if (! count($values)) {
79 3
            return;
80
        }
81
82 12
        $header = new GenericMultiHeader('Link', implode(', ', $values));
83 12
        $currentHeader = $response->getHeaders()->get($header->getFieldName());
84
85
        /** @var HeaderInterface[] $headers */
86 12
        $headers = [];
87 12
        if ($currentHeader instanceof ArrayIterator) {
88 1
            $headers = iterator_to_array($currentHeader);
89 11
        } elseif (false !== $currentHeader) {
90 3
            $headers[] = $currentHeader;
91
        }
92
93 12
        foreach ($headers as $headerItem) {
94 4
            $response->getHeaders()->removeHeader($headerItem);
95
        }
96
97 12
        $headers[] = $header;
98
99 12
        $headerValues = array_map(
100
            static function (HeaderInterface $header) {
101 12
                return $header->getFieldValue();
102 12
            },
103 12
            $headers
104
        );
105
106 12
        $response->getHeaders()->addHeader(new GenericMultiHeader($header->getFieldName(), implode(', ', $headerValues)));
107 12
    }
108
}
109