Passed
Push — master ( 9c21af...8e9b7d )
by Nicolaas
08:33
created

CloudFlareGeoIP::visitor_country()   B

Complexity

Conditions 11
Paths 55

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 32
rs 7.3166
c 0
b 0
f 0
cc 11
nc 55
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\CloudFlare\Api;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use Sunnysideup\Geoip\Geoip;
9
10
class CloudFlareGeoIP extends Geoip
11
{
12
    private static $debug_email = '';
13
14
    /**
15
     * Find the country for an IP address.
16
     *
17
     * Always returns a string (parent method may return array)
18
     *
19
     * To return the code only, pass in true for the
20
     * $codeOnly parameter.
21
     *
22
     * @param string $address The IP address to get the country of
23
     * @param boolean $codeOnly Returns just the country code
24
     *
25
     * @return string
26
     */
27
    public static function ip2country($address, $codeOnly = false)
28
    {
29
        $results1 = null;
30
        $results2 = null;
31
        if (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) {
32
            $results2 = $_SERVER['HTTP_CF_IPCOUNTRY'];
33
        } else {
34
            $results1 = parent::ip2country($address);
35
        }
36
        $returnValue = $results2 ?: $results1;
37
        if ($codeOnly) {
38
            if (is_array($returnValue)) {
39
                return $returnValue['code'];
40
            }
41
            return $returnValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $returnValue also could return the type boolean which is incompatible with the documented return type string.
Loading history...
42
        }
43
        $name = parent::countryCode2name($returnValue);
0 ignored issues
show
Bug introduced by
It seems like $returnValue can also be of type boolean and null; however, parameter $code of Sunnysideup\Geoip\Geoip::countryCode2name() does only seem to accept string, 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

43
        $name = parent::countryCode2name(/** @scrutinizer ignore-type */ $returnValue);
Loading history...
44
        return ['code' => $returnValue, 'name' => $name];
45
    }
46
47
    /**
48
     * Returns the country code, for the current visitor
49
     *
50
     * @return string|bool
51
     */
52
    public static function visitor_country()
53
    {
54
        $code = null;
55
        if (Director::isDev()) {
56
            if (isset($_GET['countryfortestingonly'])) {
57
                $code = $_GET['countryfortestingonly'];
58
                Controller::curr()->getRequest()->getSession()->set('countryfortestingonly', $code);
59
            }
60
            if ($code = Controller::curr()->getRequest()->getSession()->get('countryfortestingonly')) {
61
                Controller::curr()->getRequest()->getSession()->set('MyCloudFlareCountry', $code);
62
            }
63
        }
64
        if (! $code) {
65
            if (isset($_SERVER['HTTP_CF_IPCOUNTRY']) && $_SERVER['HTTP_CF_IPCOUNTRY']) {
66
                return $_SERVER['HTTP_CF_IPCOUNTRY'];
67
            }
68
            $code = Controller::curr()->getRequest()->getSession()->get('MyCloudFlareCountry');
69
            if (! $code) {
70
                if ($address = self::get_remote_address()) {
71
                    $code = CloudFlareGeoip::ip2country($address, true);
72
                }
73
                if (! $code) {
74
                    $code = self::get_default_country_code();
75
                }
76
                if (! $code) {
77
                    $code = Config::inst()->get('CloudFlareGeoip', 'default_country_code');
78
                }
79
                Controller::curr()->getRequest()->getSession()->set('MyCloudFlareCountry', $code);
80
            }
81
        }
82
83
        return $code;
84
    }
85
86
    /**
87
     * @see: http://stackoverflow.com/questions/14985518/cloudflare-and-logging-visitor-ip-addresses-via-in-php
88
     * @return string
89
     */
90
    public static function get_remote_address()
91
    {
92
        $ip = null;
93
        if (isset($_GET['ipfortestingonly']) && Director::isDev()) {
94
            $ip = $_GET['ipfortestingonly'];
95
        } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
96
            $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
97
        }
98
        if (! $ip ||
99
            ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
100
        ) {
101
            $ip = Controller::curr()->getRequest()->getIP();
102
        }
103
        return $ip;
104
    }
105
}
106