Completed
Push — master ( cd366a...2f8733 )
by Ehsan
01:33
created

Geocoder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 108
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getApiKey() 0 4 1
A setApiKey() 0 4 1
A geocode() 0 8 2
A getLatLng() 0 19 3
A validateOutputFormat() 0 8 2
A generateRequestUrl() 0 14 3
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
12 9
    public function __construct($apiKey = '')
13
    {
14 9
        $this->setApiKey($apiKey);
15 9
    }
16
17
    /**
18
     * @return string
19
     */
20 8
    public function getApiKey()
21
    {
22 8
        return $this->apiKey;
23
    }
24
25
    /**
26
     * @param string $apiKey
27
     */
28 9
    public function setApiKey($apiKey)
29
    {
30 9
        $this->apiKey = $apiKey;
31 9
    }
32
33
    /**
34
     * @param        $address
35
     * @param string $region
36
     * @param string $outputFormat
37
     *
38
     * @throws \Exception
39
     *
40
     * @return string
41
     */
42 9
    public function geocode($address, $region = '', $outputFormat = 'json')
43
    {
44 9
        if ($this->validateOutputFormat($outputFormat) !== true) {
45 1
            throw new \Exception("'{$outputFormat}' is not a valid format");
46
        }
47
48 8
        return file_get_contents($this->generateRequestUrl($address, $region, $outputFormat));
49
    }
50
51
    /**
52
     * @param $address
53
     *
54
     * @return array
55
     */
56 3
    public function getLatLng($address)
57
    {
58 3
        $result = $this->geocode($address);
59 3
        $result = json_decode($result, true);
60
61 3
        if ($result['status'] !== 'OK') {
62 1
            return;
63
        }
64
65 2
        $latLng = [];
66 2
        foreach ($result['results'] as $result) {
67 2
            $latLng[] = [
68 2
                'lat' => $result['geometry']['location']['lat'],
69 2
                'lng' => $result['geometry']['location']['lng'],
70
            ];
71
        }
72
73 2
        return $latLng;
74
    }
75
76
    /**
77
     * @param $format
78
     *
79
     * @return bool
80
     */
81 9
    private function validateOutputFormat($format)
82
    {
83 9
        if (in_array($format, self::VALID_OUTPUT_FORMAT)) {
84 8
            return true;
85
        }
86
87 1
        return false;
88
    }
89
90
    /**
91
     * @param        $address
92
     * @param string $region
93
     * @param string $outputFormat
94
     * @param bool   $sensor
95
     *
96
     * @return string
97
     */
98 8
    private function generateRequestUrl($address, $region = '', $outputFormat = 'json', $sensor = false)
99
    {
100 8
        $baseUrl = self::API_URL.'/'.$outputFormat.'?address='.urlencode($address).'&sensor='.$sensor;
101
102 8
        if (!empty($region)) {
103 2
            $baseUrl .= "&region={$region}";
104
        }
105
106 8
        if (!empty($this->getApiKey())) {
107 1
            $baseUrl .= '&key='.$this->getApiKey();
108
        }
109
110 8
        return $baseUrl;
111
    }
112
}
113