Completed
Push — master ( 07bfac...b814cf )
by Kamran
02:37
created

Geocode::getPostcode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace kamranahmedse;
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 v1.1
14
 */
15
class Geocode
16
{
17
    /**
18
     * API URL through which the address will be obtained.
19
     */
20
    private $service_url = "://maps.googleapis.com/maps/api/geocode/json?";
21
22
    /**
23
     * Array containing the query results
24
     */
25
    private $service_results;
26
    
27
   /**
28
     * Constructor
29
     *
30
     * @param string $address The address that is to be parsed
0 ignored issues
show
Bug introduced by
There is no parameter named $address. 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...
31
     * @param boolean $secure_protocol true if you need to use HTTPS and false otherwise (Defaults to false)
0 ignored issues
show
Bug introduced by
There is no parameter named $secure_protocol. 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...
32
     * @param string $key GMAPS API KEY
33
     */
34 6
    public function __construct($key = null)
35
    {
36 6
        $this->service_url = (!is_null($key))
37 6
            ? 'https' . $this->service_url."key={$key}"
38 6
            : 'http' . $this->service_url;
39 6
    }
40
41
    /**
42
     * Returns the private $service_url
43
     * 
44
     * @return string The service URL
45
     */
46 5
    public function getServiceUrl()
47
    {
48 5
        return $this->service_url;
49
    }
50
51
    /**
52
     * get 
53
     * 
54
     * Sends request to the passed Google Geocode API URL and fetches the address details and returns them
55
     * 
56
     * @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...
57
     * @return bool|object false if no data is returned by URL and the detail otherwise
58
     */
59 4
    public function get($address)
60
    {
61 4
        if (is_null($address)|| $address=="") {
62 1
            throw new \Exception("Address is needed");
63
        }
64
65 3
        $url= $this->getServiceUrl() . "&address=" . urlencode($address);
66 3
        $ch = curl_init();
67
68 3
        curl_setopt($ch, CURLOPT_URL, $url);
69 3
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
70
        
71 3
        $service_results = json_decode(curl_exec($ch));
72 3
        if ($service_results && $service_results->status === 'OK') {
73 2
            $this->service_results = $service_results;
74 2
            return new LocationInfo($address, $this->service_results);
75
        }
76 1
        return new LocationInfo($address, new \StdClass);
77
    }
78
}
79