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 ( 13e329...ecc4d0 )
by Yong
05:43
created

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