Completed
Push — master ( 89c3d8...a5c828 )
by David
9s
created

ExpressionResponseMatcher::getExpressionLanguage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 3.0416
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\ResponseMatcher;
13
14
use Sensio\Bundle\FrameworkExtraBundle\Security\ExpressionLanguage as SecurityExpressionLanguage;
15
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class ExpressionResponseMatcher implements ResponseMatcherInterface
19
{
20
    /**
21
     * @var ExpressionLanguage
22
     */
23
    private $expressionLanguage;
24
25
    /**
26
     * @var string
27
     */
28
    private $expression;
29
30 2
    public function __construct($expression, ExpressionLanguage $expressionLanguage = null)
31
    {
32 2
        $this->expression = $expression;
33 2
        $this->expressionLanguage = $expressionLanguage;
34 2
    }
35
36 2
    public function matches(Response $response)
37
    {
38 2
        return $this->getExpressionLanguage()->evaluate(
39 2
            $this->expression,
40 2
            ['response' => $response]
41
        );
42
    }
43
44 2
    private function getExpressionLanguage()
45
    {
46 2
        if (!$this->expressionLanguage) {
47 1
            $this->expressionLanguage = class_exists(SecurityExpressionLanguage::class)
48 1
                ? new SecurityExpressionLanguage()
49
                : new ExpressionLanguage()
50
            ;
51
        }
52
53 2
        return $this->expressionLanguage;
54
    }
55
}
56