Completed
Pull Request — master (#42)
by Chad
02:53
created

Response   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 75
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A getHttpCode() 0 4 1
A getResponse() 0 4 1
A getResponseHeaders() 0 4 1
1
<?php
2
3
namespace TraderInteractive\Api;
4
5
use DominionEnterprises\Util;
6
7
/**
8
 * Represents a response to an API request
9
 */
10
final class Response
11
{
12
    /**
13
     * The http status of the response.
14
     *
15
     * @var int
16
     */
17
    private $httpCode;
18
19
    /**
20
     * The response from the API
21
     *
22
     * @var array
23
     */
24
    private $body;
25
26
    /**
27
     * A array of headers received with the response.
28
     *
29
     * @var array array where each header key has an array of values
30
     */
31
    private $headers;
32
33
    /**
34
     * Create a new instance of Response
35
     *
36
     * @param int $httpCode
37
     * @param array $headers
38
     * @param array $body
39
     *
40
     * @throws \InvalidArgumentException Throw if $httpCode is not an integer between 100 and 600
41
     */
42
    public function __construct($httpCode, array $headers, array $body = [])
43
    {
44
        Util::throwIfNotType(['int' => $httpCode, 'array' => $headers]);
45
46
        if ($httpCode < 100 || $httpCode > 600) {
47
            throw new \InvalidArgumentException('$httpCode must be an integer >= 100 and <= 600');
48
        }
49
50
        $this->httpCode = $httpCode;
51
        $this->headers = $headers;
52
        $this->body = $body;
53
    }
54
55
    /**
56
     * Returns the HTTP status code of the response
57
     *
58
     * @return int
59
     */
60
    public function getHttpCode()
61
    {
62
        return $this->httpCode;
63
    }
64
65
    /**
66
     * Returns an array representing the response from the API
67
     *
68
     * @return array
69
     */
70
    public function getResponse()
71
    {
72
        return $this->body;
73
    }
74
75
    /**
76
     * Returns the parsed response headers from the API
77
     *
78
     * @return array array where each header key has an array of values
79
     */
80
    public function getResponseHeaders()
81
    {
82
        return $this->headers;
83
    }
84
}
85