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

Request::stringToBeSigned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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