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
Push — master ( 82f96f...07aa43 )
by Yong
05:43
created

RpcRequest::stringToBeSigned()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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