Passed
Branch master (41ef34)
by Roberto
04:08
created

GoogleMapsResponse   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 44
dl 0
loc 201
c 0
b 0
f 0
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getResults() 0 3 1
A __construct() 0 7 1
A setStatus() 0 5 1
A setErrorMessage() 0 5 1
A checkHttpStatusCode() 0 5 1
A toArray() 0 5 1
A setArrayResponse() 0 5 1
A parseResponse() 0 26 5
A getErrorMessage() 0 3 1
A setResponse() 0 5 1
A getArrayResponse() 0 3 1
A setResults() 0 5 1
A getHttpStatusCode() 0 3 1
A getStatus() 0 3 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
	 * @var Response
27
	 */
28
	protected $response = null;
29
30
	/**
31
	 * @var array
32
	 */
33
	protected $results = null;
34
35
	/**
36
	 * @var string
37
	 */
38
	protected $status = null;
39
40
	/**
41
	 * @var string
42
	 */
43
	protected $error_message = null;
44
45
	/**
46
	 * @var array
47
	 */
48
	protected $array_response = null;
49
50
	/**
51
	 * @var int
52
	 */
53
	protected $http_status_code = null;
54
55
	/**
56
	 * GoogleMapsResponse constructor.
57
	 *
58
	 * @param Response $response
59
	 */
60
	public function __construct(Response $response) {
61
62
		$this->setResponse($response);
63
64
		$this->parseResponse();
65
66
		$this->checkHttpStatusCode();
67
	}
68
69
	/**
70
	 * @param Response $response
71
	 *
72
	 * @return GoogleMapsResponse
73
	 */
74
	public function setResponse(Response $response): GoogleMapsResponse {
75
76
		$this->response = $response;
77
78
		return $this;
79
	}
80
81
	/**
82
	 * @return GoogleMapsResponse
83
	 *
84
	 * @throws RequestException
85
	 * @throws ResponseException
86
	 */
87
	protected function parseResponse(): GoogleMapsResponse {
88
89
		$json_response = $this->response->getBody()->getContents();
90
		$array_response = $this->toArray($json_response);
91
92
		if (is_null($array_response[GoogleMapsResponseFields::RESULTS])) {
93
			throw new ResponseException('Missing "results" in GoogleMapsApi Response');
94
		}
95
		$this->setResults($array_response[GoogleMapsResponseFields::RESULTS]);
96
97
		if (empty($array_response[GoogleMapsResponseFields::STATUS])) {
98
			throw new ResponseException('Missing "status" in GoogleMapsApi Response');
99
		}
100
		$this->setStatus($array_response[GoogleMapsResponseFields::STATUS]);
101
102
		if ($this->getStatus() != GoogleMapsResponseStatusValues::OK) {
103
			$error_message = 'Something went wrong';
104
			if (!empty($array_response[GoogleMapsResponseFields::ERROR_MESSAGE])) {
105
				$error_message = $array_response[GoogleMapsResponseFields::ERROR_MESSAGE];
106
				$this->setErrorMessage($error_message);
107
			}
108
			throw new RequestException($error_message);
109
110
		}
111
112
		return $this;
113
	}
114
115
	/**
116
	 * Check HTTP status code (silent/No exceptions!)
117
	 * @return int
118
	 */
119
	protected function checkHttpStatusCode(): int {
120
121
		$this->http_status_code = $this->response->getStatusCode();
122
123
		return $this->http_status_code;
124
	}
125
126
	/**
127
	 * @param string $json_response
128
	 *
129
	 * @return array
130
	 */
131
	public function toArray(string $json_response): array {
132
133
		$this->array_response = json_decode($json_response, true);
134
135
		return $this->array_response;
136
	}
137
138
	/**
139
	 * @return array
140
	 */
141
	public function getResults() {
142
143
		return $this->results;
144
	}
145
146
	/**
147
	 * @param array $results
148
	 *
149
	 * @return $this
150
	 */
151
	public function setResults(array $results) {
152
153
		$this->results = $results;
154
155
		return $this;
156
	}
157
158
	/**
159
	 * @return string
160
	 */
161
	public function getStatus(): string {
162
163
		return $this->status;
164
	}
165
166
	/**
167
	 * @param string $status
168
	 *
169
	 * @return GoogleMapsResponse
170
	 */
171
	public function setStatus(string $status) {
172
173
		$this->status = $status;
174
175
		return $this;
176
	}
177
178
	/**
179
	 * @return array
180
	 */
181
	public function getArrayResponse(): array {
182
183
		return $this->array_response;
184
	}
185
186
	/**
187
	 * @param array $array_response
188
	 *
189
	 * @return GoogleMapsResponse
190
	 */
191
	public function setArrayResponse(array $array_response): GoogleMapsResponse {
192
193
		$this->array_response = $array_response;
194
195
		return $this;
196
	}
197
198
	/**
199
	 * @return mixed
200
	 */
201
	public function getErrorMessage() {
202
203
		return $this->error_message;
204
	}
205
206
	/**
207
	 * @param $error_message
208
	 *
209
	 * @return GoogleMapsResponse
210
	 */
211
	public function setErrorMessage($error_message): GoogleMapsResponse {
212
213
		$this->error_message = $error_message;
214
215
		return $this;
216
	}
217
218
	/**
219
	 * @return int
220
	 */
221
	public function getHttpStatusCode(): int {
222
223
		return intval($this->http_status_code);
224
	}
225
226
}