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

Response::limits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 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 24
    public function __construct($status = 200, array $headers = [], $body = null)
36
    {
37 24
        $this->status = $status;
38 24
        $this->headers = $headers;
39 24
        $this->body = $body;
40 24
    }
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
        return $content['result'];
59
    }
60
61
    /**
62
     * Get query limit headers.
63
     *
64
     * @param  array  $limits
65
     *
66
     * @return array
67
     */
68 2
    public function limits(array $limits = [])
69
    {
70
        $headers = [
71 2
            'X-Query-Limit-Limit',
72 2
            'X-Query-Limit-Remaining',
73
            'X-Query-Limit-Request-Queries'
74 2
        ];
75
76 2
        foreach ($headers as $header) {
77 2
            $limits[$header] = $this->getHeader($header);
78 2
        }
79
80 2
        return $limits;
81
    }
82
83
    /**
84
     * Get body.
85
     *
86
     * @return string
87
     */
88 4
    public function getBody()
89
    {
90 4
        return $this->body;
91
    }
92
93
    /**
94
     * Get HTTP status code.
95
     *
96
     * @return string
97
     */
98 4
    public function getStatus()
99
    {
100 4
        return $this->status;
101
    }
102
103
    /**
104
     * Get response headers.
105
     *
106
     * @return array
107
     */
108 2
    public function getHeaders()
109
    {
110 2
        return $this->headers;
111
    }
112
113
    /**
114
     * Get a response header by key.
115
     *
116
     * @param  string $key
117
     *
118
     * @return string
119
     */
120 6
    public function getHeader($key)
121
    {
122 6
        if (array_key_exists($key, $this->headers)) {
123 4
            return $this->headers[$key];
124
        }
125 4
    }
126
}
127