QueryResult   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 26
c 2
b 1
f 0
dl 0
loc 169
rs 10
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 3
A getResponse() 0 7 2
A isObject() 0 3 1
A getErrorMessage() 0 4 3
A hasError() 0 10 4
A isArray() 0 3 1
A parameterExists() 0 7 2
A getResultCount() 0 7 2
A __construct() 0 3 1
A getRawResponse() 0 3 1
1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck\Matomo;
4
5
use GuzzleHttp\Psr7\Response;
6
7
/**
8
 * Contains the result of a query to the Matomo Reporting API.
9
 */
10
class QueryResult
11
{
12
    /**
13
     * The raw HTTP response.
14
     *
15
     * @var \GuzzleHttp\Psr7\Response
16
     */
17
    protected $response;
18
19
    /**
20
     * The decoded response.
21
     *
22
     * @var mixed
23
     */
24
    protected $decodedResponse;
25
26
    /**
27
     * Constructs a QueryResult object.
28
     *
29
     * @param \GuzzleHttp\Psr7\Response $response
30
     *                                            The HTTP response from the Matomo server that contains the query result.
31
     */
32
    public function __construct(Response $response)
33
    {
34
        $this->response = $response;
35
    }
36
37
    /**
38
     * Returns the decoded response that was returned by the Matomo server.
39
     *
40
     * @return mixed
41
     *               The response. Can be an object, or an array of objects in case multiple
42
     *               results are returned.
43
     */
44
    public function getResponse()
45
    {
46
        if (empty($this->decodedResponse)) {
47
            $this->decodedResponse = json_decode($this->response->getBody());
48
        }
49
50
        return $this->decodedResponse;
51
    }
52
53
    /**
54
     * Returns the raw HTTP response object.
55
     *
56
     * Use this if you need to inspect the HTTP headers, or other data.
57
     *
58
     * @return \GuzzleHttp\Psr7\Response
59
     *                                   The HTTP response.
60
     */
61
    public function getRawResponse()
62
    {
63
        return $this->response;
64
    }
65
66
    /**
67
     * Returns whether or not an error occurred in the request.
68
     *
69
     * @return bool
70
     *              TRUE if an error occurred.
71
     */
72
    public function hasError()
73
    {
74
        if ($this->getRawResponse()->getStatusCode() != 200) {
75
            return true;
76
        }
77
        // If an error occurs the Matomo server still returns a 200 OK response,
78
        // but the body of the response will contain the string "error" in the
79
        // "result" parameter.
80
        // @see https://github.com/matomo/matomo/issues/7293
81
        return !$this->isObject() || $this->parameterExists('result') && $this->get('result') === 'error';
82
    }
83
84
    /**
85
     * Returns the error message if one is available.
86
     *
87
     * @return string|null
88
     *                     The error message, or NULL if no error message is available.
89
     */
90
    public function getErrorMessage()
91
    {
92
        if ($this->hasError() && $this->parameterExists('message')) {
93
            return $this->get('message');
94
        }
95
    }
96
97
    /**
98
     * Returns the value that corresponds with the given parameter name.
99
     *
100
     * @param string $name
101
     *                     The parameter name for which to return the value.
102
     *
103
     * @throws \InvalidArgumentException
104
     *                                   Thrown when no parameter with the given name exists, or if the response
105
     *                                   is not an object.
106
     *
107
     * @return mixed
108
     *               The value.
109
     */
110
    public function get($name)
111
    {
112
        if (!$this->isObject()) {
113
            throw new \InvalidArgumentException("Cannot retrieve parameter '$name', the response is not an object.");
114
        }
115
116
        if ($this->parameterExists($name)) {
117
            return $this->getResponse()->$name;
118
        }
119
120
        throw new \InvalidArgumentException("Parameter '$name' does not exist.");
121
    }
122
123
    /**
124
     * Checks whether the parameter with the given name exists.
125
     *
126
     * @param string $name
127
     *                     The name of the parameter to check.
128
     *
129
     * @return bool
130
     *              TRUE if the parameter exists.
131
     */
132
    public function parameterExists($name)
133
    {
134
        if (!$this->isObject()) {
135
            throw new \InvalidArgumentException("Cannot check if '$name' exists, the response is not an object.");
136
        }
137
138
        return property_exists($this->getResponse(), $name);
139
    }
140
141
    /**
142
     * Checks whether an object was returned.
143
     *
144
     * @return bool
145
     *              TRUE if the response is an object, FALSE if it is something else.
146
     */
147
    public function isObject()
148
    {
149
        return is_object($this->getResponse());
150
    }
151
152
    /**
153
     * Checks whether an array was returned.
154
     *
155
     * @return bool
156
     *              TRUE if the response is an array, FALSE if it is something else.
157
     */
158
    public function isArray()
159
    {
160
        return is_array($this->getResponse());
161
    }
162
163
    /**
164
     * Returns the number of results that are present in the response.
165
     *
166
     * @throws \DomainException
167
     *                          Thrown when the response that was returned by Matomo was not an array.
168
     *
169
     * @return int
170
     *             The number of results.
171
     */
172
    public function getResultCount()
173
    {
174
        if (!$this->isArray()) {
175
            throw new \DomainException('Cannot get result count, the response is not an array.');
176
        }
177
178
        return count($this->getResponse());
179
    }
180
}
181