Completed
Push — master ( af3ede...2a1d1e )
by Ondrej
01:47
created

AbstractVotingManager::innerVote()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

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