Completed
Push — master ( 4ff36d...27c00a )
by Ehsan
01:36
created

Geocoder   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 0
dl 0
loc 196
ccs 63
cts 63
cp 1
rs 10
c 0
b 0
f 0

16 Methods

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