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
04:40
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
     * @throws Exception
54
     */
55 18
    public function resolveParameter()
56
    {
57 18
        $this->resolveQuery();
58 18
        $this->resolveBody();
59 18
        $this->resolveHeaders();
60 18
    }
61
62 18
    private function resolveQuery()
63
    {
64 18
        if (!isset($this->options['query']['Version'])) {
65 18
            $this->options['query']['Version'] = $this->version;
66 18
        }
67 18
    }
68
69 18
    private function resolveBody()
70
    {
71 18
        if (isset($this->options['form_params']) && !isset($this->options['body'])) {
72 3
            $params = \AlibabaCloud\Client\arrayMerge(
73
                [
74 3
                    $this->data,
75 3
                    $this->options['form_params']
76 3
                ]
77 3
            );
78
79 3
            $this->options['body']                    = Encode::create($params)->ksort()->toString();
80 3
            $this->options['headers']['Content-MD5']  = base64_encode(md5($this->options['body'], true));
81 3
            $this->options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
82 3
            unset($this->options['form_params']);
83 3
        }
84 18
    }
85
86
    /**
87
     * @throws ClientException
88
     * @throws ServerException
89
     */
90 18
    private function resolveHeaders()
91
    {
92 18
        $this->options['headers']['x-acs-version']   = $this->version;
93 18
        $this->options['headers']['x-acs-region-id'] = $this->realRegionId();
94 18
        $this->options['headers']['Date']            = gmdate($this->dateTimeFormat);
95 18
        $this->options['headers']['Accept']          = Format::create($this->format)->toString();
96 18
        $this->resolveSignature();
97 18
        $this->resolveContentType();
98 18
        $this->resolveSecurityToken();
99 18
        $this->resolveBearerToken();
100 18
        $this->options['headers']['Authorization'] = $this->signature();
101 18
    }
102
103
    /**
104
     * @throws ClientException
105
     * @throws Exception
106
     */
107 18
    private function resolveSignature()
108
    {
109 18
        $signature                                           = $this->httpClient()->getSignature();
110 18
        $this->options['headers']['x-acs-signature-method']  = $signature->getMethod();
111 18
        $this->options['headers']['x-acs-signature-nonce']   = Uuid::uuid1()->toString();
112 18
        $this->options['headers']['x-acs-signature-version'] = $signature->getVersion();
113 18
        if ($signature->getType()) {
114 4
            $this->options['headers']['x-acs-signature-type'] = $signature->getType();
115 4
        }
116 18
    }
117
118 18
    private function resolveContentType()
119
    {
120 18
        if (!isset($this->options['headers']['Content-Type'])) {
121 15
            $this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']};chrset=utf-8";
122 15
        }
123 18
    }
124
125
    /**
126
     * @throws ClientException
127
     * @throws ServerException
128
     */
129 18
    private function resolveSecurityToken()
130
    {
131 18
        if ($this->credential() instanceof StsCredential && $this->credential()->getSecurityToken()) {
132
            $this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
133
        }
134 18
    }
135
136
    /**
137
     * @throws ClientException
138
     * @throws ServerException
139
     */
140 18
    private function resolveBearerToken()
141
    {
142 18
        if ($this->credential() instanceof BearerTokenCredential) {
143 4
            $this->options['headers']['x-acs-bearer-token'] = $this->credential()->getBearerToken();
144 4
        }
145 18
    }
146
147
    /**
148
     * Sign the request message.
149
     *
150
     * @return string
151
     * @throws ClientException
152
     * @throws ServerException
153
     */
154 18
    private function signature()
155
    {
156
        /**
157
         * @var AccessKeyCredential $credential
158
         */
159 18
        $credential  = $this->credential();
160 18
        $accessKeyId = $credential->getAccessKeyId();
161 18
        $signature   = $this->httpClient()
162 18
                            ->getSignature()
163 18
                            ->sign(
164 18
                                $this->stringToSign(),
165 18
                                $credential->getAccessKeySecret()
166 18
                            );
167
168 18
        return "acs $accessKeyId:$signature";
169
    }
170
171
    /**
172
     * @return string
173
     */
174 18
    public function stringToSign()
175
    {
176 18
        return $this->headerStringToSign() . $this->resourceStringToSign();
177
    }
178
179
    /**
180
     * @return string
181
     */
182 18
    private function headerStringToSign()
183
    {
184 18
        $string = $this->method . self::$headerSeparator;
185 18
        if (isset($this->options['headers']['Accept'])) {
186 18
            $string .= $this->options['headers']['Accept'];
187 18
        }
188 18
        $string .= self::$headerSeparator;
189
190 18
        if (isset($this->options['headers']['Content-MD5'])) {
191 3
            $string .= $this->options['headers']['Content-MD5'];
192 3
        }
193 18
        $string .= self::$headerSeparator;
194
195 18
        if (isset($this->options['headers']['Content-Type'])) {
196 18
            $string .= $this->options['headers']['Content-Type'];
197 18
        }
198 18
        $string .= self::$headerSeparator;
199
200 18
        if (isset($this->options['headers']['Date'])) {
201 18
            $string .= $this->options['headers']['Date'];
202 18
        }
203 18
        $string .= self::$headerSeparator;
204
205 18
        $string .= $this->acsHeaderString();
206
207 18
        return $string;
208
    }
209
210
    /**
211
     * Construct standard Header for Alibaba Cloud.
212
     *
213
     * @return string
214
     */
215 18
    private function acsHeaderString()
216
    {
217 18
        $array = [];
218 18
        foreach ($this->options['headers'] as $headerKey => $headerValue) {
219 18
            $key = strtolower($headerKey);
220 18
            if (strncmp($key, 'x-acs-', 6) === 0) {
221 18
                $array[$key] = $headerValue;
222 18
            }
223 18
        }
224 18
        ksort($array);
225 18
        $string = '';
226 18
        foreach ($array as $sortMapKey => $sortMapValue) {
227 18
            $string .= $sortMapKey . ':' . $sortMapValue . self::$headerSeparator;
228 18
        }
229
230 18
        return $string;
231
    }
232
233
    /**
234
     * @return string
235
     */
236 18
    private function resourceStringToSign()
237
    {
238 18
        $this->uri = $this->uri->withPath($this->resolvePath())
239 18
                               ->withQuery(
240 18
                                   Encode::create(isset($this->options['query'])
241 18
                                                      ? $this->options['query']
242 18
                                                      : [])
243 18
                                         ->ksort()
244 18
                                         ->toString()
245 18
                               );
246
247 18
        return $this->uri->getPath() . '?' . $this->uri->getQuery();
248
    }
249
250
    /**
251
     * Assign path parameters to the url.
252
     *
253
     * @return string
254
     */
255 20
    private function resolvePath()
256
    {
257 20
        $path = $this->pathPattern;
258 20
        foreach ($this->pathParameters as $pathKey => $value) {
259 12
            $target = "[$pathKey]";
260 12
            $path   = str_replace($target, $value, $path);
261 20
        }
262
263 20
        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 10
    public function pathPattern($pattern)
300
    {
301 10
        ApiFilter::pattern($pattern);
302
303 8
        $this->pathPattern = $pattern;
304
305 8
        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