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 (#134)
by Yong
03:52
created

RoaRequest::ksortAndEncode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use Exception;
6
use Ramsey\Uuid\Uuid;
7
use RuntimeException;
8
use AlibabaCloud\Client\SDK;
9
use AlibabaCloud\Client\Encode;
10
use AlibabaCloud\Client\Format;
11
use AlibabaCloud\Client\Filter\Filter;
12
use AlibabaCloud\Client\Filter\ApiFilter;
13
use AlibabaCloud\Client\Credentials\StsCredential;
14
use AlibabaCloud\Client\Exception\ClientException;
15
use AlibabaCloud\Client\Exception\ServerException;
16
use AlibabaCloud\Client\Credentials\AccessKeyCredential;
17
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
18
use AlibabaCloud\Client\Request\Traits\DeprecatedRoaTrait;
19
20
/**
21
 * RESTful ROA Request.
22
 *
23
 * @package   AlibabaCloud\Client\Request
24
 */
25
class RoaRequest extends Request
26
{
27
    use DeprecatedRoaTrait;
28
29
    /**
30
     * @var string
31
     */
32
    private static $headerSeparator = "\n";
33
34
    /**
35
     * @var string
36
     */
37
    public $pathPattern = '/';
38
39
    /**
40
     * @var array
41
     */
42
    public $pathParameters = [];
43
44
    /**
45
     * @var string
46
     */
47
    private $dateTimeFormat = "D, d M Y H:i:s \G\M\T";
48
49
    /**
50
     * Resolve request parameter.
51
     *
52
     * @throws ClientException
53
     * @throws Exception
54
     */
55 17
    public function resolveParameter()
56
    {
57 17
        $this->resolveQuery();
58 17
        $this->resolveBody();
59 17
        $this->resolveHeaders();
60 17
    }
61
62 17
    private function resolveQuery()
63
    {
64 17
        if (!isset($this->options['query']['Version'])) {
65 17
            $this->options['query']['Version'] = $this->version;
66 17
        }
67 17
    }
68
69 17
    private function resolveBody()
70
    {
71 17
        if (isset($this->options['form_params']) && !isset($this->options['body'])) {
72 2
            $params = \AlibabaCloud\Client\arrayMerge(
73
                [
74 2
                    $this->data,
75 2
                    $this->options['form_params']
76 2
                ]
77 2
            );
78
79 2
            $this->options['body']                    = Encode::create($params)->ksort()->toString();
80 2
            $this->options['headers']['Content-MD5']  = base64_encode(md5($this->options['body'], true));
81 2
            $this->options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
82 2
            unset($this->options['form_params']);
83 2
        }
84 17
    }
85
86
    /**
87
     * @throws ClientException
88
     * @throws ServerException
89
     */
90 17
    private function resolveHeaders()
91
    {
92 17
        $this->options['headers']['x-acs-version']   = $this->version;
93 17
        $this->options['headers']['x-acs-region-id'] = $this->realRegionId();
94 17
        $this->options['headers']['Date']            = gmdate($this->dateTimeFormat);
95 17
        $this->options['headers']['Accept']          = Format::create($this->format)->toString();
96 17
        $this->resolveSignature();
97 17
        $this->resolveContentType();
98 17
        $this->resolveSecurityToken();
99 17
        $this->resolveBearerToken();
100 17
        $this->options['headers']['Authorization'] = $this->signature();
101 17
    }
102
103
    /**
104
     * @throws ClientException
105
     * @throws Exception
106
     */
107 17
    private function resolveSignature()
108
    {
109 17
        $signature                                           = $this->httpClient()->getSignature();
110 17
        $this->options['headers']['x-acs-signature-method']  = $signature->getMethod();
111 17
        $this->options['headers']['x-acs-signature-nonce']   = Uuid::uuid1()->toString();
112 17
        $this->options['headers']['x-acs-signature-version'] = $signature->getVersion();
113 17
        if ($signature->getType()) {
114 4
            $this->options['headers']['x-acs-signature-type'] = $signature->getType();
115 4
        }
116 17
    }
117
118 17
    private function resolveContentType()
119
    {
120 17
        if (!isset($this->options['headers']['Content-Type'])) {
121 15
            $this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']};chrset=utf-8";
122 15
        }
123 17
    }
124
125
    /**
126
     * @throws ClientException
127
     * @throws ServerException
128
     */
129 17
    private function resolveSecurityToken()
130
    {
131 17
        if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) {
132
            $this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
133
        }
134 17
    }
135
136
    /**
137
     * @throws ClientException
138
     * @throws ServerException
139
     */
140 17
    private function resolveBearerToken()
141
    {
142 17
        if ($this->credential() instanceof BearerTokenCredential) {
143 4
            $this->options['headers']['x-acs-bearer-token'] = $this->credential()->getBearerToken();
144 4
        }
145 17
    }
146
147
    /**
148
     * Sign the request message.
149
     *
150
     * @return string
151
     * @throws ClientException
152
     * @throws ServerException
153
     */
154 17
    private function signature()
155
    {
156
        /**
157
         * @var AccessKeyCredential $credential
158
         */
159 17
        $credential  = $this->credential();
160 17
        $accessKeyId = $credential->getAccessKeyId();
161 17
        $signature   = $this->httpClient()
162 17
                            ->getSignature()
163 17
                            ->sign(
164 17
                                $this->stringToSign(),
165 17
                                $credential->getAccessKeySecret()
166 17
                            );
167
168 17
        return "acs $accessKeyId:$signature";
169
    }
170
171
    /**
172
     * @return string
173
     */
174 17
    public function stringToSign()
175
    {
176 17
        return $this->headerStringToSign() . $this->resourceStringToSign();
177
    }
178
179
    /**
180
     * @return string
181
     */
182 17
    private function headerStringToSign()
183
    {
184 17
        $string = $this->method . self::$headerSeparator;
185 17
        if (isset($this->options['headers']['Accept'])) {
186 17
            $string .= $this->options['headers']['Accept'];
187 17
        }
188 17
        $string .= self::$headerSeparator;
189
190 17
        if (isset($this->options['headers']['Content-MD5'])) {
191 2
            $string .= $this->options['headers']['Content-MD5'];
192 2
        }
193 17
        $string .= self::$headerSeparator;
194
195 17
        if (isset($this->options['headers']['Content-Type'])) {
196 17
            $string .= $this->options['headers']['Content-Type'];
197 17
        }
198 17
        $string .= self::$headerSeparator;
199
200 17
        if (isset($this->options['headers']['Date'])) {
201 17
            $string .= $this->options['headers']['Date'];
202 17
        }
203 17
        $string .= self::$headerSeparator;
204
205 17
        $string .= $this->acsHeaderString();
206
207 17
        return $string;
208
    }
209
210
    /**
211
     * Construct standard Header for Alibaba Cloud.
212
     *
213
     * @return string
214
     */
215 17
    private function acsHeaderString()
216
    {
217 17
        $array = [];
218 17
        foreach ($this->options['headers'] as $headerKey => $headerValue) {
219 17
            $key = strtolower($headerKey);
220 17
            if (strncmp($key, 'x-acs-', 6) === 0) {
221 17
                $array[$key] = $headerValue;
222 17
            }
223 17
        }
224 17
        ksort($array);
225 17
        $string = '';
226 17
        foreach ($array as $sortMapKey => $sortMapValue) {
227 17
            $string .= $sortMapKey . ':' . $sortMapValue . self::$headerSeparator;
228 17
        }
229
230 17
        return $string;
231
    }
232
233
    /**
234
     * @return string
235
     */
236 17
    private function resourceStringToSign()
237
    {
238 17
        $this->uri = $this->uri->withPath($this->resolvePath())
239 17
                               ->withQuery(
240 17
                                   Encode::create(isset($this->options['query'])
241 17
                                                      ? $this->options['query']
242 17
                                                      : [])
243 17
                                         ->ksort()
244 17
                                         ->toString()
245 17
                               );
246
247 17
        return $this->uri->getPath() . '?' . $this->uri->getQuery();
248
    }
249
250
    /**
251
     * Assign path parameters to the url.
252
     *
253
     * @return string
254
     */
255 19
    private function resolvePath()
256
    {
257 19
        $path = $this->pathPattern;
258 19
        foreach ($this->pathParameters as $pathKey => $value) {
259 12
            $target = "[$pathKey]";
260 12
            $path   = str_replace($target, $value, $path);
261 19
        }
262
263 19
        return $path;
264
    }
265
266
    /**
267
     * Set path parameter by name.
268
     *
269
     * @param string $name
270
     * @param string $value
271
     *
272
     * @return RoaRequest
273
     * @throws ClientException
274
     */
275 11
    public function pathParameter($name, $value)
276
    {
277 11
        Filter::name($name);
278
279 9
        if ($value === '') {
280 1
            throw new ClientException(
281 1
                'Value cannot be empty',
282
                SDK::INVALID_ARGUMENT
283 1
            );
284
        }
285
286 8
        $this->pathParameters[$name] = $value;
287
288 8
        return $this;
289
    }
290
291
    /**
292
     * Set path pattern.
293
     *
294
     * @param string $pattern
295
     *
296
     * @return self
297
     * @throws ClientException
298
     */
299 9
    public function pathPattern($pattern)
300
    {
301 9
        ApiFilter::pattern($pattern);
302
303 7
        $this->pathPattern = $pattern;
304
305 7
        return $this;
306
    }
307
308
    /**
309
     * Magic method for set or get request parameters.
310
     *
311
     * @param string $name
312
     * @param mixed  $arguments
313
     *
314
     * @return $this
315
     */
316 12
    public function __call($name, $arguments)
317
    {
318 12
        if (strncmp($name, 'get', 3) === 0) {
319 1
            $parameterName = $this->propertyNameByMethodName($name);
320
321 1
            return $this->__get($parameterName);
322
        }
323
324 12
        if (strncmp($name, 'with', 4) === 0) {
325 10
            $parameterName = $this->propertyNameByMethodName($name, 4);
326 10
            $this->__set($parameterName, $arguments[0]);
327 10
            $this->pathParameters[$parameterName] = $arguments[0];
328
329 10
            return $this;
330
        }
331
332 2
        if (strncmp($name, 'set', 3) === 0) {
333 1
            $parameterName = $this->propertyNameByMethodName($name);
334 1
            $withMethod    = "with$parameterName";
335
336 1
            throw new RuntimeException("Please use $withMethod instead of $name");
337
        }
338
339 1
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
340
    }
341
}
342