Completed
Push — master ( b814cf...f1473b )
by Kamran
13:45 queued 06:55
created

Geocode   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 26
Bugs 2 Features 7
Metric Value
wmc 8
c 26
b 2
f 7
lcom 1
cbo 1
dl 0
loc 64
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getServiceUrl() 0 4 1
A get() 0 20 4
1
<?php
2
3
namespace KamranAhmed\Geocode;
4
5
/**
6
 * Geocode
7
 *
8
 * A wrapper around Google's Geocode API that parses the address, 
9
 * to get different details regarding the address
10
 *
11
 * @author Kamran Ahmed <[email protected]>
12
 * @license http://www.opensource.org/licenses/MIT
13
 * @version v2.0
14
 */
15
class Geocode
16
{
17
    /**
18
     * API URL through which the address will be obtained.
19
     */
20
    private $serviceUrl = "://maps.googleapis.com/maps/api/geocode/json?";
21
22
    /**
23
     * Array containing the query results
24
     */
25
    private $serviceResults;
26
    
27
   /**
28
     * Constructor
29
     *
30
     * @param string $key Google Maps Geocoding API key
31
     */
32 6
    public function __construct($key = '')
33
    {
34 6
        $this->serviceUrl = (!empty($key))
35 6
            ? 'https' . $this->serviceUrl."key={$key}"
36 6
            : 'http' . $this->serviceUrl;
37 6
    }
38
39
    /**
40
     * Returns the private $serviceUrl
41
     * 
42
     * @return string The service URL
43
     */
44 5
    public function getServiceUrl()
45
    {
46 5
        return $this->serviceUrl;
47
    }
48
49
    /**
50
     * get 
51
     * 
52
     * Sends request to the passed Google Geocode API URL and fetches the address details and returns them
53
     * 
54
     * @param  string $url Google geocode API URL containing the address or latitude/longitude
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
     * @return bool|object false if no data is returned by URL and the detail otherwise
56
     */
57 4
    public function get($address)
58
    {
59 4
        if ( empty($address) ) {
60 1
            throw new \Exception("Address is required in order to process");
61
        }
62
63 3
        $url= $this->getServiceUrl() . "&address=" . urlencode($address);
64 3
        $ch = curl_init();
65
66 3
        curl_setopt($ch, CURLOPT_URL, $url);
67 3
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
68
        
69 3
        $serviceResults = json_decode(curl_exec($ch));
70 3
        if ($serviceResults && $serviceResults->status === 'OK') {
71 2
            $this->serviceResults = $serviceResults;
72 2
            return new Location($address, $this->serviceResults);
73
        }
74
    
75 1
        return new Location($address, new \stdClass);
76
    }
77
}
78