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

ServerException::distinguishSignatureAndCredentialError()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 8
ccs 4
cts 6
cp 0.6667
crap 3.3332
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
     */
33 45
    public function __construct(Result $result, $errorMessage = '', $errorCode = '')
34
    {
35 45
        $this->result = $result;
36 45
        $this->settingProperties();
37
38 45
        if ($errorCode !== '') {
39 1
            $this->errorCode = $errorCode;
40
        }
41
42 45
        if ($errorMessage !== '') {
43 1
            $this->errorMessage = $errorMessage;
44
        }
45
46 45
        if (!$this->errorMessage) {
47 3
            $this->errorMessage = (string)$this->result->getResponse()->getBody();
48
        }
49
50 45
        $this->distinguishSignatureAndCredentialError();
51
52 45
        parent::__construct(
53 45
            $this->getParentMessage(),
54 45
            $this->result->getResponse()->getStatusCode()
55
        );
56 45
    }
57
58 45
    private function distinguishSignatureAndCredentialError()
59
    {
60 45
        if ($this->result->getRequest()) {
61 43
            $same = Stringy::create($this->errorMessage)->contains($this->result->getRequest()->stringToBeSigned);
62
            // If the string to be signed are the same with server, it is considered a credential error.
63 43
            if ($same) {
64
                $this->errorCode    = 'InvalidAccessKeySecret';
65
                $this->errorMessage = 'Specified Access Key Secret is not valid.';
66
            }
67
        }
68 45
    }
69
70
    /**
71
     * @return string
72
     */
73 45
    private function getParentMessage()
74
    {
75 45
        $data = $this->result->toArray();
76 45
        \ksort($data);
77 45
        $message = '';
78 45
        foreach ($data as $key => $value) {
79 43
            $message .= "$key:$value ";
80
        }
81 45
        return $message;
82
    }
83
84
    /**
85
     * @return void
86
     */
87 45
    private function settingProperties()
88
    {
89 45
        if (isset($this->result['message'])) {
90 1
            $this->errorMessage = $this->result['message'];
91 1
            $this->errorCode    = $this->result['code'];
92
        }
93 45
        if (isset($this->result['Message'])) {
94 42
            $this->errorMessage = $this->result['Message'];
95 42
            $this->errorCode    = $this->result['Code'];
96
        }
97 45
        if (isset($this->result['errorMsg'])) {
98 1
            $this->errorMessage = $this->result['errorMsg'];
99 1
            $this->errorCode    = $this->result['errorCode'];
100
        }
101 45
        if (isset($this->result['requestId'])) {
102 1
            $this->requestId = $this->result['requestId'];
103
        }
104 45
        if (isset($this->result['RequestId'])) {
105 42
            $this->requestId = $this->result['RequestId'];
106
        }
107 45
    }
108
109
    /**
110
     * @codeCoverageIgnore
111
     *
112
     * @deprecated deprecated since version 2.0.
113
     *
114
     * @return string
115
     */
116
    public function getErrorType()
117
    {
118
        return 'Server';
119
    }
120
121
    /**
122
     * @return Result
123
     */
124 3
    public function getResult()
125
    {
126 3
        return $this->result;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 1
    public function getRequestId()
133
    {
134 1
        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