RequestConstraint::matches()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 8.0555
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 9
nc 5
nop 1
crap 9
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Security\Http;
12
13
use Symfony\Component\HttpFoundation\IpUtils;
14
15
/**
16
 * RequestConstraint holds a set of request requirements:
17
 * - path pattern
18
 * - host pattern
19
 * - http methods array
20
 * - ip addresses array.
21
 *
22
 * @author Yaroslav Honcharuk <[email protected]>
23
 */
24
class RequestConstraint implements RequestConstraintInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    private $pathPattern;
30
31
    /**
32
     * @var string
33
     */
34
    private $hostPattern;
35
36
    /**
37
     * @var array
38
     */
39
    private $methods;
40
41
    /**
42
     * @var array
43
     */
44
    private $ips;
45
46
    /**
47
     * @param string|null   $pathPattern
48
     * @param string|null   $hostPattern
49
     * @param string[]|null $methods
50
     * @param string[]|null $ips
51
     */
52 73
    public function __construct($pathPattern = null, $hostPattern = null, array $methods = null, array $ips = null)
53
    {
54 73
        if (null !== $methods) {
55 29
            $methods = array_map('strtoupper', $methods);
56
        }
57
58 73
        $this->pathPattern = $pathPattern;
59 73
        $this->hostPattern = $hostPattern;
60 73
        $this->methods = $methods;
61 73
        $this->ips = $ips;
62 73
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 59
    public function getPathPattern()
68
    {
69 59
        return $this->pathPattern;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 60
    public function getHostPattern()
76
    {
77 60
        return $this->hostPattern;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 63
    public function getMethods()
84
    {
85 63
        return $this->methods;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 61
    public function getIps()
92
    {
93 61
        return $this->ips;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 16
    public function matches(RequestContext $requestContext)
100
    {
101 16
        if ($this->getMethods() && !in_array($requestContext->getMethod(), $this->getMethods(), true)) {
102 2
            return false;
103
        }
104
105 14
        if ($this->getIps() && !IpUtils::checkIp($requestContext->getClientIp(), $this->getIps())) {
106 1
            return false;
107
        }
108
109 13
        if (null !== $this->getHostPattern() && !preg_match('{'.$this->getHostPattern().'}i', $requestContext->getHost())) {
110 1
            return false;
111
        }
112
113 12
        if (null !== $this->getPathPattern() && !preg_match('{'.$this->getPathPattern().'}', rawurldecode($requestContext->getPathInfo()))) {
114 5
            return false;
115
        }
116
117 10
        return true;
118
    }
119
}
120