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

Geocoder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 14
cts 14
cp 1
wmc 4
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A geocode() 0 6 1
A getLatLng() 0 19 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