Passed
Branch ipv4range-test (42ce2e)
by Sebastian
03:15
created

IPv4Range::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Filter\Rules;
13
14
use InvalidArgumentException;
15
16
/**
17
 * Check if provided ipv4 is in CIDR range.
18
 *
19
 */
20
class IPv4Range extends Ip implements RuleValidateInterface
21
{
22
    /**
23
     * @var array Rule properties
24
     */
25
    public static $config = [
26
        'class' => 'Ip',
27
        'full_class' => __CLASS__,
28
        'alias' => ['ipv4range', 'ip4r'],
29
        'args_count' => 1,
30
        'args_type' => [],
31
        'has_validate' => true
32
    ];
33
34
    /**
35
     * @var string Error message
36
     */
37
    private $message = '';
38
39
    /**
40
     * Validate.
41
     *
42
     * @return bool
43
     */
44 56
    public function validate(): bool
45
    {
46 56
        $args = func_get_args();
47
48 56
        return $this->concreteValidate($args[0], $args[1]);
49
    }
50
51
    /**
52
     * Concrete validate.
53
     *
54
     * @param string $received
55
     * @param string $range
56
     *
57
     * @return bool
58
     */
59 56
    private function concreteValidate(string $received, string $range): bool
60
    {
61 56
        [$address, $bitSuffix] = $this->getRange($range);
62
63 46
        if (parent::validate($received)) {
64 1
            $this->message = 'Received value is not a valid ip address';
65 1
            return true;
66
        }
67
68 45
        $decimalWildcard = pow(2, (32 - $bitSuffix)) - 1;
69 45
        $decimalBits = ~ $decimalWildcard;
70
71 45
        $result = ((ip2long($received) & $decimalBits) === (ip2long($address) & $decimalBits));
72
73 45
        if ($result) {
74 40
            return false;
75
        }
76
77 5
        $this->message = "Received ip is not in ({$range}) range";
78 5
        return true;
79
    }
80
81
    /**
82
     * Get ip and bits for subnet mask.
83
     *
84
     * @param string $range
85
     *
86
     * @return array
87
     *
88
     * @throws InvalidArgumentException If range is not in valid format
89
     */
90 56
    private function getRange(string $range): array
91
    {
92 56
        $cidr = explode('/', $range, 2);
93
94 56
        $address = $cidr[0];
95 56
        $bitSuffix = $cidr[1] ?? null;
96
97 56
        if (empty($bitSuffix)) {
98 3
            throw new InvalidArgumentException('Range must be in valid IP/CIDR format, empty bits for suffix.');
99
        }
100
101 53
        if ((int) $bitSuffix < 1 || (int) $bitSuffix > 32) {
102 6
            throw new InvalidArgumentException('Range must be in valid IP/CIDR format, invalid bits suffix range.');
103
        }
104
105 47
        if (parent::validate($address)) {
106 1
            throw new InvalidArgumentException('Range must be in valid IP/CIDR format, invalid address.');
107
        }
108
109 46
        return [$address, $bitSuffix];
110
    }
111
112
    /**
113
     * Return error message.
114
     *
115
     * @return string Error message
116
     */
117 2
    public function getMessage(): string
118
    {
119 2
        return $this->message;
120
    }
121
}
122