Passed
Pull Request — master (#9)
by
unknown
17:20
created

QueryResult   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 167
rs 10
c 0
b 0
f 0
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 3
A getResponse() 0 6 2
A isObject() 0 3 1
A getErrorMessage() 0 4 3
A hasError() 0 10 4
A isArray() 0 3 1
A parameterExists() 0 6 2
A getResultCount() 0 6 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
    /**
14
     * The raw HTTP response.
15
     *
16
     * @var \GuzzleHttp\Psr7\Response
17
     */
18
    protected $response;
19
20
    /**
21
     * The decoded response.
22
     *
23
     * @var mixed
24
     */
25
    protected $decodedResponse;
26
27
    /**
28
     * Constructs a QueryResult object.
29
     *
30
     * @param \GuzzleHttp\Psr7\Response $response
31
     *   The HTTP response from the Matomo server that contains the query result.
32
     */
33
    public function __construct(Response $response)
34
    {
35
        $this->response = $response;
36
    }
37
38
    /**
39
     * Returns the decoded response that was returned by the Matomo server.
40
     *
41
     * @return mixed
42
     *   The response. Can be an object, or an array of objects in case multiple
43
     *   results are returned.
44
     */
45
    public function getResponse()
46
    {
47
        if (empty($this->decodedResponse)) {
48
            $this->decodedResponse = json_decode($this->response->getBody());
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
     * @return mixed
104
     *   The value.
105
     *
106
     * @throws \InvalidArgumentException
107
     *   Thrown when no parameter with the given name exists, or if the response
108
     *   is not an object.
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
        return property_exists($this->getResponse(), $name);
138
    }
139
140
    /**
141
     * Checks whether an object was returned.
142
     *
143
     * @return bool
144
     *   TRUE if the response is an object, FALSE if it is something else.
145
     */
146
    public function isObject()
147
    {
148
        return is_object($this->getResponse());
149
    }
150
151
    /**
152
     * Checks whether an array was returned.
153
     *
154
     * @return bool
155
     *   TRUE if the response is an array, FALSE if it is something else.
156
     */
157
    public function isArray()
158
    {
159
        return is_array($this->getResponse());
160
    }
161
162
    /**
163
     * Returns the number of results that are present in the response.
164
     *
165
     * @return int
166
     *   The number of results.
167
     *
168
     * @throws \DomainException
169
     *   Thrown when the response that was returned by Matomo was not an array.
170
     */
171
    public function getResultCount()
172
    {
173
        if (!$this->isArray()) {
174
            throw new \DomainException('Cannot get result count, the response is not an array.');
175
        }
176
        return count($this->getResponse());
177
    }
178
}
179