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

RuleProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
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\Processor;
16
17
use SWP\Component\Rule\Applicator\RuleApplicatorInterface;
18
use SWP\Component\Rule\Evaluator\RuleEvaluatorInterface;
19
use SWP\Component\Rule\Model\RuleSubjectInterface;
20
use SWP\Component\Rule\Repository\RuleRepositoryInterface;
21
22
final class RuleProcessor implements RuleProcessorInterface
23
{
24
    /**
25
     * @var RuleRepositoryInterface
26
     */
27
    private $ruleRepository;
28
29
    /**
30
     * @var RuleEvaluatorInterface
31
     */
32
    private $ruleEvaluator;
33
34
    /**
35
     * @var RuleApplicatorInterface
36
     */
37
    private $ruleApplicator;
38
39
    /**
40
     * RuleProcessor constructor.
41
     *
42
     * @param RuleRepositoryInterface $ruleRepository
43
     * @param RuleEvaluatorInterface  $ruleEvaluator
44
     */
45 11
    public function __construct(
46
        RuleRepositoryInterface $ruleRepository,
47
        RuleEvaluatorInterface $ruleEvaluator,
48
        RuleApplicatorInterface $ruleApplicator
49
    ) {
50 11
        $this->ruleRepository = $ruleRepository;
51 11
        $this->ruleEvaluator = $ruleEvaluator;
52 11
        $this->ruleApplicator = $ruleApplicator;
53 11
    }
54
55 9
    public function process(RuleSubjectInterface $subject)
56
    {
57 9
        $rules = $this->ruleRepository->findBy([], ['priority' => 'desc']);
58
59 9
        foreach ($rules as $rule) {
60 3
            if (!$this->ruleEvaluator->evaluate($rule, $subject)) {
61 1
                continue;
62
            }
63
64 2
            $this->ruleApplicator->apply($rule, $subject);
65
        }
66 9
    }
67
}
68