Response   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 5
Metric Value
wmc 10
c 9
b 0
f 5
lcom 2
cbo 0
dl 0
loc 120
ccs 29
cts 29
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A limits() 0 14 2
A getBody() 0 4 1
A getStatus() 0 4 1
A getHeaders() 0 4 1
A getHeader() 0 6 2
A result() 0 11 2
1
<?php
2
3
namespace Artstorm\MonkeyLearn\HttpClient;
4
5
class Response
6
{
7
    /**
8
     * HTTP Status code
9
     *
10
     * @int
11
     */
12
    protected $status;
13
14
    /**
15
     * Headers.
16
     *
17
     * @var array
18
     */
19
    protected $headers;
20
21
    /**
22
     * Contents.
23
     *
24
     * @var string
25
     */
26
    protected $body;
27
28
    /**
29
     * Assign dependencies.
30
     *
31
     * @param int    $status
32
     * @param array  $headers
33
     * @param string $body
34
     */
35 26
    public function __construct($status = 200, array $headers = [], $body = null)
36
    {
37 26
        $this->status = $status;
38 26
        $this->headers = $headers;
39 26
        $this->body = $body;
40 26
    }
41
42
    /**
43
     * Extracts the relevant content from the response.
44
     *
45
     * @return array|mixed
46
     */
47 4
    public function result()
48
    {
49 4
        $body = $this->body;
50 4
        $content = json_decode($body, true);
51
52 4
        if (JSON_ERROR_NONE !== json_last_error()) {
53 2
            return $body;
54
        }
55
56 2
        return $content['result'];
57
    }
58
59
    /**
60
     * Get query limit headers.
61
     *
62
     * @param  array  $limits
63
     *
64
     * @return array
65
     */
66 2
    public function limits(array $limits = [])
67
    {
68
        $headers = [
69 2
            'X-Query-Limit-Limit',
70 2
            'X-Query-Limit-Remaining',
71
            'X-Query-Limit-Request-Queries'
72 2
        ];
73
74 2
        foreach ($headers as $header) {
75 2
            $limits[$header] = $this->getHeader($header);
76 2
        }
77
78 2
        return $limits;
79
    }
80
81
    /**
82
     * Get body.
83
     *
84
     * @return string
85
     */
86 4
    public function getBody()
87
    {
88 4
        return $this->body;
89
    }
90
91
    /**
92
     * Get HTTP status code.
93
     *
94
     * @return string
95
     */
96 4
    public function getStatus()
97
    {
98 4
        return $this->status;
99
    }
100
101
    /**
102
     * Get response headers.
103
     *
104
     * @return array
105
     */
106 2
    public function getHeaders()
107
    {
108 2
        return $this->headers;
109
    }
110
111
    /**
112
     * Get a response header by key.
113
     *
114
     * @param  string $key
115
     *
116
     * @return string
117
     */
118 6
    public function getHeader($key)
119
    {
120 6
        if (array_key_exists($key, $this->headers)) {
121 4
            return $this->headers[$key];
122
        }
123 4
    }
124
}
125