IpDetector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 44
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B detect() 0 12 5
1
<?php namespace Arcanedev\GeoIP\Support;
2
3
/**
4
 * Class     IpDetector
5
 *
6
 * @package  Arcanedev\GeoIP\Support
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class IpDetector
10
{
11
    /* -----------------------------------------------------------------
12
     |  Properties
13
     | -----------------------------------------------------------------
14
     */
15
16
    /** @var string */
17
    protected static $default = '127.0.0.0';
18
19
    /** @var array */
20
    protected static $remotes = [
21
        'HTTP_X_FORWARDED_FOR',
22
        'HTTP_CLIENT_IP',
23
        'HTTP_X_FORWARDED',
24
        'HTTP_FORWARDED_FOR',
25
        'HTTP_FORWARDED',
26
        'REMOTE_ADDR',
27
        'HTTP_X_CLUSTER_CLIENT_IP',
28
    ];
29
30
    /* -----------------------------------------------------------------
31
     |  Main Methods
32
     | -----------------------------------------------------------------
33
     */
34
35
    /**
36
     * Detect the IP address.
37
     *
38
     * @return string
39
     */
40 18
    public static function detect()
41
    {
42 18
        foreach (static::$remotes as $remote) {
43 18
            if ($address = getenv($remote)) {
44
                foreach (explode(',', $address) as $ipAddress) {
45 12
                    if (IpValidator::validate($ipAddress)) return $ipAddress;
46
                }
47
            }
48
        }
49
50 18
        return static::$default;
51
    }
52
}
53