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
|
|
|
return $this->decodedResponse;
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* Returns the raw HTTP response object.
|
54
|
|
|
*
|
55
|
|
|
* Use this if you need to inspect the HTTP headers, or other data.
|
56
|
|
|
*
|
57
|
|
|
* @return \GuzzleHttp\Psr7\Response
|
58
|
|
|
* The HTTP response.
|
59
|
|
|
*/
|
60
|
|
|
public function getRawResponse()
|
61
|
|
|
{
|
62
|
|
|
return $this->response;
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* Returns whether or not an error occurred in the request.
|
67
|
|
|
*
|
68
|
|
|
* @return bool
|
69
|
|
|
* TRUE if an error occurred.
|
70
|
|
|
*/
|
71
|
|
|
public function hasError()
|
72
|
|
|
{
|
73
|
|
|
if ($this->getRawResponse()->getStatusCode() != 200) {
|
74
|
|
|
return true;
|
75
|
|
|
}
|
76
|
|
|
// If an error occurs the Matomo server still returns a 200 OK response,
|
77
|
|
|
// but the body of the response will contain the string "error" in the
|
78
|
|
|
// "result" parameter.
|
79
|
|
|
// @see https://github.com/matomo/matomo/issues/7293
|
80
|
|
|
return !$this->isObject() || $this->parameterExists('result') && $this->get('result') === 'error';
|
81
|
|
|
}
|
82
|
|
|
|
83
|
|
|
/**
|
84
|
|
|
* Returns the error message if one is available.
|
85
|
|
|
*
|
86
|
|
|
* @return string|null
|
87
|
|
|
* The error message, or NULL if no error message is available.
|
88
|
|
|
*/
|
89
|
|
|
public function getErrorMessage()
|
90
|
|
|
{
|
91
|
|
|
if ($this->hasError() && $this->parameterExists('message')) {
|
92
|
|
|
return $this->get('message');
|
93
|
|
|
}
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
/**
|
97
|
|
|
* Returns the value that corresponds with the given parameter name.
|
98
|
|
|
*
|
99
|
|
|
* @param string $name
|
100
|
|
|
* The parameter name for which to return the value.
|
101
|
|
|
*
|
102
|
|
|
* @return mixed
|
103
|
|
|
* The value.
|
104
|
|
|
*
|
105
|
|
|
* @throws \InvalidArgumentException
|
106
|
|
|
* Thrown when no parameter with the given name exists, or if the response
|
107
|
|
|
* is not an object.
|
108
|
|
|
*/
|
109
|
|
|
public function get($name)
|
110
|
|
|
{
|
111
|
|
|
if (!$this->isObject()) {
|
112
|
|
|
throw new \InvalidArgumentException("Cannot retrieve parameter '$name', the response is not an object.");
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
|
if ($this->parameterExists($name)) {
|
116
|
|
|
return $this->getResponse()->$name;
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
throw new \InvalidArgumentException("Parameter '$name' does not exist.");
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
/**
|
123
|
|
|
* Checks whether the parameter with the given name exists.
|
124
|
|
|
*
|
125
|
|
|
* @param string $name
|
126
|
|
|
* The name of the parameter to check.
|
127
|
|
|
*
|
128
|
|
|
* @return bool
|
129
|
|
|
* TRUE if the parameter exists.
|
130
|
|
|
*/
|
131
|
|
|
public function parameterExists($name)
|
132
|
|
|
{
|
133
|
|
|
if (!$this->isObject()) {
|
134
|
|
|
throw new \InvalidArgumentException("Cannot check if '$name' exists, the response is not an object.");
|
135
|
|
|
}
|
136
|
|
|
return property_exists($this->getResponse(), $name);
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
/**
|
140
|
|
|
* Checks whether an object was returned.
|
141
|
|
|
*
|
142
|
|
|
* @return bool
|
143
|
|
|
* TRUE if the response is an object, FALSE if it is something else.
|
144
|
|
|
*/
|
145
|
|
|
public function isObject()
|
146
|
|
|
{
|
147
|
|
|
return is_object($this->getResponse());
|
148
|
|
|
}
|
149
|
|
|
|
150
|
|
|
/**
|
151
|
|
|
* Checks whether an array was returned.
|
152
|
|
|
*
|
153
|
|
|
* @return bool
|
154
|
|
|
* TRUE if the response is an array, FALSE if it is something else.
|
155
|
|
|
*/
|
156
|
|
|
public function isArray()
|
157
|
|
|
{
|
158
|
|
|
return is_array($this->getResponse());
|
159
|
|
|
}
|
160
|
|
|
|
161
|
|
|
/**
|
162
|
|
|
* Returns the number of results that are present in the response.
|
163
|
|
|
*
|
164
|
|
|
* @return int
|
165
|
|
|
* The number of results.
|
166
|
|
|
*
|
167
|
|
|
* @throws \DomainException
|
168
|
|
|
* Thrown when the response that was returned by Matomo was not an array.
|
169
|
|
|
*/
|
170
|
|
|
public function getResultCount()
|
171
|
|
|
{
|
172
|
|
|
if (!$this->isArray()) {
|
173
|
|
|
throw new \DomainException('Cannot get result count, the response is not an array.');
|
174
|
|
|
}
|
175
|
|
|
return count($this->getResponse());
|
176
|
|
|
}
|
177
|
|
|
} |