Completed
Push — master ( d2f8aa...3f97e5 )
by David de
66:53 queued 64:48
created

RuleMatcher::getExpressionLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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\Http;
13
14
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * This matcher wraps a Symfony2 RequestMatcher and adds some criteria for the
21
 * response.
22
 *
23
 * @author David Buchmann <[email protected]>
24
 */
25
class RuleMatcher implements RuleMatcherInterface
26
{
27
    /**
28
     * @var RequestMatcherInterface
29
     */
30
    private $requestMatcher;
31
32
    /**
33
     * @var array
34
     */
35
    private $criteria;
36
37
    /**
38
     * @var ExpressionLanguage
39
     */
40
    private $expressionLanguage;
41
42
    /**
43
     * @param RequestMatcherInterface $requestMatcher Strategy to match the request
44
     * @param array                   $criteria       Additional criteria not covered by request matcher
45
     */
46 27
    public function __construct(RequestMatcherInterface $requestMatcher, array $criteria)
47
    {
48 27
        $this->requestMatcher = $requestMatcher;
49 27
        $this->criteria = $criteria;
50 27
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 19
    public function matches(Request $request, Response $response)
56
    {
57 19
        if (!$this->requestMatcher->matches($request)) {
58 15
            return false;
59
        }
60
61 10
        if (!empty($this->criteria['match_response'])) {
62 1
            if (!$this->getExpressionLanguage()->evaluate($this->criteria['match_response'], [
63 1
                'response' => $response,
64
            ])) {
65 1
                return false;
66
            }
67
        } else {
68
            /* We can't use Response::isCacheable because that also checks if cache
69
             * headers are already set. As we are about to set them, that would
70
             * always return false.
71
             */
72 9
            $status = [200, 203, 204, 300, 301, 302, 404, 410];
73 9
            if (!empty($this->criteria['additional_cacheable_status'])) {
74 1
                $status = array_merge($status, $this->criteria['additional_cacheable_status']);
75
            }
76 9
            if (!in_array($response->getStatusCode(), $status)) {
77 1
                return false;
78
            }
79
        }
80
81 10
        return true;
82
    }
83
84
    /**
85
     * @return ExpressionLanguage
86
     */
87 1
    private function getExpressionLanguage()
88
    {
89 1
        if (!$this->expressionLanguage) {
90 1
            $this->expressionLanguage = new ExpressionLanguage();
91
        }
92
93 1
        return $this->expressionLanguage;
94
    }
95
}
96