Completed
Push — psalm ( 804131...7e613f )
by Akihito
01:43
created

CliHttpCache   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 69
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transfer() 0 4 1
A isNotModified() 0 9 2
A getServer() 0 12 2
A getServerKey() 0 4 1
A getEtag() 0 11 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface;
8
9
use function assert;
10
use function is_string;
11
use function parse_str;
12
use function sprintf;
13
use function str_replace;
14
use function strtoupper;
15
16
use const PHP_EOL;
17
18
final class CliHttpCache implements HttpCacheInterface
19
{
20
    /** @var ResourceStorageInterface */
21
    private $storage;
22
23
    public function __construct(ResourceStorageInterface $storage)
24
    {
25
        $this->storage = $storage;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function isNotModified(array $server): bool
32
    {
33
        $etag = $this->getEtag($server);
34
        if ($etag === null) {
35
            return false;
36
        }
37
38
        return $this->storage->hasEtag($etag);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * @return void
45
     */
46
    public function transfer()
47
    {
48
        echo '304 Not Modified' . PHP_EOL . PHP_EOL;
49
    }
50
51
    /**
52
     * @return array<string, string>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
53
     */
54
    private function getServer(string $query): array
55
    {
56
        parse_str($query, $headers);
57
        $server = [];
58
        foreach ($headers as $key => $header) {
0 ignored issues
show
Bug introduced by
The expression $headers of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
59
            assert(is_string($header));
60
            assert(is_string($key));
61
            $server[$this->getServerKey($key)] = $header;
62
        }
63
64
        return $server;
65
    }
66
67
    private function getServerKey(string $key): string
68
    {
69
        return sprintf('HTTP_%s', strtoupper(str_replace('-', '_', $key)));
70
    }
71
72
    /**
73
     * @param array<string, mixed> $server
74
     */
75
    private function getEtag(array $server): ?string
76
    {
77
        $hasRequestHeaderInCli = isset($server['argc']) && $server['argc'] === 4 && isset($server['argv'][3]);
78
        if ($hasRequestHeaderInCli) {
79
            $server = $this->getServer((string) $server['argv'][3]);
80
        }
81
82
        $hasValidEtag = isset($server['HTTP_IF_NONE_MATCH']) && is_string($server['HTTP_IF_NONE_MATCH']);
83
84
        return $hasValidEtag ? $server['HTTP_IF_NONE_MATCH'] : null;
85
    }
86
}
87