ConditionalResponse   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isModified() 0 3 2
A getOutput() 0 12 3
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