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 (#135)
by Yong
03:46
created

Request::accept()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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