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
Push — master ( 8df5f5...7eb4cd )
by Yong
05:06
created

RpcRequest::resolveCommonParameters()   F

Complexity

Conditions 15
Paths 2048

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 15

Importance

Changes 0
Metric Value
cc 15
eloc 26
nc 2048
nop 1
dl 0
loc 51
ccs 39
cts 39
cp 1
crap 15
rs 1.7499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Exception;
11
use Ramsey\Uuid\Uuid;
12
use RuntimeException;
13
14
/**
15
 * RESTful RPC Request.
16
 *
17
 * @package   AlibabaCloud\Client\Request
18
 */
19
class RpcRequest extends Request
20
{
21
22
    /**
23
     * @var string
24
     */
25
    private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
26
27
    /**
28
     * Resolve request parameter.
29
     *
30
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
31
     *
32
     * @throws ClientException
33
     */
34 58
    public function resolveParameters($credential)
35
    {
36 58
        $this->resolveCommonParameters($credential);
37 58
        $this->options['query']['Signature'] = $this->signature($credential->getAccessKeySecret());
38 57
        $this->repositionParameters();
39 57
    }
40
41
    /**
42
     * Resolve Common Parameters.
43
     *
44
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
45
     *
46
     * @throws ClientException
47
     * @throws Exception
48
     */
49 60
    private function resolveCommonParameters($credential)
50
    {
51 60
        if (isset($this->options['query'])) {
52 27
            foreach ($this->options['query'] as $key => $value) {
53 27
                $this->options['query'][$key] = self::booleanValueToString($value);
54 27
            }
55 27
        }
56
57 60
        $signature = $this->httpClient()->getSignature();
58 60
        if (!isset($this->options['query']['AccessKeyId']) && $credential->getAccessKeyId()) {
59 55
            $this->options['query']['AccessKeyId'] = $credential->getAccessKeyId();
60 55
        }
61
62 60
        if (!isset($this->options['query']['RegionId'])) {
63 60
            $this->options['query']['RegionId'] = $this->realRegionId();
64 60
        }
65
66 60
        if (!isset($this->options['query']['Format'])) {
67 60
            $this->options['query']['Format'] = $this->format;
68 60
        }
69
70 60
        if (!isset($this->options['query']['SignatureMethod'])) {
71 60
            $this->options['query']['SignatureMethod'] = $signature->getMethod();
72 60
        }
73
74 60
        if (!isset($this->options['query']['SignatureVersion'])) {
75 60
            $this->options['query']['SignatureVersion'] = $signature->getVersion();
76 60
        }
77
78 60
        if (!isset($this->options['query']['SignatureType']) && $signature->getType()) {
79 12
            $this->options['query']['SignatureType'] = $signature->getType();
80 12
        }
81
82 60
        if (!isset($this->options['query']['SignatureNonce'])) {
83 60
            $this->options['query']['SignatureNonce'] = Uuid::uuid1()->toString();
84 60
        }
85
86 60
        if (!isset($this->options['query']['Timestamp'])) {
87 60
            $this->options['query']['Timestamp'] = gmdate($this->dateTimeFormat);
88 60
        }
89
90 60
        if (!isset($this->options['query']['Action'])) {
91 60
            $this->options['query']['Action'] = $this->action;
92 60
        }
93
94 60
        if (!isset($this->options['query']['Version'])) {
95 60
            $this->options['query']['Version'] = $this->version;
96 60
        }
97
98 60
        $this->resolveSecurityToken($credential);
99 60
        $this->resolveBearerToken($credential);
100 60
    }
101
102
    /**
103
     * Adjust parameter position
104
     */
105 57
    private function repositionParameters()
106
    {
107 57
        if ($this->method === 'POST' || $this->method === 'PUT') {
108 32
            foreach ($this->options['query'] as $apiParamKey => $apiParamValue) {
109 32
                $this->options['form_params'][$apiParamKey] = $apiParamValue;
110 32
            }
111 32
            unset($this->options['query']);
112 32
        }
113 57
    }
114
115
    /**
116
     * Convert a Boolean value to a string.
117
     *
118
     * @param bool|string $value
119
     *
120
     * @return string
121
     */
122 34
    private static function booleanValueToString($value)
123
    {
124 34
        if (is_bool($value)) {
125 3
            return $value ? 'true' : 'false';
126
        }
127
128 31
        return $value;
129
    }
130
131
    /**
132
     * @param CredentialsInterface $credential
133
     */
134 60
    private function resolveSecurityToken(CredentialsInterface $credential)
135
    {
136 60
        if ($credential instanceof StsCredential && $credential->getSecurityToken()) {
137 2
            $this->options['query']['SecurityToken'] = $credential->getSecurityToken();
138 2
        }
139 60
    }
140
141
    /**
142
     * @param CredentialsInterface $credential
143
     */
144 60
    private function resolveBearerToken(CredentialsInterface $credential)
145
    {
146 60
        if ($credential instanceof BearerTokenCredential) {
147 5
            $this->options['query']['BearerToken'] = $credential->getBearerToken();
148 5
        }
149 60
    }
150
151
    /**
152
     * Sign the parameters.
153
     *
154
     * @param string $accessKeySecret
155
     *
156
     * @return mixed
157
     * @throws ClientException
158
     */
159 60
    private function signature($accessKeySecret)
160
    {
161 60
        return $this->httpClient()
162 60
                    ->getSignature()
163 60
                    ->sign(
164 60
                        $this->stringToSign(),
165
                        $accessKeySecret . '&'
166 60
                    );
167
    }
168
169
    /**
170
     * @return string
171
     */
172 60
    public function stringToSign()
173
    {
174 60
        $query       = isset($this->options['query']) ? $this->options['query'] : [];
175 60
        $form_params = isset($this->options['form_params']) ? $this->options['form_params'] : [];
176 60
        $parameters  = \AlibabaCloud\Client\arrayMerge([$query, $form_params]);
177 60
        ksort($parameters);
178 60
        $canonicalizedQuery = '';
179 60
        foreach ($parameters as $key => $value) {
180 60
            $canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
181 60
        }
182
183 60
        return $this->method . '&%2F&' . $this->percentEncode(substr($canonicalizedQuery, 1));
184
    }
185
186
    /**
187
     * @param string $string
188
     *
189
     * @return null|string|string[]
190
     */
191 63
    private function percentEncode($string)
192
    {
193 63
        $result = urlencode($string);
194 63
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
195 63
        $result = preg_replace('/%7E/', '~', $result);
196
197 63
        return $result;
198
    }
199
200
    /**
201
     * Magic method for set or get request parameters.
202
     *
203
     * @param string $name
204
     * @param mixed  $arguments
205
     *
206
     * @return $this
207
     */
208 2
    public function __call($name, $arguments)
209
    {
210 2
        if (\strpos($name, 'get') === 0) {
211 1
            $parameterName = $this->propertyNameByMethodName($name);
212
213 1
            return $this->__get($parameterName);
214
        }
215
216 2
        if (\strpos($name, 'with') === 0) {
217 1
            $parameterName = $this->propertyNameByMethodName($name, 4);
218 1
            $this->__set($parameterName, $arguments[0]);
219 1
            $this->options['query'][$parameterName] = $arguments[0];
220
221 1
            return $this;
222
        }
223
224 2
        if (\strpos($name, 'set') === 0) {
225 1
            $parameterName = $this->propertyNameByMethodName($name);
226 1
            $withMethod    = "with$parameterName";
227
228 1
            return $this->$withMethod($arguments[0]);
229
        }
230
231 1
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
232
    }
233
}
234