Completed
Push — master ( d811b7...4ff36d )
by Ehsan
01:31
created

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