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

AbstractVotingManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 40
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A innerVote() 0 14 4
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