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
02:36 queued 24s
created

CacheConfiguration::cacheEncode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 3.009
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): array
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
        return [
116 1
            'body' => (string)$response->getBody(),
117 1
            'headers' => $headers,
118 1
            'code' => $response->getStatusCode(),
119 1
            'time' => (int)$this->clock->now()->format('U'),
120
        ];
121
    }
122
123 1
    public function cacheDecode(array $response): ResponseInterface
124
    {
125 1
        $response['headers'] = (array)$response['headers'];
126 1
        $response['headers']['Age'] = (int)$this->clock->now()->format('U') - (int)$response['time'];
127
128 1
        return new Response($response['code'], $response['headers'], stream_for($response['body']));
0 ignored issues
show
Bug introduced by
It seems like $response['body'] can also be of type array<mixed,mixed|integer> and array; however, parameter $resource of RingCentral\Psr7\stream_for() does only seem to accept resource|string|Psr\Http\Message\StreamInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        return new Response($response['code'], $response['headers'], stream_for(/** @scrutinizer ignore-type */ $response['body']));
Loading history...
Bug introduced by
It seems like $response['code'] can also be of type array<mixed,mixed|integer> and array; however, parameter $status of RingCentral\Psr7\Response::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        return new Response(/** @scrutinizer ignore-type */ $response['code'], $response['headers'], stream_for($response['body']));
Loading history...
129
    }
130
131 2
    private function sortUrls(array $urls)
132
    {
133 2
        foreach ($urls as $url) {
134 2
            if (!(\strlen($url) >= 3 && \in_array(substr($url, -3), self::PREFIXES, true))) {
135 2
                $this->staticUrls[] = $url;
136
137 2
                continue;
138
            }
139
140 1
            if (\strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITHOUT_QUERY) {
141 1
                $this->prefixUrlsWithoutQuery[] = substr($url, 0, -3);
142
143 1
                continue;
144
            }
145
146 1
            if (\strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITH_QUERY) {
147 1
                $this->prefixUrlsWithQuery[] = substr($url, 0, -3);
148
149 1
                continue;
150
            }
151
        }
152 2
    }
153
154 1
    private function matchesPrefixUrl(string $uri): bool
155
    {
156 1
        if ($this->urlMatchesPrefixes($this->prefixUrlsWithoutQuery, $uri)) {
157 1
            return true;
158
        }
159
160 1
        return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri);
161
    }
162
163 1
    private function queryInKey(string $uri): bool
164
    {
165 1
        return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri);
166
    }
167
168 1
    private function urlMatchesPrefixes(array $urls, string $uri): bool
169
    {
170 1
        foreach ($urls as $url) {
171 1
            if (strpos($uri, $url) === 0) {
172 1
                return true;
173
            }
174
        }
175
176 1
        return false;
177
    }
178
}
179