Completed
Push — master ( e64a87...c98c99 )
by
unknown
07:13
created

matchRequestPathAgainstBlacklist()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
crap 3
1
<?php
2
3
namespace MediaMonks\RestApi\Request;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\HttpKernelInterface;
7
8
class RegexRequestMatcher extends AbstractRequestMatcher
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $whitelist = [];
14
15
    /**
16
     * @var array
17
     */
18
    protected $blacklist = [];
19
20
    /**
21
     * @param array $whitelist
22
     * @param array $blacklist
23
     */
24 8
    public function __construct(array $whitelist = [], array $blacklist = [])
25
    {
26 8
        $this->whitelist = $whitelist;
27 8
        $this->blacklist = $blacklist;
28 8
    }
29
30
    /**
31
     * @param Request $request
32
     * @param int $requestType
33
     * @return bool
34
     */
35 7
    public function matches(Request $request, $requestType = HttpKernelInterface::MASTER_REQUEST)
36
    {
37 7
        if ($requestType !== HttpKernelInterface::MASTER_REQUEST) {
38 3
            return false;
39
        }
40
41 7
        if ($this->matchPreviouslyMatchedRequest($request)) {
42 2
            return true;
43
        }
44
45 7
        if (!$this->matchRequestPathAgainstLists($request->getPathInfo())) {
46 5
            return false;
47
        }
48
49 4
        $this->markRequestAsMatched($request);
50
51 4
        return true;
52
    }
53
54
    /**
55
     * @param Request $request
56
     */
57 4
    protected function markRequestAsMatched(Request $request)
58
    {
59 4
        $request->attributes->set(self::ATTRIBUTE_MATCHED, true);
60 4
    }
61
62
    /**
63
     * @param Request $request
64
     * @return bool
65
     */
66 7
    protected function matchPreviouslyMatchedRequest(Request $request)
67
    {
68 7
        return $request->attributes->getBoolean(self::ATTRIBUTE_MATCHED);
69
    }
70
71
    /**
72
     * @param $requestPath
73
     * @return bool
74
     */
75 7
    protected function matchRequestPathAgainstLists($requestPath)
76
    {
77 7
        if ($this->matchRequestPathAgainstBlacklist($requestPath)) {
78 1
            return false;
79
        }
80 7
        if ($this->matchRequestPathAgainstWhitelist($requestPath)) {
81 4
            return true;
82
        }
83
84 5
        return false;
85
    }
86
87
    /**
88
     * @param $requestPath
89
     * @return bool
90
     */
91 7
    protected function matchRequestPathAgainstBlacklist($requestPath)
92
    {
93 7
        foreach ($this->blacklist as $regex) {
94 1
            if (preg_match($regex, $requestPath)) {
95 1
                return true;
96
            }
97 7
        }
98 7
    }
99
100
    /**
101
     * @param $requestPath
102
     * @return bool
103
     */
104 7
    protected function matchRequestPathAgainstWhitelist($requestPath)
105
    {
106 7
        foreach ($this->whitelist as $regex) {
107 6
            if (preg_match($regex, $requestPath)) {
108 4
                return true;
109
            }
110 5
        }
111 5
    }
112
}
113