Completed
Pull Request — master (#150)
by
unknown
06:02
created

IpAddress::isValid()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace PragmaRX\Firewall\Support;
4
5
use PragmaRX\Support\IpAddress as SupportIpAddress;
6
7
class IpAddress
8
{
9
    use ServiceInstances;
10
11
    /**
12
     * Check if IP address is valid.
13
     *
14
     * @param $ip
15
     *
16
     * @return bool
17
     */
18 57
    public function isValid($ip)
19
    {
20 57
        if (!$result = SupportIpAddress::ipV4Valid($ip = $this->hostToIp($ip)) ||
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $result = (\PragmaRX\Sup...s()->validCountry($ip)), Probably Intended Meaning: ($result = \PragmaRX\Sup...es()->validCountry($ip)
Loading history...
21 57
            $this->countries()->validCountry($ip)) {
22 10
            $this->messages()->addMessage(sprintf('%s is not a valid IP address', $ip));
23
        }
24
25 57
        return $result;
26
    }
27
28
    /**
29
     * Get the ip address of a host.
30
     *
31
     * @param $ip
32
     *
33
     * @return string
34
     */
35 57
    public function hostToIp($ip)
36
    {
37 57
        if (is_string($ip) && starts_with($ip, $string = 'host:')) {
0 ignored issues
show
Deprecated Code introduced by
The function starts_with() has been deprecated with message: Str::startsWith() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
38 2
            return gethostbyname(str_replace($string, '', $ip));
39
        }
40
41 55
        return $ip;
42
    }
43
44
    /**
45
     * Check if IP is in a valid range.
46
     *
47
     * @param $ip_address
48
     * @param $range
49
     *
50
     * @return bool
51
     */
52 16
    public function validRange($ip_address, $range)
53
    {
54 16
        return $this->config()->get('enable_range_search') &&
55 16
            SupportIpAddress::ipV4Valid($range->ip_address) &&
56 16
            SupportIpAddress::ipv4InRange($ip_address, $range->ip_address);
57
    }
58
59
    /**
60
     * Check if an ip v4 is valid.
61
     *
62
     * @param $item
63
     *
64
     * @return bool
65
     */
66 22
    public function ipV4Valid($item)
67
    {
68 22
        if (realpath($item) !== false) {
69
            return false;
70
        }
71
72 22
        return SupportIpAddress::ipV4Valid($item);
73
    }
74
75
    /**
76
     * Check if a string is a CIDR.
77
     *
78
     * @param $country
79
     *
80
     * @return bool|array
81
     */
82 27
    public function isCidr($country)
83
    {
84 27
        return SupportIpAddress::isCidr($country);
85
    }
86
}
87