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 Failed
Pull Request — master (#79)
by Yong
05:23
created

ServerException::distinguishSignatureErrors()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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