CloudFlareGeoip   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 22
lcom 0
cbo 4
dl 0
loc 100
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B ip2country() 0 21 5
C visitor_country() 0 34 11
B get_remote_address() 0 16 6
1
<?php
2
3
class CloudFlareGeoip extends Geoip
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $debug_email = '';
0 ignored issues
show
Unused Code introduced by
The property $debug_email is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
7
    /**
8
     * Find the country for an IP address.
9
     *
10
     * Always returns a string (parent method may return array)
11
     *
12
     * To return the code only, pass in true for the
13
     * $codeOnly parameter.
14
     *
15
     * @param string $address The IP address to get the country of
16
     * @param boolean $codeOnly Returns just the country code
17
     *
18
     * @return string
19
     */
20
    public static function ip2country($address, $codeOnly = false)
0 ignored issues
show
Coding Style introduced by
ip2country uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
21
    {
22
        $results1 = null;
23
        $results2 = null;
24
        if (isset($_SERVER["HTTP_CF_IPCOUNTRY"])) {
25
            $results2 = $_SERVER["HTTP_CF_IPCOUNTRY"];
26
        } else {
27
            $results1 = parent::ip2country($address);
28
        }
29
        $returnValue = $results2 ? $results2 : $results1 ;
30
        if ($codeOnly) {
31
            if (is_array($returnValue)) {
32
                return $returnValue['code'];
33
            } else {
34
                return $returnValue;
35
            }
36
        } else {
37
            $name = $this->countryCode2name($returnValue);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
38
            return array('code' => $returnValue, 'name' => $name);
39
        }
40
    }
41
42
    /**
43
     * Returns the country code, for the current visitor
44
     *
45
     * @return string|bool
46
     */
47
    public static function visitor_country()
0 ignored issues
show
Coding Style introduced by
visitor_country uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
visitor_country uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48
    {
49
        $code = null;
50
        if (Director::isDev()) {
51
            if (isset($_GET['countryfortestingonly'])) {
52
                $code = $_GET['countryfortestingonly'];
53
                Session::set("countryfortestingonly", $code);
54
            }
55
            if ($code = Session::get("countryfortestingonly")) {
56
                Session::set("MyCloudFlareCountry", $code);
57
            }
58
        }
59
        if (!$code) {
60
            if (isset($_SERVER["HTTP_CF_IPCOUNTRY"]) && $_SERVER["HTTP_CF_IPCOUNTRY"]) {
61
                return $_SERVER["HTTP_CF_IPCOUNTRY"];
62
            } else {
63
                $code = Session::get("MyCloudFlareCountry");
64
                if (!$code) {
65
                    if ($address = self::get_remote_address()) {
66
                        $code = CloudFlareGeoip::ip2country($address, true);
67
                    }
68
                    if (!$code) {
69
                        $code = self::get_default_country_code();
70
                    }
71
                    if (!$code) {
72
                        $code = Config::inst()->get("EcommerceCountry", "default_country_code");
73
                    }
74
                    Session::set("MyCloudFlareCountry", $code);
75
                }
76
            }
77
        }
78
79
        return $code;
80
    }
81
82
    /**
83
     * @see: http://stackoverflow.com/questions/14985518/cloudflare-and-logging-visitor-ip-addresses-via-in-php
84
     * @return string
85
     */
86
    public static function get_remote_address()
0 ignored issues
show
Coding Style introduced by
get_remote_address uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
get_remote_address uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
87
    {
88
        $ip = null;
89
        if (isset($_GET["ipfortestingonly"]) && Director::isDev()) {
90
            $ip = $_GET["ipfortestingonly"];
91
        } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
92
            $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
93
        }
94
        if (
95
            !$ip ||
96
            !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
97
        ) {
98
            $ip = Controller::curr()->getRequest()->getIP();
99
        }
100
        return $ip;
101
    }
102
}
103