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

HTTP::make_request()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
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
}