Completed
Push — master ( d59a31...6bab13 )
by Daryl
01:32
created

Geocoder::geocode()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.5806
cc 4
eloc 14
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class Geocoder
7
 * @package Clubdeuce\WPGoogleMaps
8
 */
9
class Geocoder {
10
11
	/**
12
	 * @var string
13
	 */
14
	protected $_api_key;
15
16
	/**
17
	 * @var HTTP
18
	 */
19
	protected $_http;
20
21
	/**
22
	 * Geocoder constructor.
23
	 *
24
	 * @param array $args
25
	 */
26 1
	public function __construct( $args = array() ) {
27
28 1
		$args = wp_parse_args( $args, array(
29 1
			'api_key' => Google_Maps::api_key(),
30 1
			'http'    => new HTTP(),
31
		) );
32
33 1
		$this->_api_key = $args['api_key'];
34 1
		$this->_http    = $args['http'];
35
36 1
	}
37
38
	/**
39
	 * @return string
40
	 */
41 1
	public function api_key() {
42
43 1
		return $this->_api_key;
44
45
	}
46
47
	/**
48
	 * @param  string $address
49
	 * @return Location|\WP_Error
50
	 */
51 3
	public function geocode( $address ) {
52
53 3
		$url = $this->_make_url( $address );
54
55 3
		$response = $this->_http->make_request( $url );
56
57
		do {
58 3
			if ( is_wp_error( $response ) ) {
59 1
				$location = $response;
60 1
				break;
61
			}
62
63 2
			$results = json_decode( wp_remote_retrieve_body( $response ), true );
64
65 2
			if ( 0 === count( $results['results'] ) ) {
66 1
				$location = new \WP_Error( '100', sprintf( '%1$s: %2$s', $results['status'], $results['error_message'] ) );
67 1
				break;
68
			}
69
70 1
			$location = $this->_make_location( $results['results'][0] );
71 1
		} while ( false );
72
73 3
		return $location;
74
75
	}
76
77
	/**
78
	 * @param  string $address
79
	 * @return string
80
	 */
81 2
	private function _make_url( $address ) {
82
83 2
		return sprintf(
84 2
			'https://maps.googleapis.com/maps/api/geocode/json?address=%1$s&key=%2$s',
85 2
			urlencode( filter_var( $address, FILTER_SANITIZE_STRING ) ),
86 2
			self::api_key()
87
		);
88
89
	}
90
91
	/**
92
	 * Convert the response body into an a Location object
93
	 *
94
	 * @param  array $results
95
	 * @return Location
96
	 */
97 2
	private function _make_location( $results ) {
98
99 2
		$response = new Location( array(
100 2
			'address'           => $results['formatted_address'],
101 2
			'formatted_address' => $results['formatted_address'],
102 2
			'state'             => self::_get_state_from_results( $results ),
103 2
			'zip_code'          => self::_get_zip_from_results( $results ),
104 2
			'latitude'          => $results['geometry']['location']['lat'],
105 2
			'longitude'         => $results['geometry']['location']['lng'],
106 2
			'place_id'          => $results['place_id'],
107 2
			'types'             => $results['types'],
108 2
			'viewport'          => $results['geometry']['viewport'],
109
		) );
110
111 2
		return $response;
112
113
	}
114
115
	/**
116
	 * @param  array  $results
117
	 * @return string
118
	 */
119 1
	private function _get_state_from_results( $results ) {
120
121 1
		return self::_get_value_from_results( 'administrative_area_level_1', $results );
122
123
	}
124
125
	/**
126
	 * @param  array $results
127
	 * @return string
128
	 */
129 1
	private function _get_zip_from_results( $results ) {
130
131 1
		return self::_get_value_from_results( 'postal_code', $results );
132
133
	}
134
135
	/**
136
	 * @param  string $value
137
	 * @param  array  $results
138
	 * @return string
139
	 */
140 2
	private function _get_value_from_results( $value, $results ) {
141
142 2
		$result_value = '';
143
144 2
		if ( isset( $results['address_components'] ) ) {
145 2
			foreach ( $results['address_components'] as $component ) {
146 2
				if ( $component['types'][0] === $value ) {
147 2
					$result_value = $component['short_name'];
148 2
					break;
149
				}
150
			}
151
		}
152
153 2
		return $result_value;
154
155
	}
156
157
}
158