Completed
Push — master ( df8989...bc4c56 )
by Jonathan
07:56
created

IpWhitelist   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
c 1
b 1
f 0
lcom 1
cbo 0
dl 0
loc 88
ccs 23
cts 23
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A constructWhiteListForKey() 0 10 3
A __construct() 0 13 1
A isIpWhitelisted() 0 9 2
A isIpInWhitelist() 0 9 3
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 $ipv4Whitelist;
44
45
    /** an array of Ipv6Range items */
46
    private $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
            $whitelists,
57 12
            self::IPV4,
58 12
            'Vectorface\\Whip\\IpRange\\Ipv4Range'
59
        );
60 12
        $this->ipv6Whitelist = $this->constructWhiteListForKey(
61
            $whitelists,
62 12
            self::IPV6,
63 12
            'Vectorface\\Whip\\IpRange\\Ipv6Range'
64
        );
65 12
    }
66
67
    /**
68
     * Returns whether or not the given IP address is within the whitelist.
69
     * @param string $ipAddress A valid IPv4 or IPv6 address.
70
     * @return boolean Returns true if the IP address matches one of the
71
     *         whitelisted IP ranges and false otherwise.
72
     */
73 12
    public function isIpWhitelisted($ipAddress)
74
    {
75
        // determine whether this IP is IPv4 or IPv6
76 12
        $isIpv4Address = filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
77 12
        return $this->isIpInWhitelist(
78 12
            ($isIpv4Address) ? $this->ipv4Whitelist : $this->ipv6Whitelist,
79
            $ipAddress
80
        );
81
    }
82
83
    /**
84
     * Constructs the whitelist for the given key. Each element in the
85
     * whitelist gets mapped from a string to an instance of an Ipv4Range or
86
     * Ipv6Range.
87
     * @param array $whitelist The input whitelist of ranges.
88
     * @param string $key The key to use from the input whitelist ('ipv4' or
89
     *        'ipv6').
90
     * @param string $class Each range string gets mapped to an instance of the
91
     *        specified $class.
92
     * @return array Returns an array of Ipv4Range or Ipv6Range elements.
93
     */
94 12
    private function constructWhiteListForKey(array $whitelist, $key, $class)
95
    {
96 12
        if (isset($whitelist[$key]) && is_array($whitelist[$key])) {
97 12
            return array_map(function ($range) use ($class) {
98 12
                return new $class($range);
99 12
            }, array_values($whitelist[$key]));
100
        } else {
101 6
            return array();
102
        }
103
    }
104
105
    /**
106
     * Returns whether or not the given IP address is in the given whitelist.
107
     * @param array $whitelist The given whitelist.
108
     * @param string $ipAddress The given IP address.
109
     * @return boolean Returns true if the IP address is in the whitelist and
110
     *         false otherwise.
111
     */
112 12
    private function isIpInWhitelist(array $whitelist, $ipAddress)
113
    {
114 12
        foreach ($whitelist as $ipRange) {
115 10
            if ($ipRange->containsIp($ipAddress)) {
116 10
                return true;
117
            }
118
        }
119 4
        return false;
120
    }
121
}
122