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 (#30)
by Yong
03:09
created

ServerException::distinguishSignatureAndCredentialError()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Exception;
4
5
use AlibabaCloud\Client\Result\Result;
6
use Stringy\Stringy;
7
8
/**
9
 * Class ServerException
10
 *
11
 * @package   AlibabaCloud\Client\Exception
12
 */
13
class ServerException extends AlibabaCloudException
14
{
15
16
    /**
17
     * @var string
18
     */
19
    protected $requestId;
20
21
    /**
22
     * @var Result
23
     */
24
    protected $result;
25
26
    /**
27
     * ServerException constructor.
28
     *
29
     * @param Result|null $result
30
     * @param string      $errorMessage
31
     * @param string      $errorCode
32 45
     */
33
    public function __construct(Result $result, $errorMessage = '', $errorCode = '')
34 45
    {
35 45
        $this->result = $result;
36 45
        $this->settingProperties();
37 1
38
        if ($errorCode !== '') {
39 45
            $this->errorCode = $errorCode;
40 1
        }
41
42 45
        if ($errorMessage !== '') {
43 3
            $this->errorMessage = $errorMessage;
44
        }
45
46 45
        if (!$this->errorMessage) {
47 45
            $this->errorMessage = (string)$this->result->getResponse()->getBody();
48 45
        }
49
50 45
        $this->distinguishSignatureAndCredentialError();
51
52
        parent::__construct(
53
            $this->getParentMessage(),
54
            $this->result->getResponse()->getStatusCode()
55 45
        );
56
    }
57 45
58 45
    /**
59 45
     * If the string to be signed are the same with server's, it is considered a credential error.
60 45
     */
61 43
    private function distinguishSignatureAndCredentialError()
62
    {
63 45
        if ($this->result->getRequest()
64
            && Stringy::create($this->errorMessage)->contains($this->result->getRequest()->stringToBeSigned)) {
65
            $this->errorCode    = 'InvalidAccessKeySecret';
66
            $this->errorMessage = 'Specified Access Key Secret is not valid.';
67
        }
68
    }
69 45
70
    /**
71 45
     * @return string
72 1
     */
73 1
    private function getParentMessage()
74
    {
75 45
        $data = $this->result->toArray();
76 42
        \ksort($data);
77 42
        $message = '';
78
        foreach ($data as $key => $value) {
79 45
            $message .= "$key:$value ";
80 1
        }
81 1
        return $message;
82
    }
83 45
84 1
    /**
85
     * @return void
86 45
     */
87 42
    private function settingProperties()
88
    {
89 45
        if (isset($this->result['message'])) {
90
            $this->errorMessage = $this->result['message'];
91
            $this->errorCode    = $this->result['code'];
92
        }
93
        if (isset($this->result['Message'])) {
94
            $this->errorMessage = $this->result['Message'];
95
            $this->errorCode    = $this->result['Code'];
96
        }
97
        if (isset($this->result['errorMsg'])) {
98
            $this->errorMessage = $this->result['errorMsg'];
99
            $this->errorCode    = $this->result['errorCode'];
100
        }
101
        if (isset($this->result['requestId'])) {
102
            $this->requestId = $this->result['requestId'];
103
        }
104
        if (isset($this->result['RequestId'])) {
105
            $this->requestId = $this->result['RequestId'];
106 3
        }
107
    }
108 3
109
    /**
110
     * @codeCoverageIgnore
111
     *
112
     * @deprecated deprecated since version 2.0.
113
     *
114 1
     * @return string
115
     */
116 1
    public function getErrorType()
117
    {
118
        return 'Server';
119
    }
120
121
    /**
122
     * @return Result
123
     */
124
    public function getResult()
125
    {
126
        return $this->result;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getRequestId()
133
    {
134
        return $this->requestId;
135
    }
136
137
    /**
138
     * @codeCoverageIgnore
139
     * @deprecated deprecated since version 2.0.
140
     *
141
     * @return int
142
     */
143
    public function getHttpStatus()
144
    {
145
        return $this->getResult()->getResponse()->getStatusCode();
146
    }
147
}
148