Completed
Push — master ( 9bb55b...536c73 )
by David de
11s
created

AbstractRuleListener::matchRule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCacheBundle\EventListener;
13
14
use FOS\HttpCacheBundle\Http\RuleMatcherInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
abstract class AbstractRuleListener
19
{
20
    /**
21
     * @var array List of arrays with RuleMatcher, settings array
22
     */
23
    private $rulesMap = array();
24
25
    /**
26
     * Add a rule matcher with a list of header directives to apply if the
27
     * request and response are matched.
28
     *
29
     * @param RuleMatcherInterface $ruleMatcher The headers apply to responses matched by this matcher
30
     * @param array                $settings    An array of header configuration
31
     */
32 27
    public function addRule(
33
        RuleMatcherInterface $ruleMatcher,
34
        array $settings = array()
35
    ) {
36 27
        $this->rulesMap[] = array($ruleMatcher, $settings);
37 27
    }
38
39
    /**
40
     * Return the settings for the current request if any rule matches.
41
     *
42
     * @param Request  $request
43
     * @param Response $response
44
     *
45
     * @return array|false Settings to apply or false if no rule matched
46
     */
47 22
    protected function matchRule(Request $request, Response $response)
48
    {
49 22
        foreach ($this->rulesMap as $elements) {
50 19
            if ($elements[0]->matches($request, $response)) {
51 19
                return $elements[1];
52
            }
53
        }
54
55 21
        return false;
56
    }
57
}
58