Passed
Push — master ( 7960db...d2b48f )
by Ehsan
01:42
created

Geocoder::__construct()   A

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 10
    public function __construct($apiKey = '')
22
    {
23 10
        $this->setApiKey($apiKey);
24 10
    }
25
26
    /**
27
     * @return string
28
     */
29 9
    public function getApiKey()
30
    {
31 9
        return $this->apiKey;
32
    }
33
34
    /**
35
     * @param string $apiKey
36
     */
37 10
    public function setApiKey($apiKey)
38
    {
39 10
        $this->apiKey = $apiKey;
40 10
    }
41
42
    /**
43
     * @param        $address
44
     * @param string $region
45
     * @param string $outputFormat
46
     *
47
     * @throws \Exception
48
     *
49
     * @return $this
50
     */
51 10
    public function geocode($address, $region = '', $outputFormat = 'json')
52
    {
53 10
        if ($this->validateOutputFormat($outputFormat) !== true) {
54 1
            throw new \Exception("'{$outputFormat}' is not a valid format");
55
        }
56
57 9
        $rawResponse = file_get_contents($this->generateRequestUrl($address, $region, $outputFormat));
58
59 9
        $this->processRawResponse($rawResponse);
60
61 9
        return $rawResponse;
62
    }
63
64
    /**
65
     * @param $rawResponse
66
     */
67 9
    private function processRawResponse($rawResponse)
68
    {
69 9
        $this->setRawResponse($rawResponse);
70
71 9
        $responseArray = json_decode($rawResponse, true);
72 9
        $this->setStatus($responseArray['status']);
73
74 9
        if (isset($responseArray['error_message'])) {
75 2
            $this->setErrorMessage($responseArray['error_message']);
76
        }
77
78 9
        $this->setResults($responseArray['results']);
79 9
    }
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 10
    private function validateOutputFormat($format)
111
    {
112 10
        if (in_array($format, self::VALID_OUTPUT_FORMAT)) {
113 9
            return true;
114
        }
115
116 1
        return false;
117
    }
118
119
    /**
120
     * @param        $address
121
     * @param string $region
122
     * @param string $outputFormat
123
     * @param bool   $sensor
124
     *
125
     * @return string
126
     */
127 9
    private function generateRequestUrl($address, $region = '', $outputFormat = 'json', $sensor = false)
128
    {
129 9
        $baseUrl = self::API_URL.'/'.$outputFormat.'?address='.urlencode($address).'&sensor='.$sensor;
130
131 9
        if (!empty($region)) {
132 2
            $baseUrl .= "&region={$region}";
133
        }
134
135 9
        if (!empty($this->getApiKey())) {
136 2
            $baseUrl .= '&key='.$this->getApiKey();
137
        }
138
139 9
        return $baseUrl;
140
    }
141
142
    /**
143
     * @return string
144
     */
145 1
    public function getRawResponse()
146
    {
147 1
        return $this->rawResponse;
148
    }
149
150
    /**
151
     * @param string $rawResponse
152
     */
153 9
    public function setRawResponse($rawResponse)
154
    {
155 9
        $this->rawResponse = $rawResponse;
156 9
    }
157
158
    /**
159
     * @return string
160
     */
161 3
    public function getStatus()
162
    {
163 3
        return $this->status;
164
    }
165
166
    /**
167
     * @param string $status
168
     */
169 9
    public function setStatus($status)
170
    {
171 9
        $this->status = $status;
172 9
    }
173
174
    /**
175
     * @return string
176
     */
177 1
    public function getErrorMessage()
178
    {
179 1
        return $this->errorMessage;
180
    }
181
182
    /**
183
     * @param string $errorMessage
184
     */
185 2
    public function setErrorMessage($errorMessage)
186
    {
187 2
        $this->errorMessage = $errorMessage;
188 2
    }
189
190
    /**
191
     * @return mixed
192
     */
193 2
    public function getResults()
194
    {
195 2
        return $this->results;
196
    }
197
198
    /**
199
     * @param mixed $results
200
     */
201 9
    public function setResults($results)
202
    {
203 9
        $this->results = $results;
204 9
    }
205
}
206