ConditionalResponse::getOutput()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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