Passed
Push — master ( a406f9...3ba855 )
by Paul
08:12 queued 04:00
created

Cache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 37.84%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 67
ccs 14
cts 37
cp 0.3784
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRemotePostTest() 0 12 4
A getCloudflareIps() 0 25 5
A getReviewCountsFor() 0 12 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
7
class Cache
8
{
9
    const EXPIRY_TIME = WEEK_IN_SECONDS;
10
11
    /**
12
     * @return array
13
     */
14 1
    public function getCloudflareIps()
15
    {
16 1
        $ipAddresses = get_transient(Application::ID.'_cloudflare_ips');
17 1
        if (false === $ipAddresses) {
18 1
            $ipAddresses = array_fill_keys(['v4', 'v6'], []);
19 1
            foreach (array_keys($ipAddresses) as $version) {
20 1
                $url = 'https://www.cloudflare.com/ips-'.$version;
21 1
                $response = wp_remote_get($url, [
22 1
                    'sslverify' => false,
23
                ]);
24 1
                if (is_wp_error($response)) {
25
                    glsr_log()->error($response->get_error_message());
26
                    continue;
27
                }
28 1
                if ('200' != ($statusCode = wp_remote_retrieve_response_code($response))) {
29
                    glsr_log()->error('Unable to connect to '.$url.' ['.$statusCode.']');
30
                    continue;
31
                }
32 1
                $ipAddresses[$version] = array_filter(
33 1
                    (array) preg_split('/\R/', wp_remote_retrieve_body($response))
34
                );
35
            }
36 1
            set_transient(Application::ID.'_cloudflare_ips', $ipAddresses, static::EXPIRY_TIME);
37
        }
38 1
        return $ipAddresses;
39
    }
40
41
    /**
42
     * @param string $metaKey
43
     * @return array
44
     */
45
    public function getReviewCountsFor($metaKey)
46
    {
47
        $counts = wp_cache_get(Application::ID, $metaKey.'_count');
48
        if (false === $counts) {
49
            $counts = [];
50
            $results = glsr(SqlQueries::class)->getReviewCountsFor($metaKey);
51
            foreach ($results as $result) {
52
                $counts[$result->name] = $result->num_posts;
53
            }
54
            wp_cache_set(Application::ID, $counts, $metaKey.'_count');
55
        }
56
        return $counts;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getRemotePostTest()
63
    {
64
        $test = get_transient(Application::ID.'_remote_post_test');
65
        if (false === $test) {
66
            $response = wp_remote_post('https://api.wordpress.org/stats/php/1.0/');
67
            $test = !is_wp_error($response)
68
                && in_array($response['response']['code'], range(200, 299))
69
                ? 'Works'
70
                : 'Does not work';
71
            set_transient(Application::ID.'_remote_post_test', $test, static::EXPIRY_TIME);
72
        }
73
        return $test;
74
    }
75
}
76