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

SameSubnetStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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