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
Push — master ( 97e48e...f0222a )
by Yong
04:06
created

Request::accept()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
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 175
    public function __construct(array $options = [])
105
    {
106 175
        $this->client                     = CredentialsProvider::getDefaultName();
107 175
        $this->uri                        = new Uri();
108 175
        $this->uri                        = $this->uri->withScheme($this->scheme);
109 175
        $this->options['http_errors']     = false;
110 175
        $this->options['connect_timeout'] = self::CONNECT_TIMEOUT;
111 175
        $this->options['timeout']         = self::TIMEOUT;
112 175
        if ($options !== []) {
113 1
            $this->options($options);
114 1
        }
115
116 175
        if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') {
117 92
            $this->options['debug'] = true;
118 92
        }
119 175
    }
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 = Filter::name($name);
131
132 3
        if (!UserAgent::isGuarded($filter_name)) {
133 3
            $this->userAgent[$filter_name] = Filter::value($value);
134 1
        }
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * @param array $userAgent
141
     *
142
     * @return $this
143
     */
144 2
    public function withUserAgent(array $userAgent)
145
    {
146 2
        $this->userAgent = UserAgent::clean($userAgent);
147
148 2
        return $this;
149
    }
150
151
    /**
152
     * Set Accept format.
153
     *
154
     * @param string $format
155
     *
156
     * @return $this
157
     * @throws ClientException
158
     */
159 14
    public function format($format)
160
    {
161 14
        $this->format = ApiFilter::format($format);
162
163 12
        return $this;
164
    }
165
166
    /**
167
     * @param $contentType
168
     *
169
     * @return $this
170
     * @throws ClientException
171
     */
172 1
    public function contentType($contentType)
173
    {
174 1
        $this->options['headers']['Content-Type'] = HttpFilter::contentType($contentType);
175
176 1
        return $this;
177
    }
178
179
    /**
180
     * @param string $accept
181
     *
182
     * @return $this
183
     * @throws ClientException
184
     */
185 2
    public function accept($accept)
186 1
    {
187 2
        $this->options['headers']['Accept'] = HttpFilter::accept($accept);
188
189 2
        return $this;
190
    }
191
192
    /**
193
     * Set the request body.
194
     *
195
     * @param string $body
196
     *
197
     * @return $this
198
     * @throws ClientException
199
     */
200 5
    public function body($body)
201
    {
202 5
        $this->options['body'] = HttpFilter::body($body);
203
204 3
        return $this;
205
    }
206
207
    /**
208
     * Set the json as body.
209
     *
210
     * @param array|object $content
211
     *
212
     * @return $this
213
     * @throws ClientException
214
     */
215 3
    public function jsonBody($content)
216
    {
217 3
        if (!\is_array($content) && !\is_object($content)) {
218 1
            throw new ClientException(
219 1
                'jsonBody only accepts an array or object',
220
                SDK::INVALID_ARGUMENT
221 1
            );
222
        }
223
224 2
        return $this->body(\json_encode($content));
225
    }
226
227
    /**
228
     * Set the request scheme.
229
     *
230
     * @param string $scheme
231
     *
232
     * @return $this
233
     * @throws ClientException
234
     */
235 17
    public function scheme($scheme)
236
    {
237 17
        $this->scheme = HttpFilter::scheme($scheme);
238 15
        $this->uri    = $this->uri->withScheme($scheme);
239
240 15
        return $this;
241
    }
242
243
    /**
244
     * Set the request host.
245
     *
246
     * @param string $host
247
     *
248
     * @return $this
249
     * @throws ClientException
250
     */
251 28
    public function host($host)
252
    {
253 28
        $this->uri = $this->uri->withHost(HttpFilter::host($host));
254
255 26
        return $this;
256
    }
257
258
    /**
259
     * @param string $method
260
     *
261
     * @return $this
262
     * @throws ClientException
263
     */
264 43
    public function method($method)
265
    {
266 43
        $this->method = HttpFilter::method($method);
267
268 41
        return $this;
269
    }
270
271
    /**
272
     * @param string $clientName
273
     *
274
     * @return $this
275
     * @throws ClientException
276
     */
277 67
    public function client($clientName)
278
    {
279 67
        $this->client = ClientFilter::clientName($clientName);
280
281 65
        return $this;
282
    }
283
284
    /**
285
     * @return bool
286
     * @throws ClientException
287
     */
288 1
    public function isDebug()
289
    {
290 1
        if (isset($this->options['debug'])) {
291 1
            return $this->options['debug'] === true;
292
        }
293
294 1
        if (isset($this->httpClient()->options['debug'])) {
295 1
            return $this->httpClient()->options['debug'] === true;
296
        }
297
298 1
        return false;
299
    }
300
301
    /**
302
     * @throws ClientException
303
     * @throws ServerException
304
     */
305 60
    public function resolveOption()
306
    {
307 60
        $this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent);
308
309 60
        $this->cleanQuery();
310 60
        $this->cleanFormParams();
311 60
        $this->resolveHost();
312 57
        $this->resolveParameter();
313
314 54
        if (isset($this->options['form_params'])) {
315 21
            $this->options['form_params'] = \GuzzleHttp\Psr7\parse_query(
316 21
                Encode::create($this->options['form_params'])->toString()
317 21
            );
318 21
        }
319
320 54
        $this->mergeOptionsIntoClient();
321 54
    }
322
323
    /**
324
     * @return Result
325
     * @throws ClientException
326
     * @throws ServerException
327
     */
328 60
    public function request()
329
    {
330 60
        $this->resolveOption();
331 54
        $result = new Result($this->response(), $this);
332
333 51
        if (!$result->isSuccess()) {
334 18
            throw new ServerException($result);
335
        }
336
337 34
        return $result;
338
    }
339
340
    /**
341
     * Remove redundant Query
342
     *
343
     * @codeCoverageIgnore
344
     */
345
    private function cleanQuery()
346
    {
347
        if (isset($this->options['query']) && $this->options['query'] === []) {
348
            unset($this->options['query']);
349
        }
350
    }
351
352
    /**
353
     * Remove redundant Headers
354
     *
355
     * @codeCoverageIgnore
356
     */
357
    private function cleanFormParams()
358
    {
359
        if (isset($this->options['form_params']) && $this->options['form_params'] === []) {
360
            unset($this->options['form_params']);
361
        }
362
    }
363
364
    /**
365
     * @return Client
366
     * @throws Exception
367
     */
368 64
    public static function createClient()
369
    {
370 64
        if (AlibabaCloud::hasMock()) {
371 22
            $stack = HandlerStack::create(AlibabaCloud::getMock());
372 22
        } else {
373 42
            $stack = HandlerStack::create();
374
        }
375
376 64
        if (AlibabaCloud::isRememberHistory()) {
377 1
            $stack->push(Middleware::history(AlibabaCloud::referenceHistory()));
378 1
        }
379
380 64
        if (AlibabaCloud::getLogger()) {
381 2
            $stack->push(Middleware::log(
382 2
                AlibabaCloud::getLogger(),
383 2
                new LogFormatter(AlibabaCloud::getLogFormat())
384 2
            ));
385 2
        }
386
387 64
        self::$config['handler'] = $stack;
388
389 64
        return new Client(self::$config);
390
    }
391
392
    /**
393
     * @throws ClientException
394
     * @throws Exception
395
     */
396 54
    private function response()
397
    {
398
        try {
399 54
            return self::createClient()->request(
400 54
                $this->method,
401 54
                (string)$this->uri,
402 54
                $this->options
403 54
            );
404 4
        } catch (GuzzleException $exception) {
405 3
            throw new ClientException(
406 3
                $exception->getMessage(),
407 3
                SDK::SERVER_UNREACHABLE,
408
                $exception
409 3
            );
410
        }
411
    }
412
}
413