Test Failed
Push — develop ( 33b521...cdbd76 )
by Paul
10:16 queued 21s
created

StatsManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 1
b 0
f 0
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A location() 0 19 5
A store() 0 13 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Defaults\GeolocationDefaults;
7
use GeminiLabs\SiteReviews\Geolocation;
8
use GeminiLabs\SiteReviews\Helper;
9
use GeminiLabs\SiteReviews\Review;
10
11
class StatsManager
12
{
13
    public function location(Review $review): array
14
    {
15
        $location = get_post_meta($review->ID, '_geolocation', true);
16
        if (!empty($location)) {
17
            return glsr(GeolocationDefaults::class)->restrict($location);
18
        }
19
        $fallback = glsr(GeolocationDefaults::class)->defaults();
20
        if (Helper::isLocalIpAddress($review->ip_address)) {
21
            return $fallback;
22
        }
23
        $response = glsr(Geolocation::class)->lookup($review->ip_address);
24
        if ($response->failed()) {
25
            return $fallback;
26
        }
27
        $location = $response->body();
28
        if (!$this->store($review, $location)) {
29
            return $fallback;
30
        }
31
        return glsr(GeolocationDefaults::class)->restrict($location);
32
    }
33
34
    public function store(Review $review, array $values): bool
35
    {
36
        $values = glsr(GeolocationDefaults::class)->restrict($values);
37
        $data = [
38
            'rating_id' => $review->rating_id,
39
        ];
40
        $data = wp_parse_args($data, $values);
41
        $result = glsr(Database::class)->insert('stats', $data);
42
        if (false === $result) {
43
            return false;
44
        }
45
        update_post_meta($review->ID, '_geolocation', $values);
46
        return true;
47
    }
48
}
49