Passed
Pull Request — master (#408)
by
unknown
22:38
created

Response::__construct()   C

Complexity

Conditions 13
Paths 40

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 38.0748

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 33
c 1
b 0
f 0
nc 40
nop 5
dl 0
loc 48
ccs 16
cts 34
cp 0.4706
crap 38.0748
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Qiniu\Http;
4
5
/**
6
 * HTTP response Object
7
 */
8
final class Response
9
{
10
    public $statusCode;
11
    /**
12
     * deprecated because of field names case-sensitive.
13
     * use $normalizedHeaders instead which field names are case-insensitive.
14
     * but be careful not to use $normalizedHeaders with `array_*` functions,
15
     * such as `array_key_exists`, `array_keys`, `array_values`.
16
     *
17
     * use `isset` instead of `array_key_exists`,
18
     * and should never use `array_key_exists` at http header.
19
     *
20
     * use `foreach` instead of `array_keys`, `array_values`.
21
     *
22
     * @deprecated
23
     */
24
    public $headers;
25
    public $normalizedHeaders;
26
    public $body;
27
    public $error;
28
    private $jsonData;
29
    public $duration;
30
31
    /** @var array Mapping of status codes to reason phrases */
32
    private static $statusTexts = array(
33
        100 => 'Continue',
34
        101 => 'Switching Protocols',
35
        102 => 'Processing',
36
        200 => 'OK',
37
        201 => 'Created',
38
        202 => 'Accepted',
39
        203 => 'Non-Authoritative Information',
40
        204 => 'No Content',
41
        205 => 'Reset Content',
42
        206 => 'Partial Content',
43
        207 => 'Multi-Status',
44
        208 => 'Already Reported',
45
        226 => 'IM Used',
46
        300 => 'Multiple Choices',
47
        301 => 'Moved Permanently',
48
        302 => 'Found',
49
        303 => 'See Other',
50
        304 => 'Not Modified',
51
        305 => 'Use Proxy',
52
        307 => 'Temporary Redirect',
53
        308 => 'Permanent Redirect',
54
        400 => 'Bad Request',
55
        401 => 'Unauthorized',
56
        402 => 'Payment Required',
57
        403 => 'Forbidden',
58
        404 => 'Not Found',
59
        405 => 'Method Not Allowed',
60
        406 => 'Not Acceptable',
61
        407 => 'Proxy Authentication Required',
62
        408 => 'Request Timeout',
63
        409 => 'Conflict',
64
        410 => 'Gone',
65
        411 => 'Length Required',
66
        412 => 'Precondition Failed',
67
        413 => 'Request Entity Too Large',
68
        414 => 'Request-URI Too Long',
69
        415 => 'Unsupported Media Type',
70
        416 => 'Requested Range Not Satisfiable',
71
        417 => 'Expectation Failed',
72
        422 => 'Unprocessable Entity',
73
        423 => 'Locked',
74
        424 => 'Failed Dependency',
75
        425 => 'Reserved for WebDAV advanced collections expired proposal',
76
        426 => 'Upgrade required',
77
        428 => 'Precondition Required',
78
        429 => 'Too Many Requests',
79
        431 => 'Request Header Fields Too Large',
80
        500 => 'Internal Server Error',
81
        501 => 'Not Implemented',
82
        502 => 'Bad Gateway',
83
        503 => 'Service Unavailable',
84
        504 => 'Gateway Timeout',
85
        505 => 'HTTP Version Not Supported',
86 102
        506 => 'Variant Also Negotiates (Experimental)',
87
        507 => 'Insufficient Storage',
88 102
        508 => 'Loop Detected',
89 102
        510 => 'Not Extended',
90 102
        511 => 'Network Authentication Required',
91 102
    );
92 102
93 102
    /**
94 102
     * @param int $code 状态码
95 3
     * @param double $duration 请求时长
96
     * @param array $headers 响应头部
97
     * @param string $body 响应内容
98 102
     * @param string $error 错误描述
99
     */
100
    public function __construct($code, $duration, array $headers = array(), $body = null, $error = null)
101
    {
102
        $this->statusCode = $code;
103
        $this->duration = $duration;
104 102
        $this->headers = array();
0 ignored issues
show
Deprecated Code introduced by
The property Qiniu\Http\Response::$headers has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

104
        /** @scrutinizer ignore-deprecated */ $this->headers = array();
Loading history...
105
        $this->body = $body;
106 90
        $this->error = $error;
107 90
        $this->jsonData = null;
108 24
109 24
        if ($error !== null) {
110 24
            return;
111 24
        }
112 24
113 90
        foreach ($headers as $k => $vs) {
114 90
            if (is_array($vs)) {
115
                $this->headers[$k] = $vs[count($vs) - 1];
0 ignored issues
show
Deprecated Code introduced by
The property Qiniu\Http\Response::$headers has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

115
                /** @scrutinizer ignore-deprecated */ $this->headers[$k] = $vs[count($vs) - 1];
Loading history...
116
            } else {
117
                $this->headers[$k] = $vs;
0 ignored issues
show
Deprecated Code introduced by
The property Qiniu\Http\Response::$headers has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

117
                /** @scrutinizer ignore-deprecated */ $this->headers[$k] = $vs;
Loading history...
118
            }
119
        }
120 102
        $this->normalizedHeaders = new Header($headers);
121
122
        if ($body === null) {
123 102
            if ($code >= 400) {
124
                $this->error = self::$statusTexts[$code];
125
            }
126 75
            return;
127
        }
128 75
        if (self::isJson($this->normalizedHeaders)) {
129
            try {
130
                $jsonData = self::bodyJson($body);
131 90
                if ($code >= 400) {
132
                    $this->error = $body;
133 90
                    if ($jsonData['error'] !== null) {
134
                        $this->error = $jsonData['error'];
135
                    }
136
                }
137
                $this->jsonData = $jsonData;
138
            } catch (\InvalidArgumentException $e) {
139
                $this->error = $body;
140
                if ($code >= 200 && $code < 300) {
141
                    $this->error = $e->getMessage();
142
                }
143
            }
144
        } elseif ($code >= 400) {
145
            $this->error = $body;
146
        }
147
        return;
148 6
    }
149
150 6
    public function json()
151
    {
152
        return $this->jsonData;
153 6
    }
154
155 6
    public function headers($normalized = false)
156
    {
157
        if ($normalized) {
158 81
            return $this->normalizedHeaders;
159
        }
160 81
        return $this->headers;
0 ignored issues
show
Deprecated Code introduced by
The property Qiniu\Http\Response::$headers has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

160
        return /** @scrutinizer ignore-deprecated */ $this->headers;
Loading history...
161
    }
162
163 6
    public function body()
164
    {
165 6
        return $this->body;
166 6
    }
167 3
168
    private static function bodyJson($body)
169 6
    {
170
        return \Qiniu\json_decode((string) $body, true, 512);
171 102
    }
172
173 102
    public function xVia()
174 102
    {
175
        $via = $this->normalizedHeaders['X-Via'];
176
        if ($via === null) {
177
            $via = $this->normalizedHeaders['X-Px'];
178
        }
179
        if ($via === null) {
180
            $via = $this->normalizedHeaders['Fw-Via'];
181
        }
182
        return $via;
183
    }
184
185
    public function xLog()
186
    {
187
        return $this->normalizedHeaders['X-Log'];
188
    }
189
190
    public function xReqId()
191
    {
192
        return $this->normalizedHeaders['X-Reqid'];
193
    }
194
195
    public function ok()
196
    {
197
        return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error === null;
198
    }
199
200
    public function needRetry()
201
    {
202
        $code = $this->statusCode;
203
        if ($code < 0 || ($code / 100 === 5 and $code !== 579) || $code === 996) {
204
            return true;
205
        }
206
207
        if (in_array($code, array(
208
            996
209
        ))) {
210
            return true;
211
        }
212
213
        if (in_array($code, array(
214
            579, 612, 631
215
        ))) {
216
            return false;
217
        }
218
219
        $codeFirstDigit = floor($code / 100);
220
        if (in_array($codeFirstDigit, array(
221
            5, 6
222
        ))) {
223
            return true;
224
        }
225
226
        if (in_array($codeFirstDigit, array(
227
            4
228
        ))) {
229
            return false;
230
        }
231
232
        return true;
233
    }
234
235
    private static function isJson($headers)
236
    {
237
        return isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/json') === 0;
238
    }
239
}
240