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

RoaRequest::resolveQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
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\Credentials\BearerTokenCredential;
6
use AlibabaCloud\Client\Credentials\CredentialsInterface;
7
use AlibabaCloud\Client\Credentials\StsCredential;
8
use AlibabaCloud\Client\Exception\ClientException;
9
use AlibabaCloud\Client\Exception\ServerException;
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
    public $pathPattern = '/';
36
37
    /**
38
     * @var array
39
     */
40
    public $pathParameters = [];
41
42
    /**
43
     * @var string
44
     */
45
    private $dateTimeFormat = "D, d M Y H:i:s \G\M\T";
46
47
    /**
48
     * Resolve request parameter.
49
     *
50
     * @throws ClientException
51
     * @throws Exception
52
     */
53 17
    public function resolveParameters()
54
    {
55 17
        $this->resolveQuery();
56 17
        $this->resolveBody();
57 17
        $this->resolveHeaders();
58 17
    }
59
60 17
    private function resolveQuery()
61
    {
62 17
        if (!isset($this->options['query']['Version'])) {
63 17
            $this->options['query']['Version'] = $this->version;
64 17
        }
65 17
    }
66
67 17
    private function resolveBody()
68
    {
69 17
        if (isset($this->options['form_params']) && !isset($this->options['body'])) {
70 2
            $this->options['body']                    = $this->ksortAndEncode($this->data);
71 2
            $this->options['headers']['Content-MD5']  = base64_encode(md5($this->options['body'], true));
72 2
            $this->options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
73 2
            unset($this->options['form_params']);
74 2
        }
75 17
    }
76
77
    /**
78
     * @throws ClientException
79
     * @throws Exception
80
     */
81 17
    private function resolveHeaders()
82
    {
83 17
        $signature = $this->httpClient()->getSignature();
84
85 17
        $this->options['headers']['x-acs-version']           = $this->version;
86 17
        $this->options['headers']['x-acs-region-id']         = $this->realRegionId();
87 17
        $this->options['headers']['Date']                    = gmdate($this->dateTimeFormat);
88 17
        $this->options['headers']['Accept']                  = self::formatToAccept($this->format);
89 17
        $this->options['headers']['x-acs-signature-method']  = $signature->getMethod();
90 17
        $this->options['headers']['x-acs-signature-nonce']   = Uuid::uuid1()->toString();
91 17
        $this->options['headers']['x-acs-signature-version'] = $signature->getVersion();
92
93 17
        if ($signature->getType()) {
94 4
            $this->options['headers']['x-acs-signature-type'] = $signature->getType();
95 4
        }
96
97 17
        if (!isset($this->options['headers']['Content-Type'])) {
98 15
            $this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']};chrset=utf-8";
99 15
        }
100
101 17
        $this->resolveSecurityToken();
102 17
        $this->resolveBearerToken();
103 17
        $this->options['headers']['Authorization'] = $this->signature();
104 17
    }
105
106
    /**
107
     * @param array $data
108
     *
109
     * @return string
110
     */
111 17
    public function ksortAndEncode(array $data)
112
    {
113 17
        if (count($data) === 0) {
114
            return '';
115
        }
116
117 17
        ksort($data);
118
119 17
        $string = '';
120 17
        foreach ($data as $key => $value) {
121 17
            $encode = urlencode($value);
122 17
            $string .= "$key=$encode&";
123 17
        }
124
125 17
        if (0 < count($data)) {
126 17
            $string = substr($string, 0, -1);
127 17
        }
128
129 17
        return $string;
130
    }
131
132
    /**
133
     * Returns the accept header according to format.
134
     *
135
     * @param string $format
136
     *
137
     * @return string
138
     */
139 21
    private static function formatToAccept($format)
140
    {
141 21
        switch (\strtoupper($format)) {
142 21
            case 'JSON':
143 18
                return 'application/json';
144 3
            case 'XML':
145 1
                return 'application/xml';
146 2
            default:
147 2
                return 'application/octet-stream';
148 2
        }
149
    }
150
151
    /**
152
     * @throws ClientException
153
     * @throws ServerException
154
     */
155 17
    private function resolveSecurityToken()
156
    {
157 17
        if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) {
158
            $this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
159
        }
160 17
    }
161
162
    /**
163
     * @throws ClientException
164
     * @throws ServerException
165
     */
166 17
    private function resolveBearerToken()
167
    {
168 17
        if ($this->credential() instanceof BearerTokenCredential) {
169 4
            $this->options['headers']['x-acs-bearer-token'] = $this->credential()->getBearerToken();
170 4
        }
171 17
    }
172
173
    /**
174
     * @return string
175
     */
176 17
    private function headerStringToSign()
177
    {
178 17
        $string = $this->method . self::$headerSeparator;
179 17
        if (isset($this->options['headers']['Accept'])) {
180 17
            $string .= $this->options['headers']['Accept'];
181 17
        }
182 17
        $string .= self::$headerSeparator;
183
184 17
        if (isset($this->options['headers']['Content-MD5'])) {
185 2
            $string .= $this->options['headers']['Content-MD5'];
186 2
        }
187 17
        $string .= self::$headerSeparator;
188
189 17
        if (isset($this->options['headers']['Content-Type'])) {
190 17
            $string .= $this->options['headers']['Content-Type'];
191 17
        }
192 17
        $string .= self::$headerSeparator;
193
194 17
        if (isset($this->options['headers']['Date'])) {
195 17
            $string .= $this->options['headers']['Date'];
196 17
        }
197 17
        $string .= self::$headerSeparator;
198
199 17
        $string .= $this->constructAcsHeader();
200
201 17
        return $string;
202
    }
203
204
    /**
205
     * @return string
206
     */
207 17
    private function resourceStringToSign()
208
    {
209 17
        $this->uri = $this->uri->withPath($this->assignPathParameters())
210 17
                               ->withQuery(
211 17
                                   $this->ksortAndEncode(
212 17
                                       isset($this->options['query'])
213 17
                                           ? $this->options['query']
214 17
                                           : []
215 17
                                   )
216 17
                               );
217
218 17
        return $this->uri->getPath() . '?' . $this->uri->getQuery();
219
    }
220
221
    /**
222
     * @return string
223
     */
224 17
    public function stringToSign()
225
    {
226 17
        return $this->headerStringToSign() . $this->resourceStringToSign();
227
    }
228
229
    /**
230
     * Sign the request message.
231
     *
232
     * @return string
233
     * @throws ClientException
234
     * @throws ServerException
235
     */
236 17
    private function signature()
237
    {
238
        /**
239
         * @var CredentialsInterface $credential
240
         */
241 17
        $credential      = $this->credential();
242 17
        $accessKeyId     = $credential->getAccessKeyId();
0 ignored issues
show
Bug introduced by
The method getAccessKeyId() does not exist on AlibabaCloud\Client\Cred...ls\CredentialsInterface. It seems like you code against a sub-type of said class. However, the method does not exist in AlibabaCloud\Client\Cred...ls\RsaKeyPairCredential or AlibabaCloud\Client\Cred...ls\EcsRamRoleCredential. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

242
        /** @scrutinizer ignore-call */ 
243
        $accessKeyId     = $credential->getAccessKeyId();
Loading history...
243 17
        $accessKeySecret = $credential->getAccessKeySecret();
0 ignored issues
show
Bug introduced by
The method getAccessKeySecret() does not exist on AlibabaCloud\Client\Cred...ls\CredentialsInterface. It seems like you code against a sub-type of said class. However, the method does not exist in AlibabaCloud\Client\Cred...ls\RsaKeyPairCredential or AlibabaCloud\Client\Cred...ls\EcsRamRoleCredential. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

243
        /** @scrutinizer ignore-call */ 
244
        $accessKeySecret = $credential->getAccessKeySecret();
Loading history...
244 17
        $signature       = $this->httpClient()
245 17
                                ->getSignature()
246 17
                                ->sign(
247 17
                                    $this->stringToSign(),
248
                                    $accessKeySecret
249 17
                                );
250
251 17
        return "acs $accessKeyId:$signature";
252
    }
253
254
    /**
255
     * Construct standard Header for Alibaba Cloud.
256
     *
257
     * @return string
258
     */
259 17
    private function constructAcsHeader()
260
    {
261 17
        $sortMap = [];
262 17
        foreach ($this->options['headers'] as $headerKey => $headerValue) {
263 17
            $key = strtolower($headerKey);
264 17
            if (strpos($key, 'x-acs-') === 0) {
265 17
                $sortMap[$key] = $headerValue;
266 17
            }
267 17
        }
268 17
        ksort($sortMap);
269 17
        $headerString = '';
270 17
        foreach ($sortMap as $sortMapKey => $sortMapValue) {
271 17
            $headerString .= $sortMapKey . ':' . $sortMapValue . self::$headerSeparator;
272 17
        }
273
274 17
        return $headerString;
275
    }
276
277
    /**
278
     * Assign path parameters to the url.
279
     *
280
     * @return string
281
     */
282 19
    private function assignPathParameters()
283
    {
284 19
        $result = $this->pathPattern;
285 19
        foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue) {
286 12
            $target = '[' . $pathParameterKey . ']';
287 12
            $result = str_replace($target, $apiParameterValue, $result);
288 19
        }
289
290 19
        return $result;
291
    }
292
293
    /**
294
     * Set path parameter by name.
295
     *
296
     * @param string $name
297
     * @param string $value
298
     *
299
     * @return RoaRequest
300
     * @throws ClientException
301
     */
302 16
    public function pathParameter($name, $value)
303
    {
304 16
        Filter::name($name);
305
306 14
        if ($value === '') {
307 1
            throw new ClientException(
308 1
                'Value cannot be empty',
309
                SDK::INVALID_ARGUMENT
310 1
            );
311
        }
312
313 13
        $this->pathParameters[$name] = $value;
314
315 13
        return $this;
316
    }
317
318
    /**
319
     * Set path pattern.
320
     *
321
     * @param string $pattern
322
     *
323
     * @return self
324
     * @throws ClientException
325
     */
326 10
    public function pathPattern($pattern)
327
    {
328 10
        ApiFilter::pattern($pattern);
329
330 8
        $this->pathPattern = $pattern;
331
332 8
        return $this;
333
    }
334
335
    /**
336
     * Magic method for set or get request parameters.
337
     *
338
     * @param string $name
339
     * @param mixed  $arguments
340
     *
341
     * @return $this
342
     */
343 11
    public function __call($name, $arguments)
344
    {
345 11
        if (\strpos($name, 'get') === 0) {
346 1
            $parameterName = $this->propertyNameByMethodName($name);
347
348 1
            return $this->__get($parameterName);
349
        }
350
351 11
        if (\strpos($name, 'with') === 0) {
352 10
            $parameterName = $this->propertyNameByMethodName($name, 4);
353 10
            $this->__set($parameterName, $arguments[0]);
354 10
            $this->pathParameters[$parameterName] = $arguments[0];
355
356 10
            return $this;
357
        }
358
359 2
        if (\strpos($name, 'set') === 0) {
360 1
            $parameterName = $this->propertyNameByMethodName($name);
361 1
            $withMethod    = "with$parameterName";
362
363 1
            return $this->$withMethod($arguments[0]);
364
        }
365
366 1
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
367
    }
368
}
369