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 Setup Failed
Pull Request — master (#162)
by Yong
03:00
created

RoaRequest::resolveUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 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
    public function resolveParameter()
56
    {
57 18
        $this->resolveQuery();
58
        $this->resolveHeaders();
59 18
        $this->resolveBody();
60 18
        $this->resolveUri();
61 18
        $this->resolveSignature();
62 18
    }
63 18
64
    private function resolveQuery()
65 18
    {
66
        if (!isset($this->options['query']['Version'])) {
67 18
            $this->options['query']['Version'] = $this->version;
68 18
        }
69 18
    }
70 18
71
    private function resolveBody()
72 18
    {
73
        // If the body has already been specified, it will not be resolved.
74
        if (isset($this->options['body'])) {
75 18
            return;
76 1
        }
77
78
        if (!isset($this->options['form_params'])) {
79 17
            return;
80 14
        }
81
82
        // Merge data, compatible with parameters set from constructor.
83
        $params = Arrays::merge(
84 3
            [
85
                $this->data,
86 3
                $this->options['form_params']
87 3
            ]
88 3
        );
89 3
90
        $this->encodeBody($params);
91 3
92
        unset($this->options['form_params']);
93 3
    }
94 3
95
    /**
96
     * Determine the body format based on the Content-Type and calculate the MD5 value.
97
     *
98
     * @param array $params
99
     */
100
    private function encodeBody(array $params)
101 3
    {
102
        $stringy = Stringy::create($this->options['headers']['Content-Type']);
103 3
104
        if ($stringy->contains('application/json', false)) {
105 3
            $this->options['body']                   = json_encode($params);
106 2
            $this->options['headers']['Content-MD5'] = base64_encode(md5($this->options['body'], true));
107 2
108
            return;
109 2
        }
110
111
        $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 1
116
    /**
117
     * @throws ClientException
118
     * @throws ServerException
119
     * @throws Exception
120
     */
121
    private function resolveHeaders()
122 18
    {
123
        $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 18
127
        $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 18
            $this->options['headers']['x-acs-signature-type'] = $signature->getType();
133 4
        }
134 4
135
        $this->resolveAccept();
136 18
        $this->resolveContentType();
137 18
        $this->resolveSecurityToken();
138 18
        $this->resolveBearerToken();
139 18
    }
140 18
141
    /**
142
     * @throws ClientException
143
     * @throws Exception
144
     */
145
    private function resolveSignature()
146 18
    {
147
        $this->options['headers']['Authorization'] = $this->signature();
148 18
    }
149 18
150
    /**
151
     * If accept is not specified, it is determined by format.
152
     */
153
    private function resolveAccept()
154 18
    {
155
        if (!isset($this->options['headers']['Accept'])) {
156 18
            $this->options['headers']['Accept'] = Accept::create($this->format)->toString();
157 17
        }
158 17
    }
159 18
160
    /**
161
     * If the Content-Type is not specified, it is determined according to accept.
162
     */
163
    private function resolveContentType()
164 18
    {
165
        if (!isset($this->options['headers']['Content-Type'])) {
166 18
            $this->options['headers']['Content-Type'] = "{$this->options['headers']['Accept']}; charset=utf-8";
167 17
        }
168 17
    }
169 18
170
    /**
171
     * @throws ClientException
172
     * @throws ServerException
173
     */
174
    private function resolveSecurityToken()
175 20
    {
176
        if (!$this->credential() instanceof StsCredential) {
177 20
            return;
178 18
        }
179
180
        if (!$this->credential()->getSecurityToken()) {
181 2
            return;
182 1
        }
183
184
        $this->options['headers']['x-acs-security-token'] = $this->credential()->getSecurityToken();
185 1
    }
186 1
187
    /**
188
     * @throws ClientException
189
     * @throws ServerException
190
     */
191
    private function resolveBearerToken()
192 18
    {
193
        if ($this->credential() instanceof BearerTokenCredential) {
194 18
            $this->options['headers']['x-acs-bearer-token'] = $this->credential()->getBearerToken();
195 4
        }
196 4
    }
197 18
198
    /**
199
     * Sign the request message.
200
     *
201
     * @return string
202
     * @throws ClientException
203
     * @throws ServerException
204
     */
205
    private function signature()
206 18
    {
207
        /**
208
         * @var AccessKeyCredential $credential
209
         */
210
        $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 18
219
        return "acs $access_key_id:$signature";
220 18
    }
221
222
    /**
223
     * @return void
224
     */
225
    private function resolveUri()
226 18
    {
227
        $query = isset($this->options['query'])
228 18
            ? $this->options['query']
229
            : [];
230
        $path  = Path::assign($this->pathPattern, $this->pathParameters);
231
232
        $this->uri = $this->uri->withPath($path)
233
                               ->withQuery(
234 18
                                   Encode::create($query)->ksort()->toString()
235
                               );
236 18
    }
237 18
238 18
    /**
239 18
     * @return string
240 18
     */
241
    public function stringToSign()
242 18
    {
243 3
        $query = isset($this->options['query'])
244 3
            ? $this->options['query']
245 18
            : [];
246
        $query = Encode::create($query)->ksort()->toString();
247 18
        $uri   = $this->uri->withQuery($query);
248 18
249 18
        $request = new \GuzzleHttp\Psr7\Request(
250 18
            $this->method,
251
            $uri,
252 18
            $this->options['headers']
253 18
        );
254 18
255 18
        return Sign::roaString($request);
256
    }
257 18
258
    /**
259 18
     * Set path parameter by name.
260
     *
261
     * @param string $name
262
     * @param string $value
263
     *
264
     * @return RoaRequest
265
     * @throws ClientException
266
     */
267 18
    public function pathParameter($name, $value)
268
    {
269 18
        Filter::name($name);
270 18
271 18
        if ($value === '') {
272 18
            throw new ClientException(
273 18
                'Value cannot be empty',
274 18
                SDK::INVALID_ARGUMENT
275 18
            );
276 18
        }
277 18
278 18
        $this->pathParameters[$name] = $value;
279 18
280 18
        return $this;
281
    }
282 18
283
    /**
284
     * Set path pattern.
285
     *
286
     * @param string $pattern
287
     *
288 18
     * @return self
289
     * @throws ClientException
290 18
     */
291 18
    public function pathPattern($pattern)
292 18
    {
293 18
        ApiFilter::pattern($pattern);
294 18
295 18
        $this->pathPattern = $pattern;
296 18
297 18
        return $this;
298
    }
299 18
300
    /**
301
     * Magic method for set or get request parameters.
302
     *
303
     * @param string $name
304
     * @param mixed  $arguments
305
     *
306
     * @return $this
307 20
     */
308
    public function __call($name, $arguments)
309 20
    {
310 20
        if (strncmp($name, 'get', 3) === 0) {
311 12
            $parameterName = $this->propertyNameByMethodName($name);
312 12
313 20
            return $this->__get($parameterName);
314
        }
315 20
316
        if (strncmp($name, 'with', 4) === 0) {
317
            $parameterName = $this->propertyNameByMethodName($name, 4);
318
            $this->__set($parameterName, $arguments[0]);
319
            $this->pathParameters[$parameterName] = $arguments[0];
320
321
            return $this;
322
        }
323
324
        if (strncmp($name, 'set', 3) === 0) {
325
            $parameterName = $this->propertyNameByMethodName($name);
326
            $withMethod    = "with$parameterName";
327 11
328
            throw new RuntimeException("Please use $withMethod instead of $name");
329 11
        }
330
331 9
        throw new RuntimeException('Call to undefined method ' . __CLASS__ . '::' . $name . '()');
332 1
    }
333
}
334