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 ( 1d2db3...34818a )
by Cees-Jan
02:04
created

Client::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 3
Ratio 30 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

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