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.
Test Failed
Push — master ( 82f96f...07aa43 )
by Yong
05:43
created

Request::getSignatureNonce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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