Completed
Push — feature/response-object ( 576038 )
by Johan
01:58
created

Response   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 66.67%

Importance

Changes 7
Bugs 0 Features 5
Metric Value
wmc 10
c 7
b 0
f 5
lcom 1
cbo 0
dl 0
loc 125
ccs 20
cts 30
cp 0.6667
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A result() 0 14 2
A getQueryLimitHeaders() 0 14 2
A getBody() 0 4 1
A getStatus() 0 4 1
A getHeaders() 0 4 1
A getHeader() 0 6 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 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