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 (#69)
by Yong
05:55 queued 01:51
created

Request::removeRedundantParameters()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 15.4039

Importance

Changes 0
Metric Value
cc 7
eloc 6
nc 8
nop 0
dl 0
loc 12
ccs 4
cts 9
cp 0.4444
crap 15.4039
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use AlibabaCloud\Client\Exception\ClientException;
6
use AlibabaCloud\Client\Exception\ServerException;
7
use AlibabaCloud\Client\Http\GuzzleTrait;
8
use AlibabaCloud\Client\Request\Traits\AcsTrait;
9
use AlibabaCloud\Client\Request\Traits\ClientTrait;
10
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait;
11
use AlibabaCloud\Client\Request\Traits\MagicTrait;
12
use AlibabaCloud\Client\Result\Result;
13
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
14
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
15
use GuzzleHttp\Client;
16
use GuzzleHttp\Exception\GuzzleException;
17
use GuzzleHttp\Psr7\Uri;
18
19
/**
20
 * Class Request
21
 *
22
 * @package   AlibabaCloud\Client\Request
23
 *
24
 * @method string resolveParameters($credential)
25
 */
26
abstract class Request implements \ArrayAccess
27
{
28
    use DeprecatedTrait;
29
    use GuzzleTrait;
30
    use MagicTrait;
31
    use ClientTrait;
32
    use AcsTrait;
33
    use ArrayAccessTrait;
34
    use ObjectAccessTrait;
35
36
    /**
37
     * @var string
38
     */
39
    public $scheme = 'http';
40
41
    /**
42
     * @var string
43
     */
44
    public $method = 'GET';
45
46
    /**
47
     * @var string
48
     */
49
    public $format = 'JSON';
50
51
    /**
52
     * @var string
53
     */
54
    public $client = \ALIBABA_CLOUD_GLOBAL_CLIENT;
55
56
    /**
57
     * @var Uri
58
     */
59
    public $uri;
60
61
    /**
62
     * @var Client
63
     */
64
    public $guzzle;
65
66
    /**
67
     * @var array The original parameters of the request.
68
     */
69
    public $data = [];
70
71
    /**
72
     * @var string
73
     */
74
    protected $stringToBeSigned = '';
75
76
    /**
77
     * @var array
78
     */
79
    private $userAgent = [];
80
81
    /**
82
     * Request constructor.
83
     *
84
     * @param array $options
85
     */
86 159
    public function __construct(array $options = [])
87
    {
88 159
        $this->uri                        = new Uri();
89 159
        $this->uri                        = $this->uri->withScheme($this->scheme);
90 159
        $this->guzzle                     = new Client();
91 159
        $this->options['http_errors']     = false;
92 159
        $this->options['timeout']         = ALIBABA_CLOUD_TIMEOUT;
93 159
        $this->options['connect_timeout'] = ALIBABA_CLOUD_CONNECT_TIMEOUT;
94
95 159
        if ($options !== []) {
96 1
            $this->options($options);
97 1
        }
98
99 159
        if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') {
100 1
            $this->options['debug'] = true;
101 1
        }
102 159
    }
103
104
    /**
105
     * @param string $name
106
     * @param string $value
107
     *
108
     * @return $this
109
     */
110 1
    public function appendUserAgent($name, $value)
111
    {
112 1
        if (!UserAgent::isGuarded($name)) {
113 1
            $this->userAgent[$name] = $value;
114 1
        }
115
116 1
        return $this;
117
    }
118
119
    /**
120
     * @param array $userAgent
121
     *
122
     * @return $this
123
     */
124 2
    public function withUserAgent(array $userAgent)
125
    {
126 2
        $this->userAgent = UserAgent::clean($userAgent);
127
128 2
        return $this;
129
    }
130
131
    /**
132
     * Set the response data format.
133
     *
134
     * @param string $format
135
     *
136
     * @return $this
137
     */
138 12
    public function format($format)
139
    {
140 12
        $this->format = \strtoupper($format);
141
142 12
        return $this;
143
    }
144
145
    /**
146
     * Set the request body.
147
     *
148
     * @param string $content
149
     *
150
     * @return $this
151
     */
152 5
    public function body($content)
153
    {
154 5
        $this->options['body'] = $content;
155
156 5
        return $this;
157
    }
158
159
    /**
160
     * Set the json as body.
161
     *
162
     * @param array|object $content
163
     *
164
     * @return $this
165
     */
166 2
    public function jsonBody($content)
167
    {
168 2
        if (\is_array($content) || \is_object($content)) {
169 2
            $content = \json_encode($content);
170 2
        }
171
172 2
        return $this->body($content);
173
    }
174
175
    /**
176
     * Set the request scheme.
177
     *
178
     * @param string $scheme
179
     *
180
     * @return $this
181
     */
182 26
    public function scheme($scheme)
183
    {
184 26
        $this->scheme = \strtolower($scheme);
185 26
        $this->uri    = $this->uri->withScheme($this->scheme);
186
187 26
        return $this;
188
    }
189
190
    /**
191
     * Set the request host.
192
     *
193
     * @param string $host
194
     *
195
     * @return $this
196
     */
197 28
    public function host($host)
198
    {
199 28
        $this->uri = $this->uri->withHost($host);
200
201 28
        return $this;
202
    }
203
204
    /**
205
     * @param string $method
206
     *
207
     * @return $this
208
     */
209 67
    public function method($method)
210
    {
211 67
        $this->method = \strtoupper($method);
212
213 67
        return $this;
214
    }
215
216
    /**
217
     * @param string $clientName
218
     *
219
     * @return $this
220
     */
221 82
    public function client($clientName)
222
    {
223 82
        $this->client = $clientName;
224
225 82
        return $this;
226
    }
227
228
    /**
229
     * @return bool
230
     * @throws ClientException
231
     */
232 1
    public function isDebug()
233
    {
234 1
        if (isset($this->options['debug'])) {
235 1
            return $this->options['debug'] === true;
236
        }
237
238 1
        if (isset($this->httpClient()->options['debug'])) {
239 1
            return $this->httpClient()->options['debug'] === true;
240
        }
241
242 1
        return false;
243
    }
244
245
    /**
246
     * @return Result
247
     * @throws ClientException
248
     * @throws ServerException
249
     */
250 71
    public function request()
251
    {
252 71
        $this->removeRedundantParameters();
253
254 71
        $this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent);
255
256 71
        $this->resolveUri();
257
258 68
        $this->resolveParameters($this->credential());
259
260 52
        if (isset($this->options['form_params'])) {
261 30
            $this->options['form_params'] = \GuzzleHttp\Psr7\parse_query(
262 30
                self::getPostHttpBody($this->options['form_params'])
263 30
            );
264 30
        }
265
266 52
        $this->mergeOptionsIntoClient();
267
268 52
        $result = new Result($this->response(), $this);
269
270 49
        if (!$result->isSuccess()) {
271 28
            throw new ServerException($result);
272
        }
273
274 21
        return $result;
275
    }
276
277
    /**
278
     * Remove redundant parameters
279
     */
280 71
    private function removeRedundantParameters()
281
    {
282 71
        if (isset($this->options['query']) && $this->options['query'] === []) {
283
            unset($this->options['query']);
284
        }
285
286 71
        if (isset($this->options['headers']) && $this->options['headers'] === []) {
287
            unset($this->options['headers']);
288
        }
289
290 71
        if (isset($this->options['form_params']) && $this->options['form_params'] === []) {
291
            unset($this->options['form_params']);
292
        }
293 71
    }
294
295
    /**
296
     * @param array $post
297
     *
298
     * @return bool|string
299
     */
300 33
    public static function getPostHttpBody(array $post)
301
    {
302 33
        $content = '';
303 33
        foreach ($post as $apiKey => $apiValue) {
304 33
            $content .= "$apiKey=" . urlencode($apiValue) . '&';
305 33
        }
306
307 33
        return substr($content, 0, -1);
308
    }
309
310
    /**
311
     * @throws ClientException
312
     */
313 52
    private function response()
314
    {
315
        try {
316 52
            return $this->guzzle->request(
317 52
                $this->method,
318 52
                (string)$this->uri,
319 52
                $this->options
320 52
            );
321 3
        } catch (GuzzleException $e) {
322 3
            throw new ClientException(
323 3
                $e->getMessage(),
324 3
                \ALIBABA_CLOUD_SERVER_UNREACHABLE,
325
                $e
326 3
            );
327
        }
328
    }
329
330
    /**
331
     * @return string
332
     */
333 28
    public function stringToBeSigned()
334
    {
335 28
        return $this->stringToBeSigned;
336
    }
337
}
338