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 (#4)
by Cees-Jan
08:41
created

CacheConfiguration::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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