HttpResponse::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Katapoka\Ahgora;
4
5
use Katapoka\Ahgora\Contracts\IHttpResponse;
6
7
/**
8
 * Class for the Http responses
9
 */
10
class HttpResponse implements IHttpResponse
11
{
12
    /** @var int The http response status code */
13
    private $httpStatus;
14
15
    /** @var string The response body */
16
    private $body;
17
18
    /** @var array The response headers */
19
    private $headers = [];
20
21 3
    public function __construct(array $data = [])
22
    {
23 3
        foreach ($data as $key => $value) {
24 3
            if (property_exists($this, $key)) {
25 3
                $this->$key = $value;
26 3
            }
27 3
        }
28 3
    }
29
30
    /**
31
     * Get the HttpResponse status.
32
     *
33
     * @return mixed
34
     */
35 3
    public function getHttpStatus()
36
    {
37 3
        return $this->httpStatus;
38
    }
39
40
    /**
41
     * Get the request body.
42
     *
43
     * @return string
44
     */
45 3
    public function getBody()
46
    {
47 3
        return $this->body;
48
    }
49
50
    /**
51
     * Get the request body as json, if is json compatible.
52
     *
53
     * @return string
54
     */
55
    public function json()
56
    {
57
        $json = json_decode($this->body);
58
        if (json_last_error() !== JSON_ERROR_NONE) {
59
            throw new \InvalidArgumentException('Failed to decode json:' . json_last_error_msg());
60
        }
61
62
        return $json;
63
    }
64
65
    /**
66
     * The the response headers.
67
     *
68
     * @return array
69
     */
70
    public function getHeaders()
71
    {
72
        return $this->headers;
73
    }
74
}
75