GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#5)
by Cees-Jan
07:46
created

CacheConfiguration::cacheKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Lcobucci\Clock\Clock;
6
use Lcobucci\Clock\SystemClock;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use RingCentral\Psr7\Response;
10
use function RingCentral\Psr7\stream_for;
11
12
final class CacheConfiguration implements CacheConfigurationInterface
13
{
14
    private const PREFIX_WITHOUT_QUERY = '***';
15
    private const PREFIX_WITH_QUERY = '???';
16
    private const PREFIXES = [
17
        self::PREFIX_WITH_QUERY,
18
        self::PREFIX_WITHOUT_QUERY,
19
    ];
20
21
    /**
22
     * @var array
23
     */
24
    private $staticUrls = [];
25
26
    /**
27
     * @var array
28
     */
29
    private $prefixUrlsWithoutQuery = [];
30
31
    /**
32
     * @var array
33
     */
34
    private $prefixUrlsWithQuery = [];
35
36
    /**
37
     * @var array
38
     */
39
    private $headers;
40
41
    /**
42
     * @var Clock
43
     */
44
    private $clock;
45
46
    /**
47
     * @var null|callable
48
     */
49
    private $ttl;
50
51 1
    /**
52
     * @param array $urls
53 1
     * @param array $headers
54 1
     * @param Clock|null $clock
55 1
     * @param callable|null $ttl
56 1
     */
57
    public function __construct(array $urls, array $headers = [], Clock $clock = null, callable $ttl = null)
58 1
    {
59
        $this->sortUrls($urls);
60 1
        $this->headers = $headers;
61
        $this->clock = $clock instanceof Clock ? $clock : new SystemClock();
62
        $this->ttl = $ttl;
63
    }
64 1
65 1
    public function requestIsCacheable(ServerRequestInterface $request): bool
66
    {
67
        if ($request->getMethod() !== 'GET') {
68
            return false;
69 1
        }
70
71
        $uri = $request->getUri()->getPath();
72 1
        if (!\in_array($uri, $this->staticUrls, true) && !$this->matchesPrefixUrl($uri)) {
73
            return false;
74 1
        }
75
76
        return true;
77 1
    }
78
79 1
    public function responseIsCacheable(ServerRequestInterface $request, ResponseInterface $response): bool
80 1
    {
81 1
        return true;
82 1
    }
83
84
    public function cacheKey(ServerRequestInterface $request): string
85 1
    {
86
        $key = $request->getUri()->getPath();
87
        $query = $request->getUri()->getQuery();
88 1
        if (\strlen($query) > 0 && $this->queryInKey($key)) {
89
            $key .= '?' . $query;
90 1
        }
91 1
92 1
        return $key;
93
    }
94
95
    public function cacheTtl(ServerRequestInterface $request, ResponseInterface $response): ?int
96 1
    {
97
        if ($this->ttl === null) {
98
            return null;
99 1
        }
100 1
101 1
        return ($this->ttl)($request, $response);
102 1
    }
103 1
104
    public function cacheEncode(ResponseInterface $response): string
105
    {
106
        $headers = [];
107 1
        foreach ($this->headers as $header) {
108
            if (!$response->hasHeader($header)) {
109 1
                continue;
110 1
            }
111 1
112
            $headers[$header] = $response->getHeaderLine($header);
113 1
        }
114
115
        return msgpack_pack([
116 1
            'code' => $response->getStatusCode(),
117
            'time' => (int)$this->clock->now()->format('U'),
118 1
            'headers' => $headers,
119 1
            'body' => (string)$response->getBody(),
120 1
        ]);
121
    }
122 1
123
    public function cacheDecode(string $response): ResponseInterface
124
    {
125 1
        $response = msgpack_unpack($response);
126 1
        $response['headers'] = (array)$response['headers'];
127
        $response['headers']['Age'] = (int)$this->clock->now()->format('U') - (int)$response['time'];
128 1
129
        return new Response($response['code'], $response['headers'], stream_for($response['body']));
130
    }
131 1
132 1
    private function sortUrls(array $urls)
133
    {
134 1
        foreach ($urls as $url) {
135
            if (!(\strlen($url) >= 3 && \in_array(substr($url, -3), self::PREFIXES, true))) {
136
                $this->staticUrls[] = $url;
137 1
138
                continue;
139 1
            }
140
141 1
            if (\strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITHOUT_QUERY) {
142 1
                $this->prefixUrlsWithoutQuery[] = substr($url, 0, -3);
143
144
                continue;
145 1
            }
146
147
            if (\strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITH_QUERY) {
148 1
                $this->prefixUrlsWithQuery[] = substr($url, 0, -3);
149
150 1
                continue;
151
            }
152
        }
153 1
    }
154
155 1
    private function matchesPrefixUrl(string $uri): bool
156 1
    {
157 1
        if ($this->urlMatchesPrefixes($this->prefixUrlsWithoutQuery, $uri)) {
158
            return true;
159
        }
160
161 1
        return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri);
162
    }
163
164
    private function queryInKey(string $uri): bool
165
    {
166
        return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri);
167
    }
168
169
    private function urlMatchesPrefixes(array $urls, string $uri): bool
170
    {
171
        foreach ($urls as $url) {
172
            if (strpos($uri, $url) === 0) {
173
                return true;
174
            }
175
        }
176
177
        return false;
178
    }
179
}
180