Passed
Push — master ( 8426b9...53b7ea )
by Paul
07:11 queued 03:34
created

Cache   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 34.38%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 60
ccs 11
cts 32
cp 0.3438
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRemotePostTest() 0 12 4
A getCloudflareIps() 0 18 4
A getReviewCountsFor() 0 12 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\SqlQueries;
7
8
class Cache
9
{
10
	const EXPIRY_TIME = WEEK_IN_SECONDS;
11
12
	/**
13
 	 * @return array
14
	 */
15 1
	public function getCloudflareIps()
16
	{
17 1
		$ipAddresses = get_transient( Application::ID.'_cloudflare_ips' );
18 1
		if( $ipAddresses === false ) {
19 1
			$ipAddresses = array_fill_keys( ['v4', 'v6'], [] );
20 1
			foreach( array_keys( $ipAddresses ) as $version ) {
21 1
				$response = wp_remote_get( 'https://www.cloudflare.com/ips-'.$version );
22 1
				if( is_wp_error( $response )) {
23
					glsr_log()->error( $response->get_error_message() );
24
					continue;
25
				}
26 1
				$ipAddresses[$version] = array_filter(
27 1
					(array)preg_split( '/\R/', wp_remote_retrieve_body( $response ))
1 ignored issue
show
Bug introduced by
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_body() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
					(array)preg_split( '/\R/', wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response ))
Loading history...
28
				);
29
			}
30 1
			set_transient( Application::ID.'_cloudflare_ips', $ipAddresses, static::EXPIRY_TIME );
31
		}
32 1
		return $ipAddresses;
33
	}
34
35
	/**
36
	 * @param string $metaKey
37
	 * @return array
38
	 */
39
	public function getReviewCountsFor( $metaKey )
40
	{
41
		$counts = wp_cache_get( Application::ID, $metaKey.'_count' );
42
		if( $counts === false ) {
43
			$counts = [];
44
			$results = glsr( SqlQueries::class )->getReviewCountsFor( $metaKey );
45
			foreach( $results as $result ) {
46
				$counts[$result->name] = $result->num_posts;
47
			}
48
			wp_cache_set( Application::ID, $counts, $metaKey.'_count' );
49
		}
50
		return $counts;
51
	}
52
53
	/**
54
	 * @return string
55
	 */
56
	public function getRemotePostTest()
57
	{
58
		$test = get_transient( Application::ID.'_remote_post_test' );
59
		if( $test === false ) {
60
			$response = wp_remote_post( 'https://api.wordpress.org/stats/php/1.0/' );
61
			$test = !is_wp_error( $response )
62
				&& in_array( $response['response']['code'], range( 200, 299 ))
63
				? 'Works'
64
				: 'Does not work';
65
			set_transient( Application::ID.'_remote_post_test', $test, static::EXPIRY_TIME );
66
		}
67
		return $test;
68
	}
69
}
70