Completed
Pull Request — develop (#22)
by Johan
02:58 queued 01:04
created

Response::result()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 2.0932
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 22
    public function __construct($status = 200, array $headers = [], $body = null)
36
    {
37 22
        $this->status = $status;
38 22
        $this->headers = $headers;
39 22
        $this->body = $body;
40 22
    }
41
42
    /**
43
     * Extracts the relevant content from the response.
44
     *
45
     * @param  Response $response
0 ignored issues
show
Bug introduced by
There is no parameter named $response. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     *
47
     * @return array|mixed
48
     */
49 2
    public function result()
50
    {
51 2
        $body = $this->body;
52 2
        $content = json_decode($body, true);
53
54 2
        if (JSON_ERROR_NONE !== json_last_error()) {
55 2
            return $body;
56
        }
57
58
        // Add remainin query limits to the response
59
        $content['limits'] = $this->getQueryLimitHeaders();
60
61
        return $content;
62
    }
63
64
    /**
65
     * Get query limit headers.
66
     *
67
     * @param  array  $limits
68
     *
69
     * @return array
70
     */
71
    protected function getQueryLimitHeaders(array $limits = [])
72
    {
73
        $headers = [
74
            'X-Query-Limit-Limit',
75
            'X-Query-Limit-Remaining',
76
            'X-Query-Limit-Request-Queries'
77
        ];
78
79
        foreach ($headers as $header) {
80
            $limits[$header] = $this->getHeader($header);
81
        }
82
83
        return $limits;
84
    }
85
86
    /**
87
     * Get body.
88
     *
89
     * @return string
90
     */
91 4
    public function getBody()
92
    {
93 4
        return $this->body;
94
    }
95
96
    /**
97
     * Get HTTP status code.
98
     *
99
     * @return string
100
     */
101 4
    public function getStatus()
102
    {
103 4
        return $this->status;
104
    }
105
106
    /**
107
     * Get response headers.
108
     *
109
     * @return array
110
     */
111 2
    public function getHeaders()
112
    {
113 2
        return $this->headers;
114
    }
115
116
    /**
117
     * Get a response header by key.
118
     *
119
     * @param  string $key
120
     *
121
     * @return string
122
     */
123 4
    public function getHeader($key)
124
    {
125 4
        if (array_key_exists($key, $this->headers)) {
126 2
            return $this->headers[$key];
127
        }
128 2
    }
129
}
130