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 (#134)
by Yong
05:39
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 Method

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