Completed
Push — master ( 6d0d95...b62338 )
by Jan-Paul
02:04
created

IP   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 10
c 5
b 0
f 2
lcom 1
cbo 2
dl 0
loc 106
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A fromString() 0 4 1
A fromCurrentRequest() 0 4 1
A validate() 0 4 1
A getVersion() 0 8 2
A equals() 0 4 1
A toString() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace HtaccessFirewall\Host;
4
5
use HtaccessFirewall\Host\Exception\InvalidArgumentException;
6
7
class IP implements Host
8
{
9
    const IPV4 = 'IPv4';
10
    const IPV6 = 'IPv6';
11
12
    /**
13
     * @var string
14
     */
15
    private $value;
16
17
    /**
18
     * Initialize IP.
19
     *
20
     * @param $value
21
     *
22
     * @throws InvalidArgumentException
23
     */
24
    private function __construct($value)
25
    {
26
        if (!self::validate($value)) {
27
            throw new InvalidArgumentException('The first parameter of IP must be a valid IP address.');
28
        }
29
30
        $this->value = $value;
31
    }
32
33
    /**
34
     * Create IP from string.
35
     *
36
     * @param $string
37
     *
38
     * @return IP
39
     */
40
    public static function fromString($string)
41
    {
42
        return new self($string);
43
    }
44
45
    /**
46
     * Create IP from current request.
47
     *
48
     * @return IP
49
     */
50
    public static function fromCurrentRequest()
0 ignored issues
show
Coding Style introduced by
fromCurrentRequest 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...
51
    {
52
        return new self($_SERVER['REMOTE_ADDR']);
53
    }
54
55
    /**
56
     * Check if string is a valid IP.
57
     *
58
     * @param string $value
59
     *
60
     * @return bool
61
     */
62
    public static function validate($value)
63
    {
64
        return filter_var($value, FILTER_VALIDATE_IP) !== false;
65
    }
66
67
    /**
68
     * Get the version (IPv4 or IPv6) of the IP.
69
     *
70
     * @return string
71
     */
72
    public function getVersion()
73
    {
74
        $isIPv4 = filter_var($this->toString(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
75
        if ($isIPv4 !== false) {
76
            return self::IPV4;
77
        }
78
        return self::IPV6;
79
    }
80
81
    /**
82
     * Compare equality with another Host.
83
     *
84
     * @param Host $other
85
     *
86
     * @return bool
87
     */
88
    public function equals(Host $other)
89
    {
90
        return $this->toString() === $other->toString();
91
    }
92
93
    /**
94
     * Get string representation of IP.
95
     *
96
     * @return string
97
     */
98
    public function toString()
99
    {
100
        return $this->value;
101
    }
102
103
    /**
104
     * Cast IP to string.
105
     *
106
     * @return string
107
     */
108
    public function __toString()
109
    {
110
        return $this->toString();
111
    }
112
}
113