Passed
Push — develop ( c1d582...4a9276 )
by nguereza
05:36
created

HttpResponse   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 249
rs 10
c 1
b 0
f 0
wmc 22

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 3 1
A __construct() 0 19 2
A getStatusCode() 0 3 1
A isError() 0 3 2
A getCookies() 0 3 1
A getHeaderSize() 0 3 1
A json() 0 6 1
A getHeaders() 0 3 1
A processContent() 0 3 1
A getError() 0 3 1
A getContentLength() 0 3 1
A getHeader() 0 3 1
A xml() 0 3 1
A getContent() 0 3 1
A getCookie() 0 3 1
A processCookies() 0 13 4
A getContentType() 0 3 1
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant PHP
7
 * Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 * Copyright (c) 2011 - 2017 rehyved.com
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file HttpResponse.php
35
 *
36
 *  The Http Response class
37
 *
38
 *  @package    Platine\Framework\Http\Client
39
 *  @author Platine Developers team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   https://www.platine-php.com
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Framework\Http\Client;
50
51
use Platine\Stdlib\Helper\Json;
52
use Platine\Stdlib\Helper\Xml;
53
54
/**
55
 * @class HttpResponse
56
 * @package Platine\Framework\Http\Client
57
 */
58
class HttpResponse
59
{
60
    /**
61
     * The request URL
62
     * @var string
63
     */
64
    protected string $url;
65
66
    /**
67
     * The HTTP status code
68
     * @var int
69
     */
70
    protected int $statusCode;
71
72
    /**
73
     * The HTTP headers size
74
     * @var int
75
     */
76
    protected int $headerSize = 0;
77
78
    /**
79
     * The response headers
80
     * @var array<string, mixed>
81
     */
82
    protected array $headers = [];
83
84
    /**
85
     * The response cookies
86
     * @var array<string, HttpCookie>
87
     */
88
    protected array $cookies = [];
89
90
    /**
91
     * The content type
92
     * @var string
93
     */
94
    protected string $contentType = '';
95
96
    /**
97
     * The response content length
98
     * @var int
99
     */
100
    protected int $contentLength = 0;
101
102
    /**
103
     * The response raw content
104
     * @var string
105
     */
106
    protected string $content = '';
107
108
    /**
109
     * The response error
110
     * @var string
111
     */
112
    protected string $error = '';
113
114
    /**
115
     * Create new instance
116
     * @param array<string, mixed> $requestInfo
117
     * @param array<string, mixed> $headers
118
     * @param mixed $response
119
     * @param string $error
120
     */
121
    public function __construct(
122
        array $requestInfo,
123
        array $headers,
124
        $response,
125
        string $error
126
    ) {
127
        $this->url = $requestInfo['url'];
128
        $this->statusCode = (int) $requestInfo['http_code'];
129
        $this->headers = $headers;
130
        $this->cookies = $this->processCookies($headers);
131
132
        if ($response !== false) {
133
            $this->contentType = $requestInfo['content_type'];
134
            $this->headerSize = (int) $requestInfo['header_size'];
135
            $this->contentLength = (int) ($requestInfo['content_length'] ?? (strlen($response) - $this->headerSize));
136
            $this->content = $this->processContent($response, $this->headerSize);
137
        }
138
139
        $this->error = $error;
140
    }
141
142
    /**
143
     * Return the request URL
144
     * @return string
145
     */
146
    public function getUrl(): string
147
    {
148
        return $this->url;
149
    }
150
151
    /**
152
     * Return the HTTP status code
153
     * @return int
154
     */
155
    public function getStatusCode(): int
156
    {
157
        return $this->statusCode;
158
    }
159
    
160
    /**
161
     * Return the error message
162
     * @return string
163
     */
164
    public function getError(): string
165
    {
166
        return $this->error;
167
    }
168
169
    
170
    /**
171
     * Return the header size
172
     * @return int
173
     */
174
    public function getHeaderSize(): int
175
    {
176
        return $this->headerSize;
177
    }
178
179
    /**
180
     * Return the headers
181
     * @return array<string, mixed>
182
     */
183
    public function getHeaders(): array
184
    {
185
        return $this->headers;
186
    }
187
188
    /**
189
     * Return the HTTP header
190
     * @param string $name
191
     * @return mixed|null
192
     */
193
    public function getHeader(string $name)
194
    {
195
        return $this->headers[$name] ?? null;
196
    }
197
198
    /**
199
     * Return the cookies
200
     * @return array<string, HttpCookie>
201
     */
202
    public function getCookies(): array
203
    {
204
        return $this->cookies;
205
    }
206
207
    /**
208
     * Return the HTTP Cookie
209
     * @param string $name
210
     * @return HttpCookie|null
211
     */
212
    public function getCookie(string $name): ?HttpCookie
213
    {
214
        return $this->cookies[$name] ?? null;
215
    }
216
217
    /**
218
     * Return the response content type
219
     * @return string
220
     */
221
    public function getContentType(): string
222
    {
223
        return $this->contentType;
224
    }
225
226
    /**
227
     * Return the response content length
228
     * @return int
229
     */
230
    public function getContentLength(): int
231
    {
232
        return $this->contentLength;
233
    }
234
235
    /**
236
     * Return the raw response content
237
     * @return string
238
     */
239
    public function getContent(): string
240
    {
241
        return $this->content;
242
    }
243
244
    /**
245
     * Return the response as JSON
246
     * @param bool $assoc
247
     * @param int $depth
248
     * @param int $options
249
     * @return array<mixed>|object
250
     */
251
    public function json(
252
        bool $assoc = false,
253
        int $depth = 512,
254
        int $options = 0
255
    ) {
256
        return Json::decode($this->content, $assoc, $depth, $options);
257
    }
258
259
    /**
260
     * Return the response as XML
261
     * @return mixed
262
     */
263
    public function xml()
264
    {
265
        return Xml::decode($this->content);
266
    }
267
268
    /**
269
     * Whether the HTTP response indicated success or failure
270
     * @return bool
271
     */
272
    public function isError(): bool
273
    {
274
        return !empty($this->error) || HttpStatus::isError($this->statusCode);
275
    }
276
277
278
    /**
279
     * Process the cookies headers
280
     * @param array $headers
281
     * @return array<string, HttpCookie>
282
     */
283
    protected function processCookies(array $headers): array
284
    {
285
        $cookies = [];
286
        foreach ($headers as $header => $value) {
287
            if (stripos($header, 'Set-Cookie') !== false) {
288
                foreach ($value as $cookieString) {
289
                    $cookie = new HttpCookie($cookieString);
290
                    $cookies[$cookie->getName()] = $cookie;
291
                }
292
            }
293
        }
294
295
        return $cookies;
296
    }
297
298
    /**
299
     * Process the response content
300
     * @param string $response
301
     * @param int $headerSize
302
     * @return string
303
     */
304
    protected function processContent(string $response, int $headerSize): string
305
    {
306
        return substr($response, $headerSize);
307
    }
308
}
309