HttpResponse   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 65
ccs 11
cts 18
cp 0.6111
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A getHttpStatus() 0 4 1
A getBody() 0 4 1
A json() 0 9 2
A getHeaders() 0 4 1
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