IPValidate::getType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
1
<?php
2
3
namespace Pamo\IPValidate;
4
5
/**
6
 * IP validator
7
 */
8
class IPValidate
9
{
10
    /**
11
     * @var string $ipV4 type.
12
     * @var string $ipV6 type.
13
     */
14
    private $ipV4 = FILTER_FLAG_IPV4;
15
    private $ipV6 = FILTER_FLAG_IPV6;
16
17
18
19
    /**
20
     * Valide an IP Address.
21
     *
22
     * @param string $ipAddress to validate.
23
     *
24
     * @return bool
25
     */
26 1
    public function isValid(string $ipAddress) : bool
27
    {
28 1
        return filter_var($ipAddress, FILTER_VALIDATE_IP);
29
    }
30
31
32
33
    /**
34
     * Get an IP Address type.
35
     *
36
     * @param string $ipAddress to get type for.
37
     *
38
     * @return string
39
     */
40 1
    public function getType(string $ipAddress) : string
41
    {
42 1
        $type = "";
43
44
        switch ($ipAddress) {
45 1
            case (filter_var($ipAddress, FILTER_VALIDATE_IP, $this->ipV4)):
46 1
                $type = "IPv4";
47 1
                break;
48 1
            case (filter_var($ipAddress, FILTER_VALIDATE_IP, $this->ipV6)):
49 1
                $type = "IPv6";
50 1
                break;
51
        }
52
53 1
        return $type;
54
    }
55
56
57
58
    /**
59
     * Get IP Address Domain name if exists and IP is valid.
60
     *
61
     * @param string $ipAddress IP Address to get the domain name for.
62
     *
63
     * @return string
64
     */
65 1
    public function getDomain(string $ipAddress) : string
66
    {
67 1
        $domain = gethostbyaddr($ipAddress);
68
69 1
        return ($domain != $ipAddress ? $domain : "unavailable");
70
    }
71
}
72