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

Request::stringToBeSigned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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