Passed
Push — master ( d2b48f...557931 )
by Ehsan
01:30
created

Geocoder::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 11
    public function __construct($apiKey = '')
22
    {
23 11
        $this->setApiKey($apiKey);
24 11
    }
25
26
    /**
27
     * @return string
28
     */
29 10
    public function getApiKey()
30
    {
31 10
        return $this->apiKey;
32
    }
33
34
    /**
35
     * @param string $apiKey
36
     */
37 11
    public function setApiKey($apiKey)
38
    {
39 11
        $this->apiKey = $apiKey;
40 11
    }
41
42
    /**
43
     * @param        $address
44
     * @param string $region
45
     * @param string $outputFormat
46
     *
47
     * @throws \Exception
48
     *
49
     * @return $this
50
     */
51 11
    public function geocode($address, $region = '', $outputFormat = 'json')
52
    {
53 11
        if ($this->validateOutputFormat($outputFormat) !== true) {
54 1
            throw new \Exception("'{$outputFormat}' is not a valid format");
55
        }
56
57 10
        $rawResponse = file_get_contents($this->generateRequestUrl($address, $region, $outputFormat));
58
59 10
        $this->processRawResponse($rawResponse);
60
61 10
        return $rawResponse;
62
    }
63
64
    /**
65
     * @param $rawResponse
66
     */
67 10
    private function processRawResponse($rawResponse)
68
    {
69 10
        $this->setRawResponse($rawResponse);
70
71 10
        $responseArray = json_decode($rawResponse, true);
72 10
        $this->setStatus($responseArray['status']);
73
74 10
        if (isset($responseArray['error_message'])) {
75 2
            $this->setErrorMessage($responseArray['error_message']);
76
        }
77
78 10
        $this->setResults($responseArray['results']);
79 10
    }
80
81
    /**
82
     * @param $address
83
     *
84
     * @return array
85
     */
86 3
    public function getLatLng($address)
87
    {
88 3
        $this->geocode($address);
89
90 3
        if ($this->getStatus() !== 'OK') {
91 1
            return;
92
        }
93
94 2
        $latLng = [];
95 2
        foreach ($this->getResults() as $result) {
96 2
            $latLng[] = [
97 2
                'lat' => $result['geometry']['location']['lat'],
98 2
                'lng' => $result['geometry']['location']['lng'],
99
            ];
100
        }
101
102 2
        return $latLng;
103
    }
104
105
    /**
106
     * @param $format
107
     *
108
     * @return bool
109
     */
110 11
    private function validateOutputFormat($format)
111
    {
112 11
        if (in_array($format, self::VALID_OUTPUT_FORMAT)) {
113 10
            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 10
    private function generateRequestUrl($address, $region = '', $outputFormat = 'json')
127
    {
128 10
        $baseUrl = self::API_URL.'/'.$outputFormat.'?address='.urlencode($address);
129
130 10
        if (!empty($region)) {
131 2
            $baseUrl .= "&region={$region}";
132
        }
133
134 10
        if (!empty($this->getApiKey())) {
135 2
            $baseUrl .= '&key='.$this->getApiKey();
136
        }
137
138 10
        return $baseUrl;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 1
    public function getRawResponse()
145
    {
146 1
        return $this->rawResponse;
147
    }
148
149
    /**
150
     * @param string $rawResponse
151
     */
152 10
    public function setRawResponse($rawResponse)
153
    {
154 10
        $this->rawResponse = $rawResponse;
155 10
    }
156
157
    /**
158
     * @return string
159
     */
160 4
    public function getStatus()
161
    {
162 4
        return $this->status;
163
    }
164
165
    /**
166
     * @param string $status
167
     */
168 10
    public function setStatus($status)
169
    {
170 10
        $this->status = $status;
171 10
    }
172
173
    /**
174
     * @return string
175
     */
176 1
    public function getErrorMessage()
177
    {
178 1
        return $this->errorMessage;
179
    }
180
181
    /**
182
     * @param string $errorMessage
183
     */
184 2
    public function setErrorMessage($errorMessage)
185
    {
186 2
        $this->errorMessage = $errorMessage;
187 2
    }
188
189
    /**
190
     * @return mixed
191
     */
192 2
    public function getResults()
193
    {
194 2
        return $this->results;
195
    }
196
197
    /**
198
     * @param mixed $results
199
     */
200 10
    public function setResults($results)
201
    {
202 10
        $this->results = $results;
203 10
    }
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
     * @return bool
212
     */
213 1
    public function isAddressValid($address, $region = '')
214
    {
215 1
        $this->geocode($address, $region);
216
217 1
        if ($this->getStatus() === 'OK') {
218
            return true;
219
        }
220
221 1
        return false;
222
    }
223
}
224