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

HTTP   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 43
ccs 12
cts 14
cp 0.8571
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A make_request() 0 18 3
A _get_data() 0 12 2
1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class HTTP
7
 * @package Clubdeuce\WPGoogleMaps
8
 */
9
class HTTP {
10
11
	/**
12
	 * @param  string $url
13
	 * @return \WP_HTTP_Response|\WP_Error
14
	 */
15 2
	public function make_request( $url ) {
16
17 2
		$return = new \WP_Error( 1, 'Invalid URL', $url );
18
19 2
		if ( wp_http_validate_url( $url ) ) {
20 1
			$request = $this->_get_data( $url );
21
22 1
			$return = new \WP_Error( $request['response']['code'], $request['response']['message'] );
23
24 1
			if ( 200 == $request['response']['code'] ) {
25 1
				$return = $request;
26
			}
27
28
		}
29
30 2
		return $return;
31
32
	}
33
34
	/**
35
	 * @param $url
36
	 * @return array|\WP_Error
37
	 */
38 1
	private function _get_data( $url ) {
39
40 1
		$cache_key = md5( serialize( $url ) );
41
42 1
		if ( ! $data = wp_cache_get( $cache_key ) ) {
43
			$data = wp_remote_get( $url );
44
			wp_cache_add( $cache_key, $data, 300 );
45
		}
46
47 1
		return $data;
48
49
	}
50
51
}