AbstractVotingManager::innerVote()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4
1
<?php
2
namespace SpareParts\Overseer;
3
4
use SpareParts\Overseer\Assembly\IVotingAbilityAwareAssembly;
5
use SpareParts\Overseer\Context\IVotingContext;
6
7
abstract class AbstractVotingManager
8
{
9
    /**
10
     * @var IVotingAbilityAwareAssembly[]
11
     */
12
    private $votingAssemblies;
13
14
15
    /**
16
     * VotingManager constructor.
17
     * @param IVotingAbilityAwareAssembly[] $votingAssemblies
18
     */
19 7
    public function __construct(array $votingAssemblies)
20
    {
21 7
        $this->votingAssemblies = $votingAssemblies;
22 7
    }
23
24
25
    /**
26
     * @param string $action
27
     * @param mixed $votingSubject
28
     * @param \SpareParts\Overseer\Context\IVotingContext $votingContext
29
     * @return \SpareParts\Overseer\IVotingResult
30
     * @throws \SpareParts\Overseer\InvalidVotingResultException
31
     */
32 7
    protected function innerVote($action, $votingSubject, IVotingContext $votingContext)
33
    {
34 7
        foreach ($this->votingAssemblies as $votingAssembly) {
35 7
            if (!($votingAssembly instanceof IVotingAbilityAwareAssembly)) {
36 1
                throw new InvalidArgumentException('Voting assemblies provided to voting manager must implement IVotingAbilityAwareAssembly interface!');
37
            }
38
39 6
            if ($votingAssembly->canVoteOn($action, $votingSubject, $votingContext)) {
40 5
                return $votingAssembly->commenceVote($votingSubject, $votingContext);
41
            }
42
        }
43
44 1
        throw new InvalidVotingResultException('No voting assembly for subject::action: '.
45 1
            (string) $votingSubject.'::'.$action);
46
    }
47
}
48