Completed
Push — master ( 550cc9...3b65fe )
by Bai
04:10
created

Response::__construct()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12.8905

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 11
eloc 28
c 4
b 0
f 0
nc 14
nop 5
dl 0
loc 39
ccs 24
cts 32
cp 0.75
crap 12.8905
rs 5.2653

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
    public $headers;
12
    public $body;
13
    public $error;
14
    private $jsonData;
15
    public $duration;
16
17
    /** @var array Mapping of status codes to reason phrases */
18
    private static $statusTexts = array(
19
        100 => 'Continue',
20
        101 => 'Switching Protocols',
21
        102 => 'Processing',
22
        200 => 'OK',
23
        201 => 'Created',
24
        202 => 'Accepted',
25
        203 => 'Non-Authoritative Information',
26
        204 => 'No Content',
27
        205 => 'Reset Content',
28
        206 => 'Partial Content',
29
        207 => 'Multi-Status',
30
        208 => 'Already Reported',
31
        226 => 'IM Used',
32
        300 => 'Multiple Choices',
33
        301 => 'Moved Permanently',
34
        302 => 'Found',
35
        303 => 'See Other',
36
        304 => 'Not Modified',
37
        305 => 'Use Proxy',
38
        307 => 'Temporary Redirect',
39
        308 => 'Permanent Redirect',
40
        400 => 'Bad Request',
41
        401 => 'Unauthorized',
42
        402 => 'Payment Required',
43
        403 => 'Forbidden',
44
        404 => 'Not Found',
45
        405 => 'Method Not Allowed',
46
        406 => 'Not Acceptable',
47
        407 => 'Proxy Authentication Required',
48
        408 => 'Request Timeout',
49
        409 => 'Conflict',
50
        410 => 'Gone',
51
        411 => 'Length Required',
52
        412 => 'Precondition Failed',
53
        413 => 'Request Entity Too Large',
54
        414 => 'Request-URI Too Long',
55
        415 => 'Unsupported Media Type',
56
        416 => 'Requested Range Not Satisfiable',
57
        417 => 'Expectation Failed',
58
        422 => 'Unprocessable Entity',
59
        423 => 'Locked',
60
        424 => 'Failed Dependency',
61
        425 => 'Reserved for WebDAV advanced collections expired proposal',
62
        426 => 'Upgrade required',
63
        428 => 'Precondition Required',
64
        429 => 'Too Many Requests',
65
        431 => 'Request Header Fields Too Large',
66
        500 => 'Internal Server Error',
67
        501 => 'Not Implemented',
68
        502 => 'Bad Gateway',
69
        503 => 'Service Unavailable',
70
        504 => 'Gateway Timeout',
71
        505 => 'HTTP Version Not Supported',
72
        506 => 'Variant Also Negotiates (Experimental)',
73
        507 => 'Insufficient Storage',
74
        508 => 'Loop Detected',
75
        510 => 'Not Extended',
76
        511 => 'Network Authentication Required',
77
    );
78
79
    /**
80
     * @param int $code 状态码
81
     * @param double $duration 请求时长
82
     * @param array $headers 响应头部
83
     * @param string $body 响应内容
84
     * @param string $error 错误描述
85
     */
86 90
    public function __construct($code, $duration, array $headers = array(), $body = null, $error = null)
87
    {
88 90
        $this->statusCode = $code;
89 90
        $this->duration = $duration;
90 90
        $this->headers = $headers;
91 90
        $this->body = $body;
92 90
        $this->error = $error;
93 90
        $this->jsonData = null;
94 90
        if ($error !== null) {
95 3
            return;
96
        }
97
98 90
        if ($body === null) {
99
            if ($code >= 400) {
100
                $this->error = self::$statusTexts[$code];
101
            }
102
            return;
103
        }
104 90
        if (self::isJson($headers)) {
105
            try {
106 81
                $jsonData = self::bodyJson($body);
107 81
                if ($code >=400) {
108 18
                    $this->error = $body;
109 18
                    if ($jsonData['error'] !== null) {
110 18
                        $this->error = $jsonData['error'];
111 18
                    }
112 18
                }
113 81
                $this->jsonData = $jsonData;
114 81
            } catch (\InvalidArgumentException $e) {
115
                $this->error = $body;
116
                if ($code >= 200 && $code < 300) {
117
                    $this->error = $e->getMessage();
118
                }
119
            }
120 90
        } elseif ($code >=400) {
121 3
            $this->error = $body;
122 3
        }
123 90
        return;
124
    }
125
126 66
    public function json()
127
    {
128 66
        return $this->jsonData;
129
    }
130
131 81
    private static function bodyJson($body)
132
    {
133 81
        return \Qiniu\json_decode((string) $body, true, 512);
134
    }
135
136
    public function xVia()
137
    {
138
        $via = $this->headers['X-Via'];
139
        if ($via === null) {
140
            $via = $this->headers['X-Px'];
141
        }
142
        if ($via === null) {
143
            $via = $this->headers['Fw-Via'];
144
        }
145
        return $via;
146
    }
147
148 6
    public function xLog()
149
    {
150 6
        return $this->headers['X-Log'];
151
    }
152
153 6
    public function xReqId()
154
    {
155 6
        return $this->headers['X-Reqid'];
156
    }
157
158 72
    public function ok()
159
    {
160 72
        return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error === null;
161
    }
162
163 6
    public function needRetry()
164
    {
165 6
        $code = $this->statusCode;
166 6
        if ($code< 0 || ($code / 100 === 5 and $code !== 579) || $code === 996) {
167 3
            return true;
168
        }
169 6
    }
170
171 90
    private static function isJson($headers)
172
    {
173 90
        return array_key_exists('Content-Type', $headers) &&
174 90
        strpos($headers['Content-Type'], 'application/json') === 0;
175
    }
176
}
177