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 (#81)
by Yong
05:10
created

Request::createClient()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use AlibabaCloud\Client\AlibabaCloud;
6
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
7
use AlibabaCloud\Client\Exception\ClientException;
8
use AlibabaCloud\Client\Exception\ServerException;
9
use AlibabaCloud\Client\Filter\ApiFilter;
10
use AlibabaCloud\Client\Filter\ClientFilter;
11
use AlibabaCloud\Client\Filter\Filter;
12
use AlibabaCloud\Client\Filter\HttpFilter;
13
use AlibabaCloud\Client\Request\Traits\AcsTrait;
14
use AlibabaCloud\Client\Request\Traits\ClientTrait;
15
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait;
16
use AlibabaCloud\Client\Request\Traits\MagicTrait;
17
use AlibabaCloud\Client\Result\Result;
18
use AlibabaCloud\Client\SDK;
19
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
20
use AlibabaCloud\Client\Traits\HttpTrait;
21
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
22
use AlibabaCloud\Client\Traits\RegionTrait;
23
use GuzzleHttp\Client;
24
use GuzzleHttp\Exception\GuzzleException;
25
use GuzzleHttp\HandlerStack;
26
use GuzzleHttp\Middleware;
27
use GuzzleHttp\Psr7\Uri;
28
29
/**
30
 * Class Request
31
 *
32
 * @package   AlibabaCloud\Client\Request
33
 *
34
 * @method string resolveParameters($credential)
35
 */
36
abstract class Request implements \ArrayAccess
37
{
38
    use DeprecatedTrait;
39
    use HttpTrait;
40
    use RegionTrait;
41
    use MagicTrait;
42
    use ClientTrait;
43
    use AcsTrait;
44
    use ArrayAccessTrait;
45
    use ObjectAccessTrait;
46
47
    /**
48
     * Request Connect Timeout
49
     */
50
    const CONNECT_TIMEOUT = 3;
51
52
    /**
53
     * Request Timeout
54
     */
55
    const TIMEOUT = 15;
56
57
    /**
58
     * @var string
59
     */
60
    public $scheme = 'http';
61
62
    /**
63
     * @var string
64
     */
65
    public $method = 'GET';
66
67
    /**
68
     * @var string
69
     */
70
    public $format = 'JSON';
71
72
    /**
73
     * @var string
74
     */
75
    public $client;
76
77
    /**
78
     * @var Uri
79
     */
80
    public $uri;
81
82
    /**
83
     * @var array The original parameters of the request.
84
     */
85
    public $data = [];
86
87
    /**
88
     * @var string
89
     */
90
    protected $stringToBeSigned = '';
91
92
    /**
93
     * @var array
94
     */
95
    private $userAgent = [];
96
97
    /**
98
     * Request constructor.
99
     *
100
     * @param array $options
101
     *
102
     * @throws ClientException
103
     */
104 190
    public function __construct(array $options = [])
105
    {
106 190
        $this->client                     = CredentialsProvider::getDefaultName();
107 190
        $this->uri                        = new Uri();
108 190
        $this->uri                        = $this->uri->withScheme($this->scheme);
109 190
        $this->options['http_errors']     = false;
110 190
        $this->options['connect_timeout'] = self::CONNECT_TIMEOUT;
111 190
        $this->options['timeout']         = self::TIMEOUT;
112 190
        if ($options !== []) {
113 1
            $this->options($options);
114 1
        }
115
116 190
        if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') {
117 1
            $this->options['debug'] = true;
118 1
        }
119 190
    }
120
121
    /**
122
     * @param string $name
123
     * @param string $value
124
     *
125
     * @return $this
126
     * @throws ClientException
127
     */
128 6
    public function appendUserAgent($name, $value)
129
    {
130 6
        Filter::name($name);
131
132 3
        Filter::value($value);
133
134 1
        if (!UserAgent::isGuarded($name)) {
135 1
            $this->userAgent[$name] = $value;
136 1
        }
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * @param array $userAgent
143
     *
144
     * @return $this
145
     */
146 2
    public function withUserAgent(array $userAgent)
147
    {
148 2
        $this->userAgent = UserAgent::clean($userAgent);
149
150 2
        return $this;
151
    }
152
153
    /**
154
     * Set the response data format.
155
     *
156
     * @param string $format
157
     *
158
     * @return $this
159
     * @throws ClientException
160
     */
161 19
    public function format($format)
162
    {
163 19
        ApiFilter::format($format);
164
165 15
        $this->format = \strtoupper($format);
166
167 15
        return $this;
168
    }
169
170
    /**
171
     * Set the request body.
172
     *
173
     * @param string $body
174
     *
175
     * @return $this
176
     * @throws ClientException
177
     */
178 7
    public function body($body)
179
    {
180 7
        HttpFilter::body($body);
181
182 5
        $this->options['body'] = $body;
183
184 5
        return $this;
185
    }
186
187
    /**
188
     * Set the json as body.
189
     *
190
     * @param array|object $content
191
     *
192
     * @return $this
193
     * @throws ClientException
194
     */
195 3
    public function jsonBody($content)
196
    {
197 3
        if (!\is_array($content) && !\is_object($content)) {
198 1
            throw new ClientException(
199 1
                'jsonBody only accepts an array or object',
200
                SDK::INVALID_ARGUMENT
201 1
            );
202
        }
203
204 2
        return $this->body(\json_encode($content));
205
    }
206
207
    /**
208
     * Set the request scheme.
209
     *
210
     * @param string $scheme
211
     *
212
     * @return $this
213
     * @throws ClientException
214
     */
215 16
    public function scheme($scheme)
216
    {
217 16
        HttpFilter::scheme($scheme);
218
219 14
        $this->scheme = \strtolower($scheme);
220 14
        $this->uri    = $this->uri->withScheme($this->scheme);
221
222 14
        return $this;
223
    }
224
225
    /**
226
     * Set the request host.
227
     *
228
     * @param string $host
229
     *
230
     * @return $this
231
     * @throws ClientException
232
     */
233 25
    public function host($host)
234
    {
235 25
        HttpFilter::host($host);
236
237 23
        $this->uri = $this->uri->withHost($host);
238
239 23
        return $this;
240
    }
241
242
    /**
243
     * @param string $method
244
     *
245
     * @return $this
246
     * @throws ClientException
247
     */
248 50
    public function method($method)
249
    {
250 50
        $this->method = HttpFilter::method($method);
251
252 48
        return $this;
253
    }
254
255
    /**
256
     * @param string $clientName
257
     *
258
     * @return $this
259
     * @throws ClientException
260
     */
261 67
    public function client($clientName)
262
    {
263 67
        ClientFilter::clientName($clientName);
264
265 65
        $this->client = $clientName;
266
267 65
        return $this;
268
    }
269
270
    /**
271
     * @return bool
272
     * @throws ClientException
273
     */
274 1
    public function isDebug()
275
    {
276 1
        if (isset($this->options['debug'])) {
277 1
            return $this->options['debug'] === true;
278
        }
279
280 1
        if (isset($this->httpClient()->options['debug'])) {
281 1
            return $this->httpClient()->options['debug'] === true;
282
        }
283
284 1
        return false;
285
    }
286
287
    /**
288
     * @return Result
289
     * @throws ClientException
290
     * @throws ServerException
291
     */
292 61
    public function request()
293
    {
294 61
        $this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent);
295
296 61
        $this->removeRedundantQuery();
297 61
        $this->removeRedundantHeaders();
298 61
        $this->removeRedundantHFormParams();
299 61
        $this->resolveUri();
300 58
        $this->resolveParameters($this->credential());
301
302 55
        if (isset($this->options['form_params'])) {
303 26
            $this->options['form_params'] = \GuzzleHttp\Psr7\parse_query(
304 26
                self::getPostHttpBody($this->options['form_params'])
305 26
            );
306 26
        }
307
308 55
        $this->mergeOptionsIntoClient();
309
310 55
        $result = new Result($this->response(), $this);
311
312 51
        if (!$result->isSuccess()) {
313 18
            throw new ServerException($result);
314
        }
315
316 34
        return $result;
317
    }
318
319
    /**
320
     * Remove redundant Query
321
     *
322
     * @codeCoverageIgnore
323
     */
324
    private function removeRedundantQuery()
325
    {
326
        if (isset($this->options['query']) && $this->options['query'] === []) {
327
            unset($this->options['query']);
328
        }
329
    }
330
331
    /**
332
     * Remove redundant Headers
333
     *
334
     * @codeCoverageIgnore
335
     */
336
    private function removeRedundantHeaders()
337
    {
338
        if (isset($this->options['headers']) && $this->options['headers'] === []) {
339
            unset($this->options['headers']);
340
        }
341
    }
342
343
    /**
344
     * Remove redundant Headers
345
     *
346
     * @codeCoverageIgnore
347
     */
348
    private function removeRedundantHFormParams()
349
    {
350
        if (isset($this->options['form_params']) && $this->options['form_params'] === []) {
351
            unset($this->options['form_params']);
352
        }
353
    }
354
355
    /**
356
     * @param array $post
357
     *
358
     * @return bool|string
359
     */
360 29
    public static function getPostHttpBody(array $post)
361
    {
362 29
        $content = '';
363 29
        foreach ($post as $apiKey => $apiValue) {
364 29
            $content .= "$apiKey=" . urlencode($apiValue) . '&';
365 29
        }
366
367 29
        return substr($content, 0, -1);
368
    }
369
370
    /**
371
     * @return Client
372
     */
373 65
    public static function createClient()
374
    {
375 65
        if (AlibabaCloud::hasMock()) {
376 17
            $stack = HandlerStack::create(AlibabaCloud::getMock());
377 17
        } else {
378 48
            $stack = HandlerStack::create();
379
        }
380
381 65
        if (AlibabaCloud::isRememberHistory()) {
382 1
            $stack->push(Middleware::history(AlibabaCloud::referenceHistory()));
383 1
        }
384
385 65
        return new Client([
386 65
                              'handler' => $stack,
387 65
                          ]);
388
    }
389
390
    /**
391
     * @throws ClientException
392
     */
393 55
    private function response()
394
    {
395
        try {
396 55
            return self::createClient()->request(
397 55
                $this->method,
398 55
                (string)$this->uri,
399 55
                $this->options
400 55
            );
401 5
        } catch (GuzzleException $e) {
402 4
            throw new ClientException(
403 4
                $e->getMessage(),
404 4
                SDK::SERVER_UNREACHABLE,
405
                $e
406 4
            );
407
        }
408
    }
409
410
    /**
411
     * @return string
412
     */
413 20
    public function stringToBeSigned()
414
    {
415 20
        return $this->stringToBeSigned;
416
    }
417
}
418