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

RpcRequest::resolveSecurityToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use AlibabaCloud\Client\Credentials\AccessKeyCredential;
6
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
7
use AlibabaCloud\Client\Credentials\CredentialsInterface;
8
use AlibabaCloud\Client\Credentials\StsCredential;
9
use AlibabaCloud\Client\Exception\ClientException;
10
11
/**
12
 * RESTful RPC Request.
13
 *
14
 * @package   AlibabaCloud\Client\Request
15
 */
16
class RpcRequest extends Request
17
{
18
19
    /**
20
     * @var string
21
     */
22
    private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
23
24
    /**
25
     * Resolve request parameter.
26
     *
27
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
28
     *
29
     * @throws ClientException
30 63
     */
31
    public function resolveParameters($credential)
32 63
    {
33 35
        $this->resolveQuery($credential);
34 35
35 35
        $this->options['query']['Signature'] = $this->signature(
36 35
            $this->options['query'],
37 63
            $credential->getAccessKeySecret()
38 63
        );
39 63
40 63
        if ($this->method === 'POST') {
41 63
            foreach ($this->options['query'] as $apiParamKey => $apiParamValue) {
42 63
                $this->options['form_params'][$apiParamKey] = $apiParamValue;
43 63
            }
44 22
            unset($this->options['query']);
45 22
        }
46 63
    }
47 63
48 63
    /**
49 63
     * Resolve request query.
50 63
     *
51 2
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
52 2
     *
53 63
     * @throws ClientException
54 9
     */
55 9
    private function resolveQuery($credential)
56 63
    {
57
        if (isset($this->options['query'])) {
58
            foreach ($this->options['query'] as $key => $value) {
59
                $this->options['query'][$key] = self::booleanValueToString($value);
60
            }
61
        }
62
        $signature                                  = $this->httpClient()->getSignature();
63
        $this->options['query']['RegionId']         = $this->realRegionId();
64
        $this->options['query']['AccessKeyId']      = $credential->getAccessKeyId();
65 61
        $this->options['query']['Format']           = $this->format;
66
        $this->options['query']['SignatureMethod']  = $signature->getMethod();
67 61
        $this->options['query']['SignatureVersion'] = $signature->getVersion();
68
        if ($signature->getType()) {
69 61
            $this->options['query']['SignatureType'] = $signature->getType();
70 61
        }
71 61
        $this->options['query']['SignatureNonce'] = md5(uniqid(mt_rand(), true));
72 61
        $this->options['query']['Timestamp']      = gmdate($this->dateTimeFormat);
73
        $this->options['query']['Action']         = $this->action;
74 52
        $this->options['query']['Version']        = $this->version;
75 33
        $this->resolveSecurityToken($credential);
76 33
        $this->resolveBearerToken($credential);
77 33
    }
78 33
79 33
    /**
80 52
     * @param CredentialsInterface $credential
81
     */
82
    private function resolveSecurityToken(CredentialsInterface $credential)
83
    {
84
        if ($credential instanceof StsCredential && $credential->getSecurityToken()) {
85
            $this->options['query']['SecurityToken'] = $credential->getSecurityToken();
86
        }
87
    }
88
89 42
    /**
90
     * @param CredentialsInterface $credential
91 42
     */
92 3
    private function resolveBearerToken(CredentialsInterface $credential)
93 2
    {
94
        if ($credential instanceof BearerTokenCredential) {
95
            $this->options['query']['BearerToken'] = $credential->getBearerToken();
96 2
        }
97
    }
98
99 39
    /**
100
     * Convert a Boolean value to a string.
101
     *
102
     * @param bool|string $value
103
     *
104
     * @return string
105
     */
106
    private static function booleanValueToString($value)
107
    {
108
        if (is_bool($value)) {
109
            if ($value) {
110
                return 'true';
111 63
            }
112
113 63
            return 'false';
114 63
        }
115 63
116 63
        return $value;
117 63
    }
118
119 63
    /**
120
     * Sign the parameters.
121 63
     *
122
     * @param array  $parameters
123 63
     * @param string $accessKeySecret
124 63
     *
125 63
     * @return mixed
126
     * @throws ClientException
127
     */
128
    private function signature($parameters, $accessKeySecret)
129
    {
130
        ksort($parameters);
131
        $canonicalizedQuery = '';
132
        foreach ($parameters as $key => $value) {
133 66
            $canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
134
        }
135 66
136 66
        $this->stringToBeSigned = $this->method
137 66
                                  . '&%2F&'
138
                                  . $this->percentEncode(substr($canonicalizedQuery, 1));
139 66
140
        return $this->httpClient()
141
                    ->getSignature()
142
                    ->sign($this->stringToBeSigned, $accessKeySecret . '&');
143
    }
144
145
    /**
146
     * @param string $string
147
     *
148
     * @return null|string|string[]
149
     */
150 2
    protected function percentEncode($string)
151
    {
152 2
        $result = urlencode($string);
153 2
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
154
        $result = preg_replace('/%7E/', '~', $result);
155 2
156
        return $result;
157
    }
158 2
159 2
    /**
160 2
     * Magic method for set or get request parameters.
161 2
     *
162 2
     * @param string $name
163
     * @param mixed  $arguments
164 2
     *
165
     * @return $this
166
     */
167
    public function __call($name, $arguments)
168
    {
169
        if (\strpos($name, 'get') !== false) {
170
            $parameterName = $this->propertyNameByMethodName($name);
171
172
            return $this->__get($parameterName);
173
        }
174
175
        if (\strpos($name, 'with') !== false) {
176
            $parameterName = $this->propertyNameByMethodName($name, 4);
177
            $this->__set($parameterName, $arguments[0]);
178
            $this->options['query'][$parameterName] = $arguments[0];
179
        }
180
181
        return $this;
182
    }
183
}
184