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
05:58
created

Request::withUserAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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