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 (#162)
by Yong
04:44
created

RoaRequest::headerStringToSign()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 16
nop 0
dl 0
loc 26
ccs 20
cts 20
cp 1
crap 5
rs 9.4555
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A RoaRequest::stringToSign() 0 15 2
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use Exception;
6
use Stringy\Stringy;
7
use Ramsey\Uuid\Uuid;
8
use RuntimeException;
9
use AlibabaCloud\Client\SDK;
10
use AlibabaCloud\Client\Encode;
11
use AlibabaCloud\Client\Accept;
12
use AlibabaCloud\Client\Support\Path;
13
use AlibabaCloud\Client\Support\Sign;
14
use AlibabaCloud\Client\Filter\Filter;
15
use AlibabaCloud\Client\Support\Arrays;
16
use AlibabaCloud\Client\Filter\ApiFilter;
17
use AlibabaCloud\Client\Credentials\StsCredential;
18
use AlibabaCloud\Client\Exception\ClientException;
19
use AlibabaCloud\Client\Exception\ServerException;
20
use AlibabaCloud\Client\Credentials\AccessKeyCredential;
21
use AlibabaCloud\Client\Credentials\BearerTokenCredential;
22
use AlibabaCloud\Client\Request\Traits\DeprecatedRoaTrait;
23
24
/**
25
 * RESTful ROA Request.
26
 *
27
 * @package   AlibabaCloud\Client\Request
28
 * @method setParameter()
29
 */
30
class RoaRequest extends Request
31
{
32
    use DeprecatedRoaTrait;
33
34
    /**
35
     * @var string
36
     */
37
    public $pathPattern = '/';
38
39
    /**
40
     * @var array
41
     */
42
    public $pathParameters = [];
43
44
    /**
45
     * @var string
46
     */
47
    private $dateTimeFormat = "D, d M Y H:i:s \G\M\T";
48
49
    /**
50
     * Resolve request parameter.
51
     *
52
     * @throws ClientException
53
     * @throws Exception
54
     */
55 18
    public function resolveParameter()
56
    {
57 18
        $this->resolveQuery();
58 18
        $this->resolveHeaders();
59 18
        $this->resolveBody();
60 18
        $this->resolveUri();
61 18
        $this->resolveSignature();
62 18
    }
63
64 18
    private function resolveQuery()
65
    {
66 18
        if (!isset($this->options['query']['Version'])) {
67 18
            $this->options['query']['Version'] = $this->version;
68 18
        }
69 18
    }
70
71 18
    private function resolveBody()
72
    {
73
        // If the body has already been specified, it will not be resolved.
74 18
        if (isset($this->options['body'])) {
75 1
            return;
76
        }
77
78 17
        if (!isset($this->options['form_params'])) {
79 14
            return;
80
        }
81
82
        // Merge data, compatible with parameters set from constructor.
83 3
        $params = Arrays::merge(
84
            [
85 3
                $this->data,
86 3
                $this->options['form_params']
87 3
            ]
88 3
        );
89
90 3
        $this->encodeBody($params);
91
92 3
        unset($this->options['form_params']);
93 3
    }
94
95
    /**
96
     * Determine the body format based on the Content-Type and calculate the MD5 value.
97
     *
98
     * @param array $params
99
     */
100 3
    private function encodeBody(array $params)
101
    {
102 3
        $stringy = Stringy::create($this->options['headers']['Content-Type']);
103
104 3
        if ($stringy->contains('application/json', false)) {
105 2
            $this->options['body']                   = json_encode($params);
106 2
            $this->options['headers']['Content-MD5'] = base64_encode(md5($this->options['body'], true));
107
108 2
            return;
109
        }
110
111 1
        $this->options['body']                    = Encode::create($params)->ksort()->toString();
112 1
        $this->options['headers']['Content-MD5']  = base64_encode(md5($this->options['body'], true));
113 1
        $this->options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
114 1
    }
115
116
    /**
117
     * @throws ClientException
118
     * @throws ServerException
119
     * @throws Exception
120
     */
121 18
    private function resolveHeaders()
122
    {
123 18
        $this->options['headers']['x-acs-version']   = $this->version;
124 18
        $this->options['headers']['x-acs-region-id'] = $this->realRegionId();
125 18
        $this->options['headers']['Date']            = gmdate($this->dateTimeFormat);
126
127 18
        $signature                                           = $this->httpClient()->getSignature();
128 18
        $this->options['headers']['x-acs-signature-method']  = $signature->getMethod();
129 18
        $this->options['headers']['x-acs-signature-nonce']   = Uuid::uuid1()->toString();
130 18
        $this->options['headers']['x-acs-signature-version'] = $signature->getVersion();
131 18
        if ($signature->getType()) {
132 4
            $this->options['headers']['x-acs-signature-type'] = $signature->getType();
133 4
        }
134
135 18
        $this->resolveAccept();
136 18
        $this->resolveContentType();
137 18
        $this->resolveSecurityToken();
138 18
        $this->resolveBearerToken();
139 18
    }
140
141
    /**
142
     * @throws ClientException
143
     * @throws Exception
144
     */
145 18
    private function resolveSignature()
146
    {
147 18
        $this->options['headers']['Authorization'] = $this->signature();
148 18
    }
149
150
    /**
151
     * If accept is not specified, it is determined by format.
152
     */
153 18
    private function resolveAccept()
154
    {
155 18
        if (!isset($this->options['headers']['Accept'])) {
156 17
            $this->options['headers']['Accept'] = Accept::create($this->format)->toString();
157 17
        }
158 18
    }
159
160
    /**
161
     * If the Content-Type is not specified, it is determined according to accept.
162
     */
163 18
    private function resolveContentType()
164
    {
165 18
        if (!isset($this->options['headers']['Content-Type'])) {
166 17
            $this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']}; charset=utf-8";
167 17
        }
168 18
    }
169
170
    /**
171
     * @throws ClientException
172
     * @throws ServerException
173
     */
174 20
    private function resolveSecurityToken()
175
    {
176 20
        if (!$this->credential() instanceof StsCredential) {
177 18
            return;
178
        }
179
180 2
        if (!$this->credential()->getSecurityToken()) {
181 1
            return;
182
        }
183
184 1
        $this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
185 1
    }
186
187
    /**
188
     * @throws ClientException
189
     * @throws ServerException
190
     */
191 18
    private function resolveBearerToken()
192
    {
193 18
        if ($this->credential() instanceof BearerTokenCredential) {
194 4
            $this->options['headers']['x-acs-bearer-token'] = $this->credential()->getBearerToken();
195 4
        }
196 18
    }
197
198
    /**
199
     * Sign the request message.
200
     *
201
     * @return string
202
     * @throws ClientException
203
     * @throws ServerException
204
     */
205 18
    private function signature()
206
    {
207
        /**
208
         * @var AccessKeyCredential $credential
209
         */
210 18
        $credential    = $this->credential();
211 18
        $access_key_id = $credential->getAccessKeyId();
212 18
        $signature     = $this->httpClient()
213 18
                              ->getSignature()
214 18
                              ->sign(
215 18
                                  $this->stringToSign(),
216 18
                                  $credential->getAccessKeySecret()
217 18
                              );
218
219 18
        return "acs $access_key_id:$signature";
220
    }
221
222
    /**
223
     * @return void
224
     */
225 18
    private function resolveUri()
226
    {
227 18
        $query = isset($this->options['query'])
228 18
            ? $this->options['query']
229 18
            : [];
230 18
        $path  = Path::assign($this->pathPattern, $this->pathParameters);
231
232 18
        $this->uri = $this->uri->withPath($path)
233 18
                               ->withQuery(
234 18
                                   Encode::create($query)->ksort()->toString()
235 18
                               );
236 18
    }
237
238
    /**
239
     * @return string
240
     */
241 18
    public function stringToSign()
242
    {
243 18
        $query = isset($this->options['query'])
244 18
            ? $this->options['query']
245 18
            : [];
246 18
        $query = Encode::create($query)->ksort()->toString();
247 18
        $uri   = $this->uri->withQuery($query);
248
249 18
        $request = new \GuzzleHttp\Psr7\Request(
250 18
            $this->method,
251 18
            $uri,
252 18
            $this->options['headers']
253 18
        );
254
255 18
        return Sign::roaString($request);
256
    }
257
258
    /**
259
     * Set path parameter by name.
260
     *
261
     * @param string $name
262
     * @param string $value
263
     *
264
     * @return RoaRequest
265
     * @throws ClientException
266
     */
267 11
    public function pathParameter($name, $value)
268
    {
269 11
        Filter::name($name);
270
271 9
        if ($value === '') {
272 1
            throw new ClientException(
273 1
                'Value cannot be empty',
274
                SDK::INVALID_ARGUMENT
275 1
            );
276
        }
277
278 8
        $this->pathParameters[$name] = $value;
279
280 8
        return $this;
281
    }
282
283
    /**
284
     * Set path pattern.
285
     *
286
     * @param string $pattern
287
     *
288
     * @return self
289
     * @throws ClientException
290
     */
291 10
    public function pathPattern($pattern)
292
    {
293 10
        ApiFilter::pattern($pattern);
294
295 8
        $this->pathPattern = $pattern;
296
297 8
        return $this;
298
    }
299
300
    /**
301
     * Magic method for set or get request parameters.
302
     *
303
     * @param string $name
304
     * @param mixed  $arguments
305
     *
306
     * @return $this
307
     */
308 12
    public function __call($name, $arguments)
309
    {
310 12
        if (strncmp($name, 'get', 3) === 0) {
311 1
            $parameterName = $this->propertyNameByMethodName($name);
312
313 1
            return $this->__get($parameterName);
314
        }
315
316 12
        if (strncmp($name, 'with', 4) === 0) {
317 10
            $parameterName = $this->propertyNameByMethodName($name, 4);
318 10
            $this->__set($parameterName, $arguments[0]);
319 10
            $this->pathParameters[$parameterName] = $arguments[0];
320
321 10
            return $this;
322
        }
323
324 2
        if (strncmp($name, 'set', 3) === 0) {
325 1
            $parameterName = $this->propertyNameByMethodName($name);
326 1
            $withMethod    = "with$parameterName";
327
328 1
            throw new RuntimeException("Please use $withMethod instead of $name");
329
        }
330
331 1
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
332
    }
333
}
334