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.

ServerException::resolvePropertiesByReturn()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

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