Completed
Pull Request — master (#229)
by r
17:42 queued 15:34
created

Response   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 66.1%

Importance

Changes 0
Metric Value
dl 0
loc 169
ccs 39
cts 59
cp 0.661
rs 10
c 0
b 0
f 0
wmc 28
lcom 2
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A xLog() 0 4 1
A xReqId() 0 4 1
B needRetry() 0 7 5
A json() 0 4 1
A bodyJson() 0 4 1
A xVia() 0 11 3
A ok() 0 4 3
A isJson() 0 5 2
C __construct() 0 39 11
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 102
    public function __construct($code, $duration, array $headers = array(), $body = null, $error = null)
87
    {
88 102
        $this->statusCode = $code;
89 102
        $this->duration = $duration;
90 102
        $this->headers = $headers;
91 102
        $this->body = $body;
92 102
        $this->error = $error;
93 102
        $this->jsonData = null;
94 102
        if ($error !== null) {
95
            return;
96
        }
97
98 102
        if ($body === null) {
99
            if ($code >= 400) {
100
                $this->error = self::$statusTexts[$code];
101
            }
102
            return;
103
        }
104 102
        if (self::isJson($headers)) {
105
            try {
106 93
                $jsonData = self::bodyJson($body);
107 93
                if ($code >=400) {
108 27
                    $this->error = $body;
109 27
                    if ($jsonData['error'] !== null) {
110 27
                        $this->error = $jsonData['error'];
111 18
                    }
112 18
                }
113 93
                $this->jsonData = $jsonData;
114 62
            } catch (\InvalidArgumentException $e) {
115
                $this->error = $body;
116
                if ($code >= 200 && $code < 300) {
117 31
                    $this->error = $e->getMessage();
118
                }
119
            }
120 72
        } elseif ($code >=400) {
121
            $this->error = $body;
122
        }
123 102
        return;
124
    }
125
126 75
    public function json()
127
    {
128 75
        return $this->jsonData;
129
    }
130
131 93
    private static function bodyJson($body)
132
    {
133 93
        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 81
    public function ok()
159
    {
160 81
        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
            return true;
168
        }
169 6
    }
170
171 102
    private static function isJson($headers)
172
    {
173 102
        return array_key_exists('Content-Type', $headers) &&
174 102
        strpos($headers['Content-Type'], 'application/json') === 0;
175
    }
176
}
177