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
Push — master ( f9a93a...d93906 )
by Cees-Jan
07:26
created

Client::stripExtraSlashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ApiClients\Foundation\Transport;
5
6
use ApiClients\Foundation\Hydrator\Factory as HydratorFactory;
7
use ApiClients\Foundation\Hydrator\Hydrator;
8
use GuzzleHttp\Client as GuzzleClient;
9
use GuzzleHttp\Psr7\Request;
10
use GuzzleHttp\Psr7\Response;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\UriInterface;
14
use React\Cache\CacheInterface;
15
use React\EventLoop\LoopInterface;
16
use React\Promise\Deferred;
17
use React\Promise\PromiseInterface;
18
use function React\Promise\reject;
19
use React\Promise\RejectedPromise;
20
use function React\Promise\resolve;
21
use function WyriHaximus\React\futureFunctionPromise;
22
23
class Client
24
{
25
    const DEFAULT_OPTIONS = [
26
        Options::SCHEMA => 'https',
27
        Options::PATH => '/',
28
        Options::USER_AGENT => 'WyriHaximus/php-api-client',
29
        Options::HEADERS => [],
30
    ];
31
32
    /**
33
     * @var GuzzleClient
34
     */
35
    protected $handler;
36
37
    /**
38
     * @var LoopInterface
39
     */
40
    protected $loop;
41
42
    /**
43
     * @var array
44
     */
45
    protected $options = [];
46
47
    /**
48
     * @var Hydrator
49
     */
50
    protected $hydrator;
51
52
    /**
53
     * @var CacheInterface
54
     */
55
    protected $cache;
56
57
    /**
58
     * @param LoopInterface $loop
59
     * @param GuzzleClient $handler
60
     * @param array $options
61
     */
62 12
    public function __construct(LoopInterface $loop, GuzzleClient $handler, array $options = [])
63
    {
64 12
        $this->loop = $loop;
65 12
        $this->handler = $handler;
66 12
        $this->options = $options + self::DEFAULT_OPTIONS;
67 12 View Code Duplication
        if (isset($this->options[Options::CACHE]) && $this->options[Options::CACHE] instanceof CacheInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68 3
            $this->cache = $this->options[Options::CACHE];
69
        }
70 12
        $this->hydrator = $this->determineHydrator();
71 12
    }
72
73
    /**
74
     * @return Hydrator
75
     * @throws \Exception
76
     */
77 12
    protected function determineHydrator(): Hydrator
78
    {
79 12 View Code Duplication
        if (isset($this->options[Options::HYDRATOR]) && $this->options[Options::HYDRATOR] instanceof Hydrator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            return $this->options[Options::HYDRATOR];
81
        }
82
83 12
        if (!isset($this->options[Options::HYDRATOR_OPTIONS])) {
84
            throw new \Exception('Missing Hydrator options');
85
        }
86
87 12
        return HydratorFactory::create($this->options[Options::HYDRATOR_OPTIONS]);
88
    }
89
90
    /**
91
     * @param string $path
92
     * @param bool $refresh
93
     * @return PromiseInterface
94
     */
95 5
    public function request(string $path, bool $refresh = false): PromiseInterface
96
    {
97
        return $this->requestRaw($path, $refresh)->then(function ($json) {
98 3
            return $this->jsonDecode($json);
99 5
        });
100
    }
101
102
    /**
103
     * @param string $path
104
     * @param bool $refresh
105
     * @return PromiseInterface
106
     */
107 5
    public function requestRaw(string $path, bool $refresh = false): PromiseInterface
108
    {
109 5
        return $this->requestPsr7(
110 5
            $this->createRequest($path),
111
            $refresh
112
        )->then(function ($response) {
113 3
            return resolve($response->getBody()->getContents());
114 5
        });
115
    }
116
117
    /**
118
     * @param UriInterface $uri
119
     * @return PromiseInterface
120
     */
121 3
    protected function checkCache(UriInterface $uri): PromiseInterface
122
    {
123 3
        if (!($this->cache instanceof CacheInterface)) {
124 1
            return reject();
125
        }
126
127 2
        $key = $this->determineCacheKey($uri);
128
        return $this->cache->get($key)->then(function ($document) {
129 1
            $document = json_decode($document, true);
130 1
            $response = new Response(
131 1
                $document['status_code'],
132 1
                $document['headers'],
133 1
                $document['body'],
134 1
                $document['protocol_version'],
135 1
                $document['reason_phrase']
136
            );
137
138 1
            return resolve($response);
139 2
        });
140
    }
141
142
    /**
143
     * @param RequestInterface $request
144
     * @param ResponseInterface $response
145
     */
146 2
    protected function storeCache(RequestInterface $request, ResponseInterface $response)
147
    {
148 2
        if (!($this->cache instanceof CacheInterface)) {
149
            return;
150
        }
151
152
        $document = [
153 2
            'body' => $response->getBody()->getContents(),
154 2
            'headers' => $response->getHeaders(),
155 2
            'protocol_version' => $response->getProtocolVersion(),
156 2
            'reason_phrase' => $response->getReasonPhrase(),
157 2
            'status_code' => $response->getStatusCode(),
158
        ];
159
160 2
        $key = $this->determineCacheKey($request->getUri());
161
162 2
        $this->cache->set($key, json_encode($document));
163 2
    }
164
165
    /**
166
     * @param UriInterface $uri
167
     * @return string
168
     */
169 3
    protected function determineCacheKey(UriInterface $uri): string
170
    {
171 3
        return $this->stripExtraSlashes(
172
            implode(
173 3
                '/',
174
                [
175 3
                    $uri->getScheme(),
176 3
                    $uri->getHost(),
177 3
                    $uri->getPort(),
178 3
                    $uri->getPath(),
179 3
                    md5($uri->getQuery()),
180
                ]
181
            )
182
        );
183
    }
184
185
    /**
186
     * @param string $string
187
     * @return string
188
     */
189 3
    protected function stripExtraSlashes(string $string): string
190
    {
191 3
        return preg_replace('#/+#', '/', $string);
192
    }
193
194
    /**
195
     * @param RequestInterface $request
196
     * @param bool $refresh
197
     * @return PromiseInterface
198
     */
199 5
    public function requestPsr7(RequestInterface $request, bool $refresh = false): PromiseInterface
200
    {
201 5
        $promise = new RejectedPromise();
202
203 5
        if (!$refresh) {
204 3
            $promise = $this->checkCache($request->getUri());
205
        }
206
207
        return $promise->otherwise(function () use ($request) {
208 4
            $deferred = new Deferred();
209
210 4
            $this->handler->sendAsync(
211
                $request
212
            )->then(function (ResponseInterface $response) use ($deferred, $request) {
213 2
                $contents = $response->getBody()->getContents();
214 2
                $this->storeCache(
215
                    $request,
216 2
                    new Response(
217 2
                        $response->getStatusCode(),
218 2
                        $response->getHeaders(),
219
                        $contents,
220 2
                        $response->getProtocolVersion(),
221 2
                        $response->getReasonPhrase()
222
                    )
223
                );
224 2
                $deferred->resolve(
225 2
                    new Response(
226 2
                        $response->getStatusCode(),
227 2
                        $response->getHeaders(),
228
                        $contents,
229 2
                        $response->getProtocolVersion(),
230 2
                        $response->getReasonPhrase()
231
                    )
232
                );
233
            }, function ($error) use ($deferred) {
234
                $deferred->reject($error);
235 4
            });
236
237 4
            return $deferred->promise();
238 5
        });
239
    }
240
241
    /**
242
     * @param string $path
243
     * @return RequestInterface
244
     */
245 5
    protected function createRequest(string $path): RequestInterface
246
    {
247 5
        $url = $this->getBaseURL() . $path;
248 5
        $headers = $this->getHeaders();
249 5
        return new Request('GET', $url, $headers);
250
    }
251
252
    /**
253
     * @return array
254
     */
255 5
    public function getHeaders(): array
256
    {
257
        $headers = [
258 5
            'User-Agent' => $this->options[Options::USER_AGENT],
259
        ];
260 5
        $headers += $this->options[Options::HEADERS];
261 5
        return $headers;
262
    }
263
264
    /**
265
     * @param string $json
266
     * @return PromiseInterface
267
     */
268
    public function jsonDecode(string $json): PromiseInterface
269
    {
270 3
        return futureFunctionPromise($this->loop, $json, function ($json) {
271 3
            return json_decode($json, true);
272 3
        });
273
    }
274
275
    /**
276
     * @return Hydrator
277
     */
278 1
    public function getHydrator(): Hydrator
279
    {
280 1
        return $this->hydrator;
281
    }
282
283
    /**
284
     * @return LoopInterface
285
     */
286 3
    public function getLoop(): LoopInterface
287
    {
288 3
        return $this->loop;
289
    }
290
291
    /**
292
     * @return string
293
     */
294 8
    public function getBaseURL(): string
295
    {
296 8
        return $this->options[Options::SCHEMA] . '://' . $this->options[Options::HOST] . $this->options[Options::PATH];
297
    }
298
}
299