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
04:59
created

RoaRequest::resolveParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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