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 (#76)
by Yong
06:44 queued 01:58
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 48
    public function resolveParameters($credential)
32
    {
33 48
        $this->resolveQuery($credential);
34
35 48
        $this->options['query']['Signature'] = $this->signature(
36 48
            $this->options['query'],
37 48
            $credential->getAccessKeySecret()
38 48
        );
39
40 47
        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 47
    }
47
48
    /**
49
     * Resolve request query.
50
     *
51
     * @param AccessKeyCredential|BearerTokenCredential|StsCredential $credential
52
     *
53
     * @throws ClientException
54
     */
55 50
    private function resolveQuery($credential)
56
    {
57 50
        if (isset($this->options['query'])) {
58 24
            foreach ($this->options['query'] as $key => $value) {
59 24
                $this->options['query'][$key] = self::booleanValueToString($value);
60 24
            }
61 24
        }
62 50
        $signature                                  = $this->httpClient()->getSignature();
63 50
        $this->options['query']['RegionId']         = $this->realRegionId();
64 50
        $this->options['query']['AccessKeyId']      = $credential->getAccessKeyId();
65 50
        $this->options['query']['Format']           = $this->format;
66 50
        $this->options['query']['SignatureMethod']  = $signature->getMethod();
67 50
        $this->options['query']['SignatureVersion'] = $signature->getVersion();
68 50
        if ($signature->getType()) {
69 10
            $this->options['query']['SignatureType'] = $signature->getType();
70 10
        }
71 50
        $this->options['query']['SignatureNonce'] = md5(uniqid(mt_rand(), true));
72 50
        $this->options['query']['Timestamp']      = gmdate($this->dateTimeFormat);
73 50
        $this->options['query']['Action']         = $this->action;
74 50
        $this->options['query']['Version']        = $this->version;
75 50
        $this->resolveSecurityToken($credential);
76 50
        $this->resolveBearerToken($credential);
77 50
    }
78
79
    /**
80
     * Convert a Boolean value to a string.
81
     *
82
     * @param bool|string $value
83
     *
84
     * @return string
85
     */
86 31
    private static function booleanValueToString($value)
87
    {
88 31
        if (is_bool($value)) {
89 3
            if ($value) {
90 2
                return 'true';
91
            }
92
93 2
            return 'false';
94
        }
95
96 28
        return $value;
97
    }
98
99
    /**
100
     * @param CredentialsInterface $credential
101
     */
102 50
    private function resolveSecurityToken(CredentialsInterface $credential)
103
    {
104 50
        if ($credential instanceof StsCredential && $credential->getSecurityToken()) {
105 2
            $this->options['query']['SecurityToken'] = $credential->getSecurityToken();
106 2
        }
107 50
    }
108
109
    /**
110
     * @param CredentialsInterface $credential
111
     */
112 50
    private function resolveBearerToken(CredentialsInterface $credential)
113
    {
114 50
        if ($credential instanceof BearerTokenCredential) {
115 5
            $this->options['query']['BearerToken'] = $credential->getBearerToken();
116 5
        }
117 50
    }
118
119
    /**
120
     * Sign the parameters.
121
     *
122
     * @param array  $parameters
123
     * @param string $accessKeySecret
124
     *
125
     * @return mixed
126
     * @throws ClientException
127
     */
128 50
    private function signature($parameters, $accessKeySecret)
129
    {
130 50
        ksort($parameters);
131 50
        $canonicalizedQuery = '';
132 50
        foreach ($parameters as $key => $value) {
133 50
            $canonicalizedQuery .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
134 50
        }
135
136 50
        $this->stringToBeSigned = $this->method
137
                                  . '&%2F&'
138 50
                                  . $this->percentEncode(substr($canonicalizedQuery, 1));
139
140 50
        return $this->httpClient()
141 50
                    ->getSignature()
142 50
                    ->sign($this->stringToBeSigned, $accessKeySecret . '&');
143
    }
144
145
    /**
146
     * @param string $string
147
     *
148
     * @return null|string|string[]
149
     */
150 53
    protected function percentEncode($string)
151
    {
152 53
        $result = urlencode($string);
153 53
        $result = str_replace(['+', '*'], ['%20', '%2A'], $result);
154 53
        $result = preg_replace('/%7E/', '~', $result);
155
156 53
        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