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.
Completed
Pull Request — master (#39)
by Yong
04:33
created

RpcRequest::signature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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