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

RuleApplicatorChain   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 51
ccs 11
cts 16
cp 0.6875
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addApplicator() 0 4 1
A apply() 0 8 3
A isSupported() 0 10 3
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\Applicator;
16
17
use SWP\Component\Rule\Model\RuleInterface;
18
use SWP\Component\Rule\Model\RuleSubjectInterface;
19
20
final class RuleApplicatorChain implements RuleApplicatorInterface
21
{
22
    /**
23
     * @var RuleApplicatorInterface[] array
24
     */
25
    private $ruleApplicators = [];
26
27
    /**
28
     * RuleApplicatorChain constructor.
29
     *
30
     * @param array $ruleApplicators
31
     */
32 11
    public function __construct(array $ruleApplicators = [])
33
    {
34 11
        $this->ruleApplicators = $ruleApplicators;
35 11
    }
36
37
    /**
38
     * @param RuleApplicatorInterface $ruleApplicator
39
     */
40 11
    public function addApplicator(RuleApplicatorInterface $ruleApplicator)
41
    {
42 11
        $this->ruleApplicators[] = $ruleApplicator;
43 11
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function apply(RuleInterface $rule, RuleSubjectInterface $subject)
49
    {
50 2
        foreach ($this->ruleApplicators as $ruleApplicator) {
51 2
            if ($ruleApplicator->isSupported($subject)) {
52 2
                $ruleApplicator->apply($rule, $subject);
53
            }
54
        }
55 2
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function isSupported(RuleSubjectInterface $subject)
61
    {
62
        foreach ($this->ruleApplicators as $ruleApplicator) {
63
            if ($ruleApplicator->isSupported($subject)) {
64
                return true;
65
            }
66
        }
67
68
        return false;
69
    }
70
}
71