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 (#34)
by Yong
05:43 queued 01:56
created

ServerException::getMessageString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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