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

RoaRequest::resolveCommonParameters()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

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