Passed
Branch master (8a55cf)
by Igor
12:47 queued 09:06
created

CurlerResponse::url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ClickHouseDB\Transport;
4
5
class CurlerResponse
6
{
7
    /**
8
     * @var mixed
9
     */
10
    public $_headers;
11
12
    /**
13
     * @var mixed
14
     */
15
    public $_info;
16
17
    /**
18
     * @var mixed
19
     */
20
    public $_error;
21
22
    /**
23
     * @var int
24
     */
25
    public $_errorNo = 0;
26
27
    /**
28
     * @var float
29
     */
30
    public $_useTime;
31
32
    /**
33
     * @var string
34
     */
35
    public $_body;
36
37
38
    /**
39
     * Response constructor.
40
     */
41
    public function __construct() {}
42
43
44
    /**
45
     * @return int
46
     */
47 35
    public function error_no()
48
    {
49 35
        return $this->_errorNo;
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55 8
    public function error()
56
    {
57 8
        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()
72
    {
73 2
        return round($this->_info['total_time'], 3);
74
    }
75
76
    /**
77
     * @return string
78
     */
79 1
    public function starttransfer_time()
80
    {
81 1
        return round($this->_info['starttransfer_time'], 3);
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function connect_time()
88
    {
89
        return round($this->_info['connect_time'], 3);
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function pretransfer_time()
96
    {
97
        return round($this->_info['pretransfer_time'], 3);
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function content_type()
104
    {
105
        return $this->_info['content_type'];
106
    }
107
108
    /**
109
     * @return mixed
110
     */
111 35
    public function http_code()
112
    {
113 35
        return $this->_info['http_code'];
114
    }
115
116
    /**
117
     * @param string $name
118
     * @return null
119
     */
120
    public function headers($name)
121
    {
122
        if (isset($this->_headers[$name])) {
123
            return $this->_headers[$name];
124
        }
125
126
        return null;
127
    }
128
129
    /**
130
     * @return null
131
     */
132
    public function connection()
133
    {
134
        return $this->headers('Connection');
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->headers('Connection') targeting ClickHouseDB\Transport\CurlerResponse::headers() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140 35
    public function body()
141
    {
142 35
        return $this->_body;
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    public function as_string()
149
    {
150
        return $this->body();
151
    }
152
153
    /**
154
     *
155
     */
156
    public function dump_json()
157
    {
158
        print_r($this->json());
159
    }
160
161
    /**
162
     * @param bool $result
163
     * @return string
164
     */
165
    public function dump($result = false)
166
    {
167
        $msg = "\n--------------------------- Response -------------------------------------\nBODY:\n";
168
        $msg .= print_r($this->_body, true);
169
        $msg .= "\nHEAD:\n";
170
        $msg .= print_r($this->_headers, true);
171
        $msg .= "\nERROR:\n" . $this->error();
172
        $msg .= "\nINFO:\n";
173
        $msg .= json_encode($this->_info);
174
        $msg .= "\n----------------------------------------------------------------------\n";
175
176
        if ($result) {
177
            return $msg;
178
        }
179
180
        echo $msg;
181
    }
182
183
    /**
184
     * @param int $size
185
     * @param string $unit
186
     * @return string
187
     */
188 1
    private function humanFileSize($size, $unit = '')
189
    {
190 1
        if ((!$unit && $size >= 1 << 30) || $unit == 'GB') {
191
            return number_format($size / (1 << 30), 2) . ' GB';
192
        }
193 1
        if ((!$unit && $size >= 1 << 20) || $unit == 'MB') {
194
            return number_format($size / (1 << 20), 2) . ' MB';
195
        }
196 1
        if ((!$unit && $size >= 1 << 10) || $unit == 'KB') {
197
            return number_format($size / (1 << 10), 2) . ' KB';
198
        }
199
200 1
        return number_format($size) . ' bytes';
201
    }
202
203
    /**
204
     * @return string
205
     */
206 1
    public function upload_content_length()
207
    {
208 1
        return $this->humanFileSize($this->_info['upload_content_length']);
209
    }
210
211
    /**
212
     * @return string
213
     */
214 1
    public function speed_upload()
215
    {
216 1
        $SPEED_UPLOAD = $this->_info['speed_upload'];
217 1
        return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps';
218
    }
219
220
    /**
221
     * @return string
222
     */
223 1
    public function speed_download()
224
    {
225 1
        $SPEED_UPLOAD = $this->_info['speed_download'];
226 1
        return round(($SPEED_UPLOAD * 8) / (1000 * 1000), 2) . ' Mbps';
227
    }
228
229
    /**
230
     * @return string
231
     */
232 1
    public function size_upload()
233
    {
234 1
        return $this->humanFileSize($this->_info['size_upload']);
235
    }
236
237
    /**
238
     * @return string
239
     */
240
    public function request_size()
241
    {
242
        return $this->humanFileSize($this->_info['request_size']);
243
    }
244
245
    /**
246
     * @return string
247
     */
248
    public function header_size()
249
    {
250
        return $this->humanFileSize($this->_info['header_size']);
251
    }
252
253
    /**
254
     * @return string
255
     */
256 1
    public function size_download()
257
    {
258 1
        return $this->humanFileSize($this->_info['size_download']);
259
    }
260
261
    /**
262
     * @return mixed
263
     */
264 1
    public function info()
265
    {
266 1
        return $this->_info;
267
    }
268
    /**
269
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
270
     * @return bool|mixed
271
     */
272 35
    public function json($key = null)
273
    {
274 35
        $d = json_decode($this->body(), true);
275
276 35
        if (!$key) {
277 35
            return $d;
278
        }
279
280
        if (!isset($d[$key])) {
281
            return false;
282
        }
283
284
        return $d[$key];
285
    }
286
287
    /**
288
     * @return mixed
289
     */
290 35
    public function rawDataOrJson($format)
291
    {
292 35
        if (stripos($format, 'json') !== false)
293
        {
294 35
            return $this->json();
295
        }
296 1
        return $this->body();
297
298
    }
299
}
300