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 (#54)
by Yong
06:06
created

Request::withUserAgent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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