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.
Passed
Pull Request — master (#4)
by Cees-Jan
04:10
created

CacheConfiguration::responseIsCacheable()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 2
nop 2
crap 4
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
     * @param array      $urls
48
     * @param array      $headers
49
     * @param Clock|null $clock
50
     */
51 2
    public function __construct(array $urls, array $headers = [], Clock $clock = null)
52
    {
53 2
        $this->sortUrls($urls);
54 2
        $this->headers = $headers;
55 2
        $this->clock = $clock instanceof Clock ? $clock : new SystemClock();
56 2
    }
57
58 2
    public function requestIsCacheable(ServerRequestInterface $request): bool
59
    {
60 2
        if ($request->getMethod() !== 'GET') {
61
            return false;
62
        }
63
64
        if (
65 2
            class_exists(SessionMiddleware::class) &&
66 2
            $request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME) !== null &&
67 2
            $request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME)->isActive() === true
68
        ) {
69 1
            return false;
70
        }
71
72 2
        $uri = $request->getUri()->getPath();
73 2
        if (!\in_array($uri, $this->staticUrls, true) && !$this->matchesPrefixUrl($uri)) {
74 1
            return false;
75
        }
76
77 2
        return true;
78
    }
79
80 2
    public function responseIsCacheable(ServerRequestInterface $request, ResponseInterface $response): bool
81
    {
82
        if (
83 2
            class_exists(SessionMiddleware::class) &&
84 2
            $request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME) !== null &&
85 2
            $request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME)->isActive() === true
86
        ) {
87 1
            return false;
88
        }
89
90 1
        return true;
91
    }
92
93 2
    public function cacheKey(ServerRequestInterface $request): string
94
    {
95 2
        $key = $request->getUri()->getPath();
96 2
        $query = $request->getUri()->getQuery();
97 2
        if (\strlen($query) > 0 && $this->queryInKey($key)) {
98 1
            $key .= '?' . $query;
99
        }
100
101 2
        return $key;
102
    }
103
104 1
    public function cacheEncode(ResponseInterface $response): string
105
    {
106 1
        $headers = [];
107 1
        foreach ($this->headers as $header) {
108 1
            if (!$response->hasHeader($header)) {
109
                continue;
110
            }
111
112 1
            $headers[$header] = $response->getHeaderLine($header);
113
        }
114
115 1
        return msgpack_pack([
116 1
            'code' => $response->getStatusCode(),
117 1
            'time' => (int)$this->clock->now()->format('U'),
118 1
            'headers' => $headers,
119 1
            'body' => (string)$response->getBody(),
120
        ]);
121
    }
122
123 1
    public function cacheDecode(string $response): ResponseInterface
124
    {
125 1
        $response = msgpack_unpack($response);
126 1
        $response['headers'] = (array)$response['headers'];
127 1
        $response['headers']['Age'] = (int)$this->clock->now()->format('U') - (int)$response['time'];
128
129 1
        return new Response($response['code'], $response['headers'], stream_for($response['body']));
130
    }
131
132 2
    private function sortUrls(array $urls)
133
    {
134 2
        foreach ($urls as $url) {
135 2
            if (!(\strlen($url) >= 3 && \in_array(substr($url, -3), self::PREFIXES, true))) {
136 2
                $this->staticUrls[] = $url;
137
138 2
                continue;
139
            }
140
141 1
            if (\strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITHOUT_QUERY) {
142 1
                $this->prefixUrlsWithoutQuery[] = substr($url, 0, -3);
143
144 1
                continue;
145
            }
146
147 1
            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 2
    }
154
155 1
    private function matchesPrefixUrl(string $uri): bool
156
    {
157 1
        if ($this->urlMatchesPrefixes($this->prefixUrlsWithoutQuery, $uri)) {
158 1
            return true;
159
        }
160
161 1
        return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri);
162
    }
163
164 1
    private function queryInKey(string $uri): bool
165
    {
166 1
        return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri);
167
    }
168
169 1
    private function urlMatchesPrefixes(array $urls, string $uri): bool
170
    {
171 1
        foreach ($urls as $url) {
172 1
            if (strpos($uri, $url) === 0) {
173 1
                return true;
174
            }
175
        }
176
177 1
        return false;
178
    }
179
}
180