IpWhitelist::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
The MIT License (MIT)
5
6
Copyright (c) 2015 Vectorface, Inc.
7
8
Permission is hereby granted, free of charge, to any person obtaining a copy
9
of this software and associated documentation files (the "Software"), to deal
10
in the Software without restriction, including without limitation the rights
11
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
copies of the Software, and to permit persons to whom the Software is
13
furnished to do so, subject to the following conditions:
14
15
The above copyright notice and this permission notice shall be included in
16
all copies or substantial portions of the Software.
17
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
THE SOFTWARE.
25
*/
26
27
namespace Vectorface\Whip\IpRange;
28
29
/**
30
 * A class representing the list of whitelisted IP addresses.
31
 * @copyright Vectorface, Inc 2015
32
 * @author Daniel Bruce <[email protected]>
33
 */
34
class IpWhitelist
35
{
36
    /** The whitelist key for IPv4 addresses */
37
    const IPV4 = 'ipv4';
38
39
    /** The whitelist key for IPv6 addresses */
40
    const IPV6 = 'ipv6';
41
42
    /** an array of Ipv4Range items */
43
    private array $ipv4Whitelist;
44
45
    /** an array of Ipv6Range items */
46
    private array $ipv6Whitelist;
47
48
    /**
49
     * Constructor for the class.
50
     * @param array $whitelists An array with two keys ('ipv4' and 'ipv6') with
51
     *        each key mapping to an array of valid IP ranges.
52
     */
53 12
    public function __construct(array $whitelists)
54
    {
55 12
        $this->ipv4Whitelist = $this->constructWhiteListForKey(
56 12
            $whitelists,
57 12
            self::IPV4,
58 12
            Ipv4Range::class
59
        );
60 12
        $this->ipv6Whitelist = $this->constructWhiteListForKey(
61 12
            $whitelists,
62 12
            self::IPV6,
63 12
            Ipv6Range::class
64
        );
65 12
    }
66
67
    /**
68
     * Returns whether the given IP address is within the whitelist.
69
     *
70
     * @param string $ipAddress A valid IPv4 or IPv6 address.
71
     * @return bool Returns true if the IP address matches one of the
72
     *         whitelisted IP ranges and false otherwise.
73 12
     */
74
    public function isIpWhitelisted(string $ipAddress) : bool
75
    {
76 12
        // determine whether this IP is IPv4 or IPv6
77 12
        $isIpv4Address = filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
78 12
        return $this->isIpInWhitelist(
79 12
            ($isIpv4Address) ? $this->ipv4Whitelist : $this->ipv6Whitelist,
80
            $ipAddress
81
        );
82
    }
83
84
    /**
85
     * Constructs the whitelist for the given key. Each element in the
86
     * whitelist gets mapped from a string to an instance of an Ipv4Range or
87
     * Ipv6Range.
88
     *
89
     * @param array $whitelist The input whitelist of ranges.
90
     * @param string $key The key to use from the input whitelist ('ipv4' or
91
     *        'ipv6').
92
     * @param string $class Each range string gets mapped to an instance of the
93
     *        specified $class.
94 12
     * @return array Returns an array of Ipv4Range or Ipv6Range elements.
95
     */
96 12
    private function constructWhiteListForKey(array $whitelist, string $key, string $class) : array
97 12
    {
98 12
        if (!isset($whitelist[$key]) || !is_array($whitelist[$key])) {
99 12
            return [];
100
        }
101 6
        return array_map(function ($range) use ($class) {
102
            return new $class($range);
103
        }, array_values($whitelist[$key]));
104
    }
105
106
    /**
107
     * Returns whether the given IP address is in the given whitelist.
108
     *
109
     * @param array $whitelist The given whitelist.
110
     * @param string $ipAddress The given IP address.
111
     * @return bool Returns true if the IP address is in the whitelist and
112 12
     *         false otherwise.
113
     */
114 12
    private function isIpInWhitelist(array $whitelist, string $ipAddress) : bool
115 10
    {
116 10
        foreach ($whitelist as $ipRange) {
117
            if ($ipRange->containsIp($ipAddress)) {
118
                return true;
119 4
            }
120
        }
121
        return false;
122
    }
123
}
124