Completed
Pull Request — develop (#523)
by Bastian
14:39 queued 10:07
created

SameSubnetStrategy   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 39
ccs 3
cts 9
cp 0.3333
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 8 2
A stopPropagation() 0 4 1
1
<?php
2
/**
3
 * strategy for validating auth through the ip address.
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Symfony\Component\HttpFoundation\IpUtils;
9
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Class SameSubnetStrategy
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class SameSubnetStrategy extends AbstractHttpStrategy
19
{
20
    /** @var String */
21
    protected $subnet;
22
23
    /**
24
     * @param String $subnet Subnet to be checked (e.g. 10.2.0.0/24)
25
     */
26 4
    public function __construct($subnet)
27
    {
28 4
        $this->subnet= $subnet;
29 4
    }
30
31
    /**
32
     * Applies the defined strategy on the provided request.
33
     *
34
     * @param Request $request request to handle
35
     *
36
     * @return string
37
     */
38
    public function apply(Request $request)
39
    {
40
        if (IpUtils::checkIp($request->getClientIp(), $this->subnet)) {
41
            return 'graviton_subnet_user';
42
        }
43
44
        throw new \InvalidArgumentException('Provided request information are not valid.');
45
    }
46
47
    /**
48
     * Decider to stop other strategies running after from being considered.
49
     *
50
     * @return boolean
51
     */
52
    public function stopPropagation()
53
    {
54
        return false;
55
    }
56
}
57