Completed
Push — master ( 586166...fecb59 )
by Paweł
47:58
created

RuleEvaluator::evaluate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.864

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 2
cts 5
cp 0.4
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2.864
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Rule Component.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\Rule\Evaluator;
16
17
use Psr\Log\LoggerInterface;
18
use SWP\Component\Rule\Model\RuleInterface;
19
use SWP\Component\Rule\Model\RuleSubjectInterface;
20
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
21
22
final class RuleEvaluator implements RuleEvaluatorInterface
23
{
24
    /**
25
     * @var LoggerInterface
26
     */
27
    private $logger;
28
29
    /**
30
     * @var ExpressionLanguage
31
     */
32
    private $expression;
33
34
    /**
35
     * RuleEvaluator constructor.
36
     */
37 11
    public function __construct(LoggerInterface $logger)
38
    {
39 11
        $this->logger = $logger;
40 11
        $this->expression = new ExpressionLanguage();
41 11
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function evaluate(RuleInterface $rule, RuleSubjectInterface $subject)
47
    {
48
        try {
49 3
            return $this->expression->evaluate($rule->getValue(), [$subject->getSubjectType() => $subject]);
50
        } catch (\Exception $e) {
51
            $this->logger->warning($e->getMessage());
52
        }
53
54
        return false;
55
    }
56
}
57