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
Pull Request — master (#54)
by Yong
04:48
created

Request::withUserAgent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 1
cts 1
cp 1
crap 2
rs 10
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 155
    /**
82
     * Request constructor.
83 155
     *
84 155
     * @param array $options
85 155
     */
86 155
    public function __construct(array $options = [])
87 155
    {
88 155
        $this->uri                        = new Uri();
89 155
        $this->uri                        = $this->uri->withScheme($this->scheme);
90 1
        $this->guzzle                     = new Client();
91 1
        $this->options['http_errors']     = false;
92 155
        $this->options['timeout']         = ALIBABA_CLOUD_TIMEOUT;
93
        $this->options['connect_timeout'] = ALIBABA_CLOUD_CONNECT_TIMEOUT;
94
95
        if ($options !== []) {
96
            $this->options($options);
97
        }
98
    }
99
100 1
    /**
101
     * @param string $name
102 1
     * @param string $value
103 1
     *
104
     * @return $this
105
     */
106
    public function appendUserAgent($name, $value)
107
    {
108
        if (!UserAgent::isGuarded($name)) {
109
            $this->userAgent[$name] = $value;
110
        }
111
112
        return $this;
113 12
    }
114
115 12
    /**
116 12
     * @param array $userAgent
117
     *
118
     * @return $this
119
     */
120
    public function withUserAgent(array $userAgent)
121
    {
122
        $this->userAgent = [];
123
        foreach ($userAgent as $key => $value) {
124
            $this->appendUserAgent($key, $value);
125
        }
126 5
127
        return $this;
128 5
    }
129 5
130 1
    /**
131
     * Set the response data format.
132
     *
133
     * @param string $format
134
     *
135
     * @return $this
136
     */
137
    public function format($format)
138
    {
139 2
        $this->format = \strtoupper($format);
140
141 2
        return $this;
142 2
    }
143 2
144 2
    /**
145
     * Set the request body.
146
     *
147
     * @param string $content
148
     *
149
     * @return $this
150
     */
151
    public function body($content)
152
    {
153
        $this->options['body'] = $content;
154 26
155
        return $this;
156 26
    }
157 26
158 26
    /**
159
     * Set the json as body.
160
     *
161
     * @param array|object $content
162
     *
163
     * @return $this
164
     */
165
    public function jsonBody($content)
166
    {
167
        if (\is_array($content) || \is_object($content)) {
168 28
            $content = \json_encode($content);
169
        }
170 28
171 28
        return $this->body($content);
172
    }
173
174
    /**
175
     * Set the request scheme.
176
     *
177
     * @param string $scheme
178
     *
179 65
     * @return $this
180
     */
181 65
    public function scheme($scheme)
182 65
    {
183
        $this->scheme = \strtolower($scheme);
184
        $this->uri    = $this->uri->withScheme($this->scheme);
185
186
        return $this;
187
    }
188
189
    /**
190 80
     * Set the request host.
191
     *
192 80
     * @param string $host
193 80
     *
194
     * @return $this
195
     */
196
    public function host($host)
197
    {
198
        $this->uri = $this->uri->withHost($host);
199
200 1
        return $this;
201
    }
202 1
203 1
    /**
204
     * @param string $method
205
     *
206 1
     * @return $this
207 1
     */
208
    public function method($method)
209
    {
210 1
        $this->method = \strtoupper($method);
211
212
        return $this;
213
    }
214
215
    /**
216
     * @param string $clientName
217
     *
218 66
     * @return $this
219
     */
220 66
    public function client($clientName)
221
    {
222 66
        $this->client = $clientName;
223
224 66
        return $this;
225
    }
226 50
227 28
    /**
228 28
     * @return bool
229 28
     * @throws ClientException
230 28
     */
231
    public function isDebug()
232 50
    {
233
        if (isset($this->options['debug'])) {
234 50
            return $this->options['debug'] === true;
235
        }
236 47
237 29
        if (isset($this->httpClient()->options['debug'])) {
238
            return $this->httpClient()->options['debug'] === true;
239
        }
240 18
241
        return false;
242
    }
243
244
    /**
245
     * @return Result
246
     * @throws ClientException
247
     * @throws ServerException
248 31
     */
249
    public function request()
250 31
    {
251 31
        $this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent);
252 31
253 31
        $this->resolveUri();
254 31
255
        $this->resolveParameters($this->credential());
256
257
        if (isset($this->options['form_params'])) {
258
            $this->options['form_params'] = \GuzzleHttp\Psr7\parse_query(
259
                self::getPostHttpBody($this->options['form_params'])
260 50
            );
261
        }
262
263 50
        $this->mergeOptionsIntoClient();
264 50
265 50
        $result = new Result($this->response(), $this);
266 50
267 50
        if (!$result->isSuccess()) {
268 3
            throw new ServerException($result);
269 3
        }
270 3
271 3
        return $result;
272
    }
273 3
274
    /**
275
     * @param array $post
276
     *
277
     * @return bool|string
278
     */
279
    public static function getPostHttpBody(array $post)
280 29
    {
281
        $content = '';
282 29
        foreach ($post as $apiKey => $apiValue) {
283
            $content .= "$apiKey=" . urlencode($apiValue) . '&';
284
        }
285
286
        return substr($content, 0, -1);
287
    }
288
289
    /**
290
     * @throws ClientException
291
     */
292
    private function response()
293
    {
294
        try {
295
            return $this->guzzle->request(
296
                $this->method,
297
                (string)$this->uri,
298
                $this->options
299
            );
300
        } catch (GuzzleException $e) {
301
            throw new ClientException(
302
                $e->getMessage(),
303
                \ALIBABA_CLOUD_SERVER_UNREACHABLE,
304
                $e
305
            );
306
        }
307
    }
308
309
    /**
310
     * @return string
311
     */
312
    public function stringToBeSigned()
313
    {
314
        return $this->stringToBeSigned;
315
    }
316
}
317