1 | <?php |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
2 | |||
3 | namespace ClickHouseDB\Transport; |
||
4 | |||
5 | class CurlerResponse |
||
6 | { |
||
7 | /** |
||
0 ignored issues
–
show
|
|||
8 | * @var mixed |
||
9 | */ |
||
10 | public $_headers; |
||
0 ignored issues
–
show
|
|||
11 | |||
12 | /** |
||
0 ignored issues
–
show
|
|||
13 | * @var mixed |
||
14 | */ |
||
15 | public $_info; |
||
0 ignored issues
–
show
|
|||
16 | |||
17 | /** |
||
0 ignored issues
–
show
|
|||
18 | * @var mixed |
||
19 | */ |
||
20 | public $_error; |
||
0 ignored issues
–
show
|
|||
21 | |||
22 | /** |
||
0 ignored issues
–
show
|
|||
23 | * @var int |
||
24 | */ |
||
25 | public $_errorNo = 0; |
||
0 ignored issues
–
show
|
|||
26 | |||
27 | /** |
||
0 ignored issues
–
show
|
|||
28 | * @var float |
||
29 | */ |
||
30 | public $_useTime; |
||
0 ignored issues
–
show
|
|||
31 | |||
32 | /** |
||
0 ignored issues
–
show
|
|||
33 | * @var string |
||
34 | */ |
||
35 | public $_body; |
||
0 ignored issues
–
show
|
|||
36 | |||
37 | |||
38 | /** |
||
39 | * Response constructor. |
||
0 ignored issues
–
show
|
|||
40 | */ |
||
41 | 49 | public function __construct() {} |
|
0 ignored issues
–
show
|
|||
42 | |||
43 | |||
44 | /** |
||
45 | * @return int |
||
46 | */ |
||
47 | 39 | public function error_no() |
|
0 ignored issues
–
show
|
|||
48 | { |
||
49 | 39 | return $this->_errorNo; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return mixed |
||
54 | */ |
||
55 | 6 | public function error() |
|
56 | { |
||
57 | 6 | return $this->_error; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return mixed |
||
62 | */ |
||
63 | public function url() |
||
64 | { |
||
65 | return $this->_info['url']; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return mixed |
||
70 | */ |
||
71 | 2 | public function total_time() |
|
0 ignored issues
–
show
|
|||
72 | { |
||
73 | 2 | return round($this->_info['total_time'], 3); |
|
0 ignored issues
–
show
|
|||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | 1 | public function starttransfer_time() |
|
0 ignored issues
–
show
|
|||
80 | { |
||
81 | 1 | return round($this->_info['starttransfer_time'], 3); |
|
0 ignored issues
–
show
|
|||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | public function connect_time() |
||
0 ignored issues
–
show
|
|||
88 | { |
||
89 | return round($this->_info['connect_time'], 3); |
||
0 ignored issues
–
show
|
|||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | public function pretransfer_time() |
||
0 ignored issues
–
show
|
|||
96 | { |
||
97 | return round($this->_info['pretransfer_time'], 3); |
||
0 ignored issues
–
show
|
|||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function content_type() |
||
0 ignored issues
–
show
|
|||
104 | { |
||
105 | return $this->_info['content_type']; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return mixed |
||
110 | */ |
||
111 | 49 | public function http_code() |
|
0 ignored issues
–
show
|
|||
112 | { |
||
113 | 49 | return $this->_info['http_code']; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param string $name |
||
0 ignored issues
–
show
|
|||
118 | * @return null|string |
||
0 ignored issues
–
show
|
|||
119 | */ |
||
120 | public function headers($name) |
||
0 ignored issues
–
show
|
|||
121 | { |
||
122 | if (isset($this->_headers[$name])) { |
||
123 | return $this->_headers[$name]; |
||
124 | } |
||
125 | |||
126 | return null; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return null|string |
||
0 ignored issues
–
show
|
|||
131 | */ |
||
132 | public function connection() |
||
0 ignored issues
–
show
|
|||
133 | { |
||
134 | return $this->headers('Connection'); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return mixed |
||
139 | */ |
||
140 | 48 | public function body() |
|
141 | { |
||
142 | 48 | return $this->_body; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function as_string() |
||
0 ignored issues
–
show
|
|||
149 | { |
||
150 | return $this->body(); |
||
151 | } |
||
152 | |||
153 | /** |
||
0 ignored issues
–
show
|
|||
154 | * |
||
155 | */ |
||
156 | public function dump_json() |
||
0 ignored issues
–
show
|
|||
157 | { |
||
158 | print_r($this->json()); |
||
0 ignored issues
–
show
|
|||
159 | } |
||
160 | |||
161 | 1 | public function getDetails(): array |
|
0 ignored issues
–
show
|
|||
162 | { |
||
163 | return [ |
||
164 | 1 | 'body' => $this->_body, |
|
165 | 1 | 'headers' => $this->_headers, |
|
166 | 1 | 'error' => $this->error(), |
|
167 | 1 | 'info' => $this->_info, |
|
168 | ]; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param bool $result |
||
0 ignored issues
–
show
|
|||
173 | * @return string |
||
174 | */ |
||
175 | public function dump($result = false) |
||
0 ignored issues
–
show
|
|||
176 | { |
||
177 | $msg = "\n--------------------------- Response -------------------------------------\nBODY:\n"; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
178 | $msg .= print_r($this->_body, true); |
||
0 ignored issues
–
show
|
|||
179 | $msg .= "\nHEAD:\n"; |
||
180 | $msg .= print_r($this->_headers, true); |
||
0 ignored issues
–
show
|
|||
181 | $msg .= "\nERROR:\n" . $this->error(); |
||
182 | $msg .= "\nINFO:\n"; |
||
183 | $msg .= json_encode($this->_info); |
||
0 ignored issues
–
show
|
|||
184 | $msg .= "\n----------------------------------------------------------------------\n"; |
||
185 | |||
186 | if ($result) { |
||
187 | return $msg; |
||
188 | } |
||
189 | |||
190 | echo $msg; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param int $size |
||
0 ignored issues
–
show
|
|||
195 | * @param string $unit |
||
196 | * @return string |
||
197 | */ |
||
198 | 1 | private function humanFileSize($size, $unit = '') |
|
0 ignored issues
–
show
|
|||
199 | { |
||
200 | 1 | if ((!$unit && $size >= 1 << 30) || $unit == 'GB') { |
|
0 ignored issues
–
show
|
|||
201 | return number_format($size / (1 << 30), 2) . ' GB'; |
||
0 ignored issues
–
show
|
|||
202 | } |
||
203 | 1 | if ((!$unit && $size >= 1 << 20) || $unit == 'MB') { |
|
0 ignored issues
–
show
|
|||
204 | return number_format($size / (1 << 20), 2) . ' MB'; |
||
0 ignored issues
–
show
|
|||
205 | } |
||
206 | 1 | if ((!$unit && $size >= 1 << 10) || $unit == 'KB') { |
|
0 ignored issues
–
show
|
|||
207 | return number_format($size / (1 << 10), 2) . ' KB'; |
||
0 ignored issues
–
show
|
|||
208 | } |
||
209 | |||
210 | 1 | return number_format($size) . ' bytes'; |
|
0 ignored issues
–
show
|
|||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @return string |
||
215 | */ |
||
216 | 1 | public function upload_content_length() |
|
0 ignored issues
–
show
|
|||
217 | { |
||
218 | 1 | return $this->humanFileSize($this->_info['upload_content_length']); |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | 1 | public function speed_upload() |
|
0 ignored issues
–
show
|
|||
225 | { |
||
226 | 1 | $SPEED_UPLOAD = $this->_info['speed_upload']; |
|
0 ignored issues
–
show
|
|||
227 | 1 | return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps'; |
|
0 ignored issues
–
show
|
|||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | 1 | public function speed_download() |
|
0 ignored issues
–
show
|
|||
234 | { |
||
235 | 1 | $SPEED_UPLOAD = $this->_info['speed_download']; |
|
0 ignored issues
–
show
|
|||
236 | 1 | return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps'; |
|
0 ignored issues
–
show
|
|||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @return string |
||
241 | */ |
||
242 | 1 | public function size_upload() |
|
0 ignored issues
–
show
|
|||
243 | { |
||
244 | 1 | return $this->humanFileSize($this->_info['size_upload']); |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function request_size() |
||
0 ignored issues
–
show
|
|||
251 | { |
||
252 | return $this->humanFileSize($this->_info['request_size']); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | public function header_size() |
||
0 ignored issues
–
show
|
|||
259 | { |
||
260 | return $this->humanFileSize($this->_info['header_size']); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @return string |
||
265 | */ |
||
266 | 1 | public function size_download() |
|
0 ignored issues
–
show
|
|||
267 | { |
||
268 | 1 | return $this->humanFileSize($this->_info['size_download']); |
|
269 | } |
||
270 | |||
271 | /** |
||
272 | * @return mixed |
||
273 | */ |
||
274 | 1 | public function info() |
|
275 | { |
||
276 | 1 | return $this->_info; |
|
277 | } |
||
0 ignored issues
–
show
|
|||
278 | /** |
||
279 | * @param string|null $key |
||
0 ignored issues
–
show
|
|||
280 | * @return bool|mixed |
||
281 | */ |
||
282 | 30 | public function json($key = null) |
|
0 ignored issues
–
show
|
|||
283 | { |
||
284 | 30 | $d = json_decode($this->body(), true); |
|
0 ignored issues
–
show
|
|||
285 | |||
286 | 30 | if (!$key) { |
|
0 ignored issues
–
show
|
|||
287 | 30 | return $d; |
|
288 | } |
||
289 | |||
290 | if (!isset($d[$key])) { |
||
0 ignored issues
–
show
|
|||
291 | return false; |
||
292 | } |
||
293 | |||
294 | return $d[$key]; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * @return mixed |
||
299 | */ |
||
300 | 31 | public function rawDataOrJson($format) |
|
0 ignored issues
–
show
|
|||
301 | { |
||
302 | // JSONCompact // JSONEachRow |
||
303 | |||
304 | 31 | if (stripos($format, 'json') !== false) |
|
0 ignored issues
–
show
|
|||
305 | { |
||
306 | 30 | if (stripos($format,'JSONEachRow')===false) |
|
0 ignored issues
–
show
|
|||
307 | 30 | return $this->json(); |
|
0 ignored issues
–
show
|
|||
308 | } |
||
309 | 2 | return $this->body(); |
|
0 ignored issues
–
show
|
|||
310 | |||
311 | } |
||
0 ignored issues
–
show
|
|||
312 | } |
||
313 |