Completed
Push — master ( 96d573...f9f049 )
by Ehsan
07:54
created

Geocoder::getLatLng()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
ccs 11
cts 11
cp 1
crap 3
1
<?php
2
3
namespace Geocoder;
4
5
class Geocoder
6
{
7
    const API_URL = 'http://maps.google.com/maps/api/geocode';
8
9
    /**
10
     * @param        $address
11
     * @param string $outputFormat
12
     *
13
     * @return string
14
     */
15 6
    public function geocode($address, $outputFormat = 'json')
16
    {
17 6
        return file_get_contents(
18 6
            self::API_URL.'/'.$outputFormat.'?address='.urlencode($address).'&sensor=false'
19
        );
20
    }
21
22
    /**
23
     * @param $address
24
     *
25
     * @return array
26
     */
27 3
    public function getLatLng($address)
28
    {
29 3
        $result = $this->geocode($address);
30 3
        $result = json_decode($result, true);
31
32 3
        if ($result['status'] !== 'OK') {
33 1
            return;
34
        }
35
36 2
        $latLng = [];
37 2
        foreach ($result['results'] as $result) {
38 2
            $latLng[] = [
39 2
                'lat' => $result['geometry']['location']['lat'],
40 2
                'lng' => $result['geometry']['location']['lng'],
41
            ];
42
        }
43
44 2
        return $latLng;
45
    }
46
}
47