Completed
Branch development (f30b60)
by Daryl
01:58
created

Geocoder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
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() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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' => sprintf( __( 'Please set the api key for class %1$s', 'cgm' ), get_called_class() ),
25
        ) );
26
27
        $this->_api_key = $args['api_key'];
28
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    function api_key() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
36
        return $this->_api_key;
37
38
    }
39
40
    /**
41
     * @param  string $address
42
     * @return bool
43
     */
44
    function flush_item_from_cache( $address ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
46
        return wp_cache_delete( $this->_make_cache_key( $this->_make_url( $address ) ) );
47
48
    }
49
50
    /**
51
     * @param  string $address
52
     * @return array|\WP_Error
53
     */
54
    function geocode( $address ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
56
        $url       = $this->_make_url( $address );
57
        $cache_key = $this->_make_cache_key( $url );
58
59
        if ( ! $return = wp_cache_get( $cache_key ) ) {
60
            if ( wp_http_validate_url( $url ) ) {
61
                $request = wp_remote_get( $url );
62
63
                if ( 200 == $request['response']['code'] ) {
64
                    $return = json_decode( $request['body'], true );
65
                    wp_cache_set( $cache_key, $return );
66
                }
67
68
                if ( ! 200 == $request['response']['code'] ) {
69
                    $return = new \WP_Error( $request['response']['code'], $request['response']['message'] );
70
                }
71
72
            }
73
        }
74
75
        if ( ! is_wp_error( $return ) ) {
76
            $return = $this->_parse_response( $return );
77
        }
78
79
        return $return;
80
81
    }
82
83
    /**
84
     * @param  string $url
85
     * @return string
86
     */
87
    private function _make_cache_key( $url ) {
88
89
        return md5( serialize( $url ) );
90
91
    }
92
93
    /**
94
     * @param  string $address
95
     * @return string
96
     */
97
    private function _make_url( $address ) {
98
99
        return sprintf(
100
            'https://maps.googleapis.com/maps/api/geocode/json?address=%1$s&key=%2$s',
101
            urlencode( filter_var( $address, FILTER_SANITIZE_STRING ) ),
102
            self::api_key()
103
        );
104
105
    }
106
107
    /**
108
     * Convert the response body into an array containing the latitude/longitude.
109
     *
110
     * @param  array $response
111
     * @return array
112
     */
113
    private function _parse_response( $response ) {
114
115
        $return = array();
116
117
        if ( isset( $response['results'][0]['geometry']['location'] ) ) {
118
            $return['lat']  = $response['results'][0]['geometry']['location']['lat'];
119
            $return['lng'] = $response['results'][0]['geometry']['location']['lng'];
120
        }
121
122
        return $return;
123
124
    }
125
126
}
127