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

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