ValidateIP   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 31
c 1
b 0
f 0
dl 0
loc 74
ccs 39
cts 39
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setIP() 0 3 1
A validateIPV6() 0 3 1
A sendRes() 0 14 2
A validateIPV4() 0 3 1
A getHost() 0 3 1
A __construct() 0 4 1
A setType() 0 3 1
A getIP() 0 3 1
A getType() 0 3 1
A validate() 0 11 3
1
<?php
2
3
namespace Hab\MeModule;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
class ValidateIP implements ContainerInjectableInterface
9
{
10
    use ContainerInjectableTrait;
11
    private $ip;
12
    private $type;
13
14 16
    public function __construct($ip = "")
15
    {
16 16
        $this->ip = $ip;
17 16
        $this->type = "unknown";
18 16
    }
19
20 7
    public function setIP(String $ip = "") : void
21
    {
22 7
        $this->ip = $ip;
23 7
    }
24
25 16
    public function getIP() : String
26
    {
27 16
        return $this->ip;
28
    }
29
30 11
    public function setType(String $type)
31
    {
32 11
        $this->type = $type;
33 11
    }
34
35 16
    public function getType() : String
36
    {
37 16
        return $this->type;
38
    }
39
40 16
    public function validateIPV4(String $ip)
41
    {
42 16
        return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
43
    }
44
45 9
    public function validateIPV6(String $ip)
46
    {
47 9
        return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
48
    }
49
50 11
    public function getHost()
51
    {
52 11
        return gethostbyaddr($this->ip);
53
    }
54
55 16
    public function validate()
56
    {
57 16
        if ($this->validateIPV4($this->ip)) {
58 8
            $this->setType("ipv4");
59 8
            return true;
60
        }
61 9
        if ($this->validateIPV6($this->ip)) {
62 3
            $this->setType("ipv6");
63 3
            return true;
64
        }
65 6
        return false;
66
    }
67
68 16
    public function sendRes()
69
    {
70 16
        $res = [];
71 16
        if (!$this->validate()) {
72 6
            $res["text"] = $this->ip . " is not a valid format";
73 6
            $res["isValid"] = false;
74
        } else {
75 11
            $res["isValid"] = true;
76 11
            $res["text"] = $this->ip . " is a valid " . $this->getType() . " adress";
77 11
            $res["host"] = $this->getHost();
78
        }
79 16
        $res["ip"] = $this->getIP();
80 16
        $res["type"] = $this->getType();
81 16
        return $res;
82
    }
83
84
    // public function multiValidate(Array $ipArray = [])
85
    // {
86
    //     $temp = [];
87
    //     foreach ($ipArray as $ip) {
88
    //         array_push($temp, ltrim($this->sendRes($ip)));
89
    //     }
90
    //     return $temp;
91
    // }
92
}
93