|
1
|
|
|
<?php |
|
2
|
|
|
namespace SpareParts\Overseer\Assembly; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use SpareParts\Overseer\Context\IVotingContext; |
|
6
|
|
|
use SpareParts\Overseer\InvalidVotingResultException; |
|
7
|
|
|
use SpareParts\Overseer\StrategyEnum; |
|
8
|
|
|
use SpareParts\Overseer\Voter\IVoter; |
|
9
|
|
|
use SpareParts\Overseer\VotingDecisionEnum; |
|
10
|
|
|
use SpareParts\Overseer\VotingResult; |
|
11
|
|
|
|
|
12
|
|
|
class VotingAssembly implements IVotingAssembly |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var StrategyEnum |
|
17
|
|
|
*/ |
|
18
|
|
|
private $strategy; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var IVoter[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $voters; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* VotingAssembly constructor. |
|
28
|
|
|
* @param StrategyEnum $strategy |
|
29
|
|
|
* @param \SpareParts\Overseer\Voter\IVoter[] $voters |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(StrategyEnum $strategy, array $voters) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->strategy = $strategy; |
|
34
|
|
|
$this->voters = $voters; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param mixed $votingSubject |
|
40
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
41
|
|
|
* @return null|\SpareParts\Overseer\IVotingResult |
|
42
|
|
|
* @throws \SpareParts\Overseer\InvalidVotingResultException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function commenceVote($votingSubject, IVotingContext $votingContext) |
|
45
|
|
|
{ |
|
46
|
|
|
$result = null; |
|
|
|
|
|
|
47
|
|
|
switch ($this->strategy) { |
|
48
|
|
|
case StrategyEnum::FIRST_VOTE_DECIDES(): |
|
49
|
|
|
return $this->strategyFirstVoteDecides($votingSubject, $votingContext); |
|
50
|
4 |
|
|
|
51
|
|
|
case StrategyEnum::ALLOW_UNLESS_DENIED(): |
|
52
|
|
|
return $this->strategyAllowUnlessDenied($votingSubject, $votingContext); |
|
53
|
|
|
|
|
54
|
|
|
case StrategyEnum::DENY_UNLESS_ALLOWED(): |
|
55
|
|
|
return $this->strategyDenyUnlessAllowed($votingSubject, $votingContext); |
|
56
|
|
|
|
|
57
|
4 |
|
default: |
|
58
|
4 |
|
throw new InvalidVotingResultException('Unable to decide on result, invalid strategy: '.$this->strategy); |
|
59
|
4 |
|
} |
|
60
|
4 |
|
} |
|
61
|
4 |
|
|
|
62
|
4 |
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param mixed $votingSubject |
|
65
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
66
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
|
67
|
|
|
* @throws \SpareParts\Overseer\InvalidVotingResultException |
|
68
|
|
|
*/ |
|
69
|
|
|
private function strategyFirstVoteDecides($votingSubject, IVotingContext $votingContext) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($this->voters as $voter) { |
|
72
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
|
73
|
|
|
return new VotingResult($lastResult->getDecision(), [$lastResult]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
throw new InvalidVotingResultException('Voting assembly did not decide on any result!'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param mixed $votingSubject |
|
82
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
83
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
|
84
|
|
|
*/ |
|
85
|
|
View Code Duplication |
private function strategyAllowUnlessDenied($votingSubject, IVotingContext $votingContext) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$results = []; |
|
88
|
|
|
foreach ($this->voters as $name => $voter) { |
|
89
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
|
90
|
|
|
$results[] = $lastResult; |
|
91
|
|
|
if ($lastResult->getDecision() === VotingDecisionEnum::DENIED()) { |
|
92
|
|
|
return new VotingResult(VotingDecisionEnum::DENIED(), $results); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
return new VotingResult(VotingDecisionEnum::ALLOWED(), $results); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param mixed $votingSubject |
|
102
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
103
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
|
104
|
|
|
*/ |
|
105
|
|
View Code Duplication |
private function strategyDenyUnlessAllowed($votingSubject, IVotingContext $votingContext) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$results = []; |
|
108
|
|
|
foreach ($this->voters as $name => $voter) { |
|
109
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
|
110
|
|
|
$results[] = $lastResult; |
|
111
|
|
|
if ($lastResult->getDecision() === VotingDecisionEnum::ALLOWED()) { |
|
112
|
|
|
return new VotingResult(VotingDecisionEnum::ALLOWED(), $results); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return new VotingResult(VotingDecisionEnum::DENIED(), $results); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.