Completed
Push — 1.x ( f091f0...81f5a7 )
by Akihito
20s
created

ConditionalResponse::getOutput()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
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
final class ConditionalResponse implements ConditionalResponseInterface
10
{
11
    /**
12
     * @see https://tools.ietf.org/html/rfc7232#section-4.1
13
     */
14
    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 $originalHeaders) : Output
35
    {
36
        $headers = [];
37
        foreach ($originalHeaders as $label => $value) {
38
            if (! in_array($label, self::HEADER_IN_304, true)) {
39
                continue;
40
            }
41
            $headers[$label] = $value;
42
        }
43
44
        return new Output(304, $headers, '');
45
    }
46
}
47