Completed
Push — master ( d33c99...5d6517 )
by Roberto
03:55 queued 10s
created

GoogleMapsResponse::setResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2018 - present
4
 * Google Maps PHP - GoogleMapsResponse.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 5/9/2018
8
 * MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\GoogleMaps\Http;
12
13
use Biscolab\GoogleMaps\Exception\RequestException;
14
use Biscolab\GoogleMaps\Exception\ResponseException;
15
use Biscolab\GoogleMaps\Fields\GoogleMapsResponseFields;
16
use Biscolab\GoogleMaps\Values\GoogleMapsResponseStatusValues;
17
use GuzzleHttp\Psr7\Response;
18
19
/**
20
 * Class GoogleMapsResponse
21
 * @package Biscolab\GoogleMaps\Http
22
 */
23
class GoogleMapsResponse
24
{
25
26
	/**
27
	 * @var Response
28
	 */
29
	protected $response = null;
30
31
	/**
32
	 * contains an array of places, with information about each.
33
	 * The Places API returns up to 20 establishment results per query.
34
	 * Additionally, political results may be returned which serve to identify the area of the request.
35
	 *
36
	 * @var array
37
	 * @see https://developers.google.com/places/web-service/search#PlaceSearchResults
38
	 */
39
	protected $results = null;
40
41
	/**
42
	 * contains metadata on the request.
43
	 *
44
	 * @var string
45
	 * @see https://developers.google.com/places/web-service/search#PlaceSearchStatusCodes
46
	 */
47
	protected $status = null;
48
49
	/**
50
	 * @var string
51
	 */
52
	protected $error_message = null;
53
54
	/**
55
	 * @var array
56
	 */
57
	protected $array_response = null;
58
59
	/**
60
	 * @var null|array
61
	 */
62
	protected $http_status_code = null;
63
64
	/**
65
	 * may contain a set of attributions about this listing which must be displayed to the user (some listings may not have attribution).
66
	 *
67
	 * @var null|array
68
	 * @since 0.5.0
69
	 */
70
	protected $html_attributions = null;
71
72
	/**
73
	 * @var null|string
74
	 * @since 0.5.0
75
	 */
76
	protected $next_page_token = null;
77
78
	/**
79
	 * GoogleMapsResponse constructor.
80
	 *
81
	 * @param Response $response
82
	 */
83
	public function __construct(Response $response)
84
	{
85
86
		$this->setResponse($response);
87
88
		$this->parseResponse();
89
90
		$this->checkHttpStatusCode();
91
	}
92
93
	/**
94
	 * @param Response $response
95
	 *
96
	 * @return GoogleMapsResponse
97
	 */
98
	public function setResponse(Response $response): GoogleMapsResponse
99
	{
100
101
		$this->response = $response;
102
103
		return $this;
104
	}
105
106
	/**
107
	 * @return GoogleMapsResponse
108
	 *
109
	 * @throws RequestException
110
	 * @throws ResponseException
111
	 */
112
	protected function parseResponse(): GoogleMapsResponse
113
	{
114
115
		$json_response = $this->response->getBody()->getContents();
116
		$array_response = $this->toArray($json_response);
117
		$results = null;
118
119
		if (empty($array_response[GoogleMapsResponseFields::STATUS])) {
120
			throw new ResponseException('Missing "status" in GoogleMapsApi Response');
121
		}
122
		$this->setStatus($array_response[GoogleMapsResponseFields::STATUS]);
123
124
		if ($this->getStatus() != GoogleMapsResponseStatusValues::OK) {
125
			$error_message = 'Something went wrong';
126
			if (!empty($array_response[GoogleMapsResponseFields::ERROR_MESSAGE])) {
127
				$error_message = $array_response[GoogleMapsResponseFields::ERROR_MESSAGE];
128
				$this->setErrorMessage($error_message);
129
			} elseif (!empty($array_response[GoogleMapsResponseFields::STATUS])) {
130
				$error_message .= ': ' . $array_response[GoogleMapsResponseFields::STATUS];
131
				$this->setErrorMessage($error_message);
132
			}
133
			throw new RequestException($error_message);
134
135
		}
136
137
		if (!empty($array_response[GoogleMapsResponseFields::RESULTS])) {
138
			$results = $array_response[GoogleMapsResponseFields::RESULTS];
139
140
		} elseif (!empty($array_response[GoogleMapsResponseFields::CANDIDATES])) {
141
			$results = $array_response[GoogleMapsResponseFields::CANDIDATES];
142
143
		} else {
144
			throw new ResponseException('Missing "results" in GoogleMapsApi Response');
145
		}
146
		$this->setResults($results);
147
148
		if (!empty($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS])) {
149
			$this->setHtmlAttributions($array_response[GoogleMapsResponseFields::HTML_ATTRIBUTIONS]);
150
		}
151
152
		if (!empty($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN])) {
153
			$this->setNextPageToken($array_response[GoogleMapsResponseFields::NEXT_PAGE_TOKEN]);
154
		}
155
156
		return $this;
157
	}
158
159
	/**
160
	 * @param string $json_response
161
	 *
162
	 * @return array
163
	 */
164
	public function toArray(string $json_response): array
165
	{
166
167
		$this->array_response = json_decode($json_response, true);
168
169
		return $this->array_response;
170
	}
171
172
	/**
173
	 * @return string
174
	 */
175
	public function getStatus(): string
176
	{
177
178
		return $this->status;
179
	}
180
181
	/**
182
	 * @param string $status
183
	 *
184
	 * @return GoogleMapsResponse
185
	 */
186
	public function setStatus(string $status)
187
	{
188
189
		$this->status = $status;
190
191
		return $this;
192
	}
193
194
	/**
195
	 * Check HTTP status code (silent/No exceptions!)
196
	 * @return int
197
	 */
198
	protected function checkHttpStatusCode(): int
199
	{
200
201
		$this->http_status_code = $this->response->getStatusCode();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->response->getStatusCode() of type integer is incompatible with the declared type array|null of property $http_status_code.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
202
203
		return $this->http_status_code;
204
	}
205
206
	/**
207
	 * @return array
208
	 */
209
	public function getResults()
210
	{
211
212
		return $this->results;
213
	}
214
215
	/**
216
	 * @param array $results
217
	 *
218
	 * @return $this
219
	 */
220
	public function setResults(array $results)
221
	{
222
223
		$this->results = $results;
224
225
		return $this;
226
	}
227
228
	/**
229
	 * @return array
230
	 */
231
	public function getArrayResponse(): array
232
	{
233
234
		return $this->array_response;
235
	}
236
237
	/**
238
	 * @param array $array_response
239
	 *
240
	 * @return GoogleMapsResponse
241
	 */
242
	public function setArrayResponse(array $array_response): GoogleMapsResponse
243
	{
244
245
		$this->array_response = $array_response;
246
247
		return $this;
248
	}
249
250
	/**
251
	 * @return mixed
252
	 */
253
	public function getErrorMessage()
254
	{
255
256
		return $this->error_message;
257
	}
258
259
	/**
260
	 * @param $error_message
261
	 *
262
	 * @return GoogleMapsResponse
263
	 */
264
	public function setErrorMessage($error_message): GoogleMapsResponse
265
	{
266
267
		$this->error_message = $error_message;
268
269
		return $this;
270
	}
271
272
	/**
273
	 * @return int
274
	 */
275
	public function getHttpStatusCode(): int
276
	{
277
278
		return intval($this->http_status_code);
279
	}
280
281
	/**
282
	 * @return array|null
283
	 */
284
	public function getHtmlAttributions(): ?array
285
	{
286
287
		return $this->html_attributions;
288
	}
289
290
	/**
291
	 * @param array|null $html_attributions
292
	 *
293
	 * @return GoogleMapsResponse
294
	 */
295
	public function setHtmlAttributions(?array $html_attributions): GoogleMapsResponse
296
	{
297
298
		$this->html_attributions = $html_attributions;
299
300
		return $this;
301
	}
302
303
	/**
304
	 * @return string
305
	 */
306
	public function getNextPageToken(): string
307
	{
308
309
		return $this->next_page_token ?? '';
310
	}
311
312
	/**
313
	 * @param $next_page_token
314
	 *
315
	 * @return GoogleMapsResponse
316
	 */
317
	public function setNextPageToken($next_page_token): GoogleMapsResponse
318
	{
319
320
		$this->next_page_token = $next_page_token;
321
322
		return $this;
323
	}
324
325
}