Range   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 31
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 4 1
A convert() 0 9 1
1
<?php
2
/**
3
 * IP range converter for a range
4
 *
5
 * E.g. 10.0.0.1-10.0.1.100
6
 *
7
 * PHP version 5.5
8
 *
9
 * @category   OpCacheGUI
10
 * @package    Network
11
 * @subpackage Ip
12
 * @author     Pieter Hordijk <[email protected]>
13
 * @copyright  Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa>
14
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
15
 * @version    1.0.0
16
 */
17
namespace OpCacheGUI\Network\Ip;
18
19
/**
20
 * IP range converter for a range
21
 *
22
 * @category   OpCacheGUI
23
 * @package    Network
24
 * @subpackage Ip
25
 * @author     Pieter Hordijk <[email protected]>
26
 */
27
class Range implements Converter
28
{
29
    /**
30
     * Checks whether is certain address is valid for the converter implementation
31
     *
32
     * @param string $address The address to check
33
     *
34
     * @return boolean True when the address is valid
35
     */
36 6
    public function isValid($address)
37
    {
38 6
        return !!preg_match('/^\d+\.\d+\.\d+\.\d+\s?-\s?\d+\.\d+\.\d+\.\d+$/', $address);
39
    }
40
41
    /**
42
     * Converts an IP address or range into a range to easily check for access
43
     *
44
     * @param string $address The IP address / range
45
     *
46
     * @return double[] Array containing the first and last ip in the range
47
     */
48 2
    public function convert($address)
49
    {
50 2
        $parts = explode('-', $address);
51
52
        return [
53 2
            (float) sprintf('%u', ip2long(trim($parts[0]))),
54 2
            (float) sprintf('%u', ip2long(trim($parts[1]))),
55
        ];
56
    }
57
}
58