Passed
Push — master ( e7e3c4...d0e289 )
by Rogier
01:26
created

Response   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 24
dl 0
loc 66
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 3 1
A getBody() 0 3 1
A hasBody() 0 3 1
A getRawHeaders() 0 16 3
A getError() 0 3 1
A getHttpResponseCode() 0 7 2
A __construct() 0 6 1
1
<?php
2
3
namespace Rogierw\RwAcme\Http;
4
5
use Rogierw\RwAcme\Support\Str;
6
7
class Response
8
{
9
    private $rawHeaders;
10
    private $headers;
11
    private $body;
12
    private $error;
13
14
    public function __construct($rawHeaders, $headers, $body, $error)
15
    {
16
        $this->rawHeaders = $rawHeaders;
17
        $this->headers = $headers;
18
        $this->body = $body;
19
        $this->error = $error;
20
    }
21
22
    /** @return mixed */
23
    public function getRawHeaders()
24
    {
25
        $headers = explode("\n", $this->rawHeaders);
26
        $headersArr = [];
27
28
        foreach ($headers as $header) {
29
            if (!Str::contains($header, ':')) {
30
                continue;
31
            }
32
33
            [$name, $value] = explode(':', $header, 2);
34
35
            $headersArr[$name] = $value;
36
        }
37
38
        return $headersArr;
39
    }
40
41
    /** @return mixed */
42
    public function getHeaders()
43
    {
44
        return $this->headers;
45
    }
46
47
    /** @return mixed */
48
    public function getBody()
49
    {
50
        return $this->body;
51
    }
52
53
    /** @return bool */
54
    public function hasBody()
55
    {
56
        return $this->body != false;
57
    }
58
59
    /** @return mixed */
60
    public function getError()
61
    {
62
        return $this->error;
63
    }
64
65
    /** @return null|int */
66
    public function getHttpResponseCode()
67
    {
68
        if (!isset($this->headers['http_code'])) {
69
            return;
70
        }
71
72
        return (int) $this->headers['http_code'];
73
    }
74
}
75