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 ( 37d8a4...bd815b )
by Yong
05:40
created

Request::createClient()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 14
ccs 6
cts 6
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
    public function __construct(array $options = [])
105
    {
106
        $this->client                     = CredentialsProvider::getDefaultName();
107
        $this->uri                        = new Uri();
108
        $this->uri                        = $this->uri->withScheme($this->scheme);
109
        $this->options['http_errors']     = false;
110
        $this->options['connect_timeout'] = self::CONNECT_TIMEOUT;
111 184
        $this->options['timeout']         = self::TIMEOUT;
112
        if ($options !== []) {
113 184
            $this->options($options);
114 184
        }
115 184
116 184
        if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') {
117 184
            $this->options['debug'] = true;
118 184
        }
119 184
    }
120 184
121 1
    /**
122 1
     * @param string $name
123
     * @param string $value
124 184
     *
125 1
     * @return $this
126 1
     * @throws ClientException
127 184
     */
128
    public function appendUserAgent($name, $value)
129
    {
130
        Filter::name($name);
131
132
        Filter::value($value);
133
134
        if (!UserAgent::isGuarded($name)) {
135
            $this->userAgent[$name] = $value;
136 5
        }
137
138 5
        return $this;
139
    }
140 3
141
    /**
142 1
     * @param array $userAgent
143 1
     *
144 1
     * @return $this
145
     */
146 1
    public function withUserAgent(array $userAgent)
147
    {
148
        $this->userAgent = UserAgent::clean($userAgent);
149
150
        return $this;
151
    }
152
153
    /**
154 2
     * Set the response data format.
155
     *
156 2
     * @param string $format
157
     *
158 2
     * @return $this
159
     * @throws ClientException
160
     */
161
    public function format($format)
162
    {
163
        ApiFilter::format($format);
164
165
        $this->format = \strtoupper($format);
166
167
        return $this;
168
    }
169 20
170
    /**
171 20
     * Set the request body.
172
     *
173 16
     * @param string $body
174
     *
175 16
     * @return $this
176
     * @throws ClientException
177
     */
178
    public function body($body)
179
    {
180
        HttpFilter::body($body);
181
182
        $this->options['body'] = $body;
183
184
        return $this;
185
    }
186 7
187
    /**
188 7
     * Set the json as body.
189
     *
190 5
     * @param array|object $content
191
     *
192 5
     * @return $this
193
     * @throws ClientException
194
     */
195
    public function jsonBody($content)
196
    {
197
        if (!\is_array($content) && !\is_object($content)) {
198
            throw new ClientException(
199
                'jsonBody only accepts an array or object',
200
                SDK::INVALID_ARGUMENT
201
            );
202
        }
203 3
204
        return $this->body(\json_encode($content));
205 3
    }
206 1
207 1
    /**
208
     * Set the request scheme.
209 1
     *
210
     * @param string $scheme
211
     *
212 2
     * @return $this
213
     * @throws ClientException
214
     */
215
    public function scheme($scheme)
216
    {
217
        HttpFilter::scheme($scheme);
218
219
        $this->scheme = \strtolower($scheme);
220
        $this->uri    = $this->uri->withScheme($this->scheme);
221
222
        return $this;
223 16
    }
224
225 16
    /**
226
     * Set the request host.
227 14
     *
228 14
     * @param string $host
229
     *
230 14
     * @return $this
231
     * @throws ClientException
232
     */
233
    public function host($host)
234
    {
235
        HttpFilter::host($host);
236
237
        $this->uri = $this->uri->withHost($host);
238
239
        return $this;
240
    }
241 26
242
    /**
243 26
     * @param string $method
244
     *
245 24
     * @return $this
246
     * @throws ClientException
247 24
     */
248
    public function method($method)
249
    {
250
        $this->method = HttpFilter::method($method);
251
252
        return $this;
253
    }
254
255
    /**
256 51
     * @param string $clientName
257
     *
258 51
     * @return $this
259
     * @throws ClientException
260 49
     */
261
    public function client($clientName)
262
    {
263
        ClientFilter::clientName($clientName);
264
265
        $this->client = $clientName;
266
267
        return $this;
268
    }
269 69
270
    /**
271 69
     * @return bool
272
     * @throws ClientException
273 67
     */
274
    public function isDebug()
275 67
    {
276
        if (isset($this->options['debug'])) {
277
            return $this->options['debug'] === true;
278
        }
279
280
        if (isset($this->httpClient()->options['debug'])) {
281
            return $this->httpClient()->options['debug'] === true;
282 1
        }
283
284 1
        return false;
285 1
    }
286
287
    /**
288 1
     * @return Result
289 1
     * @throws ClientException
290
     * @throws ServerException
291
     */
292 1
    public function request()
293
    {
294
        $this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent);
295
296
        $this->removeRedundantQuery();
297
        $this->removeRedundantHeaders();
298
        $this->removeRedundantHFormParams();
299
        $this->resolveUri();
300 59
        $this->resolveParameters($this->credential());
301
302 59
        if (isset($this->options['form_params'])) {
303
            $this->options['form_params'] = \GuzzleHttp\Psr7\parse_query(
304 59
                self::getPostHttpBody($this->options['form_params'])
305 59
            );
306 59
        }
307 59
308 56
        $this->mergeOptionsIntoClient();
309
310 53
        $result = new Result($this->response(), $this);
311 27
312 27
        if (!$result->isSuccess()) {
313 27
            throw new ServerException($result);
314 27
        }
315
316 53
        return $result;
317
    }
318 53
319
    /**
320 50
     * Remove redundant Query
321 20
     *
322
     * @codeCoverageIgnore
323
     */
324 30
    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
    public static function getPostHttpBody(array $post)
361
    {
362
        $content = '';
363
        foreach ($post as $apiKey => $apiValue) {
364
            $content .= "$apiKey=" . urlencode($apiValue) . '&';
365
        }
366
367
        return substr($content, 0, -1);
368 30
    }
369
370 30
    /**
371 30
     * @return Client
372 30
     */
373 30
    public static function createClient()
374
    {
375 30
        if (AlibabaCloud::hasMock()) {
376
            $stack = HandlerStack::create(AlibabaCloud::getMock());
377
        } else {
378
            $stack = HandlerStack::create();
379
        }
380
381 53
        if (AlibabaCloud::isRememberHistory()) {
382
            $stack->push(Middleware::history(AlibabaCloud::referenceHistory()));
383
        }
384 53
385 53
        return new Client([
386 53
                              'handler' => $stack,
387 53
                          ]);
388 53
    }
389 3
390 3
    /**
391 3
     * @throws ClientException
392 3
     */
393
    private function response()
394 3
    {
395
        try {
396
            return self::createClient()->request(
397
                $this->method,
398
                (string)$this->uri,
399
                $this->options
400
            );
401 22
        } catch (GuzzleException $e) {
402
            throw new ClientException(
403 22
                $e->getMessage(),
404
                SDK::SERVER_UNREACHABLE,
405
                $e
406
            );
407
        }
408
    }
409
410
    /**
411
     * @return string
412
     */
413
    public function stringToBeSigned()
414
    {
415
        return $this->stringToBeSigned;
416
    }
417
}
418