ConditionalResponse::getOutput()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Sunday\Provide\Transfer;
6
7
use BEAR\Resource\ResourceObject;
8
9
use function in_array;
10
11
final class ConditionalResponse implements ConditionalResponseInterface
12
{
13
    /** @see https://tools.ietf.org/html/rfc7232#section-4.1 */
14
    private const HEADER_IN_304 = [
15
        'Cache-Control',
16
        'Content-Location',
17
        'Date',
18
        'ETag',
19
        'Expires',
20
        'Vary',
21
    ];
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function isModified(ResourceObject $ro, array $server): bool
27
    {
28
        return ! (isset($server['HTTP_IF_NONE_MATCH'], $ro->headers['ETag']) && $server['HTTP_IF_NONE_MATCH'] === $ro->headers['ETag']);
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function getOutput(array $headers): Output
35
    {
36
        $newHeaders = [];
37
        foreach ($headers as $label => $value) {
38
            if (! in_array($label, self::HEADER_IN_304, true)) {
39
                continue;
40
            }
41
42
            $newHeaders[$label] = $value;
43
        }
44
45
        return new Output(304, $newHeaders, '');
46
    }
47
}
48