Completed
Pull Request — master (#2)
by Daryl
02:44
created

Geocoder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 124
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A api_key() 0 5 1
A geocode() 0 13 2
A _make_url() 0 9 1
A _parse_response() 0 12 2
A _make_request() 0 18 3
A _get_data() 0 12 2
1
<?php
2
3
namespace Clubdeuce\WPLib\Components\GoogleMaps;
4
5
/**
6
 * Class Geocoder
7
 * @package Clubdeuce\WPLib\Components\GoogleMaps
8
 */
9
class Geocoder {
10
11
    /**
12
     * @var string
13
     */
14
    protected $_api_key;
15
16
    /**
17
     * KSA_Geocoder constructor.
18
     *
19
     * @param array $args
20
     */
21
    function __construct( $args = array() ) {
22
23
        $args = wp_parse_args( $args, array(
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $args. This often makes code more readable.
Loading history...
24
            'api_key' => '',
25
        ) );
26
27
        $this->_api_key = $args['api_key'];
28
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    function api_key() {
35
36
        return $this->_api_key;
37
38
    }
39
40
    /**
41
     * @param  string $address
42
     * @return array|\WP_Error
43
     */
44
    function geocode( $address ) {
45
46
        $url = $this->_make_url( $address );
47
48
        $return = $this->_make_request( $url );
49
50
        if ( ! is_wp_error( $return ) ) {
51
            $return = $this->_parse_response( $return );
52
        }
53
54
        return $return;
55
56
    }
57
58
    /**
59
     * @param  string $address
60
     * @return string
61
     */
62
    private function _make_url( $address ) {
63
64
        return sprintf(
65
            'https://maps.googleapis.com/maps/api/geocode/json?address=%1$s&key=%2$s',
66
            urlencode( filter_var( $address, FILTER_SANITIZE_STRING ) ),
67
            self::api_key()
68
        );
69
70
    }
71
72
    /**
73
     * Convert the response body into an array containing the latitude/longitude.
74
     *
75
     * @param  array $response
76
     * @return array Contains lat and lng as key/value pairs
77
     */
78
    private function _parse_response( $response ) {
79
80
        $return = array();
81
82
        if ( isset( $response['results'][0]['geometry']['location'] ) ) {
83
            $return['lat']  = $response['results'][0]['geometry']['location']['lat'];
84
            $return['lng'] = $response['results'][0]['geometry']['location']['lng'];
85
        }
86
87
        return $return;
88
89
    }
90
91
92
    /**
93
     * @param $url
94
     * @return array|\WP_Error
95
     */
96
    private function _make_request( $url ) {
97
98
        $return = new \WP_Error( 1, 'Invalid URL', $url );
99
100
        if ( wp_http_validate_url( $url ) ) {
101
            $request = $this->_get_data( $url );
102
103
            $return = new \WP_Error( $request['response']['code'], $request['response']['message'] );
104
105
            if ( 200 == $request['response']['code'] ) {
106
                $return = json_decode( $request['body'], true );
107
            }
108
109
        }
110
111
        return $return;
112
113
    }
114
115
    /**
116
     * @param $url
117
     * @return array|\WP_Error
118
     */
119
    private function _get_data( $url ) {
120
121
        $cache_key = md5( serialize( $url ) );
122
123
        if ( ! $data = wp_cache_get( $cache_key ) ) {
124
            $data = wp_remote_get( $url );
125
            wp_cache_add( $cache_key, $data, 300 );
126
        }
127
128
        return $data;
129
130
    }
131
132
}
133