Geocoder::setStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Geocoder;
4
5
class Geocoder
6
{
7
    const API_URL = 'https://maps.google.com/maps/api/geocode';
8
    const VALID_OUTPUT_FORMAT = ['json', 'xml'];
9
10
    private $apiKey;
11
    private $rawResponse;
12
    private $status;
13
    private $errorMessage;
14
    private $results;
15
16
    /**
17
     * Geocoder constructor.
18
     *
19
     * @param string $apiKey
20
     */
21 4
    public function __construct(string $apiKey = '')
22
    {
23 4
        $this->setApiKey($apiKey);
24 4
    }
25
26
    /**
27
     * @return string
28
     */
29 3
    public function getApiKey(): string
30
    {
31 3
        return $this->apiKey;
32
    }
33
34
    /**
35
     * @param string $apiKey
36
     */
37 4
    public function setApiKey(string $apiKey)
38
    {
39 4
        $this->apiKey = $apiKey;
40 4
    }
41
42
    /**
43
     * @param        $address
44
     * @param string $region
45
     * @param string $outputFormat
46
     *
47
     * @throws \Exception
48
     *
49
     * @return $this
50
     */
51 4
    public function geocode(string $address, string $region = '', string $outputFormat = 'json')
52
    {
53 4
        if ($this->validateOutputFormat($outputFormat) !== true) {
54 1
            throw new \Exception("'{$outputFormat}' is not a valid format");
55
        }
56
57 3
        $rawResponse = file_get_contents($this->generateRequestUrl($address, $region, $outputFormat));
58
59 3
        $this->processRawResponse($rawResponse);
60
61 3
        return $rawResponse;
62
    }
63
64
    /**
65
     * @param $rawResponse
66
     */
67 3
    private function processRawResponse($rawResponse)
68
    {
69 3
        $this->setRawResponse($rawResponse);
70
71 3
        $responseArray = json_decode($rawResponse, true);
72 3
        $this->setStatus($responseArray['status']);
73
74 3
        if (isset($responseArray['error_message'])) {
75 2
            $this->setErrorMessage($responseArray['error_message']);
76
        }
77
78 3
        $this->setResults($responseArray['results']);
79 3
    }
80
81
    /**
82
     * @param $address
83
     *
84
     * @return array
85
     */
86 1
    public function getLatLng($address)
87
    {
88 1
        $this->geocode($address);
89
90 1
        if ($this->getStatus() !== 'OK') {
91 1
            return;
92
        }
93
94
        $latLng = [];
95
        foreach ($this->getResults() as $result) {
96
            $latLng[] = [
97
                'lat' => $result['geometry']['location']['lat'],
98
                'lng' => $result['geometry']['location']['lng'],
99
            ];
100
        }
101
102
        return $latLng;
103
    }
104
105
    /**
106
     * @param $format
107
     *
108
     * @return bool
109
     */
110 4
    private function validateOutputFormat(string $format): bool
111
    {
112 4
        if (in_array($format, self::VALID_OUTPUT_FORMAT)) {
113 3
            return true;
114
        }
115
116 1
        return false;
117
    }
118
119
    /**
120
     * @param        $address
121
     * @param string $region
122
     * @param string $outputFormat
123
     *
124
     * @return string
125
     */
126 3
    private function generateRequestUrl(string $address, string $region = '', string $outputFormat = 'json'): string
127
    {
128 3
        $baseUrl = self::API_URL.'/'.$outputFormat.'?address='.urlencode($address);
129
130 3
        if (!empty($region)) {
131 1
            $baseUrl .= "&region={$region}";
132
        }
133
134 3
        if (!empty($this->getApiKey())) {
135 2
            $baseUrl .= '&key='.$this->getApiKey();
136
        }
137
138 3
        return $baseUrl;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getRawResponse()
145
    {
146
        return $this->rawResponse;
147
    }
148
149
    /**
150
     * @param string $rawResponse
151
     */
152 3
    public function setRawResponse($rawResponse)
153
    {
154 3
        $this->rawResponse = $rawResponse;
155 3
    }
156
157
    /**
158
     * @return string
159
     */
160 1
    public function getStatus(): string
161
    {
162 1
        return $this->status;
163
    }
164
165
    /**
166
     * @param string $status
167
     */
168 3
    public function setStatus(string $status)
169
    {
170 3
        $this->status = $status;
171 3
    }
172
173
    /**
174
     * @return string
175
     */
176 1
    public function getErrorMessage(): string
177
    {
178 1
        return $this->errorMessage;
179
    }
180
181
    /**
182
     * @param string $errorMessage
183
     */
184 2
    public function setErrorMessage(string $errorMessage)
185
    {
186 2
        $this->errorMessage = $errorMessage;
187 2
    }
188
189
    /**
190
     * @return mixed
191
     */
192
    public function getResults()
193
    {
194
        return $this->results;
195
    }
196
197
    /**
198
     * @param mixed $results
199
     */
200 3
    public function setResults($results)
201
    {
202 3
        $this->results = $results;
203 3
    }
204
205
    /**
206
     * Check if the address exists
207
     * Normally 'ZERO_RESULTS' is returned if address does not exist.
208
     *
209
     * @param $address
210
     * @param string $region
211
     *
212
     * @return bool
213
     */
214
    public function isAddressValid(string $address, string $region = ''): bool
215
    {
216
        $this->geocode($address, $region);
217
218
        if ($this->getStatus() === 'OK') {
219
            return true;
220
        }
221
222
        return false;
223
    }
224
225
    /**
226
     * Check if there are multiple results (more than 1).
227
     *
228
     * @return bool
229
     */
230
    public function hasMultipleResults()
231
    {
232
        if ($this->getStatus() === 'OK' && count($this->getResults()) > 1) {
233
            return true;
234
        }
235
236
        return false;
237
    }
238
}
239