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\ISingleVoterResult; |
9
|
|
|
use SpareParts\Overseer\Voter\IVoter; |
10
|
|
|
use SpareParts\Overseer\VotingDecisionEnum; |
11
|
|
|
use SpareParts\Overseer\VotingResult; |
12
|
|
|
|
13
|
|
|
class VotingAssembly implements IVotingAssembly |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var StrategyEnum |
18
|
|
|
*/ |
19
|
|
|
private $strategy; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var IVoter[] |
23
|
|
|
*/ |
24
|
|
|
private $voters; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* VotingAssembly constructor. |
29
|
|
|
* @param StrategyEnum $strategy |
30
|
|
|
* @param \SpareParts\Overseer\Voter\IVoter[] $voters |
31
|
|
|
*/ |
32
|
|
|
public function __construct(StrategyEnum $strategy, array $voters) |
33
|
|
|
{ |
34
|
|
|
$this->strategy = $strategy; |
35
|
|
|
$this->voters = $voters; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $votingSubject |
41
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
42
|
|
|
* @return null|\SpareParts\Overseer\IVotingResult |
43
|
|
|
* @throws \SpareParts\Overseer\InvalidVotingResultException |
44
|
|
|
*/ |
45
|
|
|
public function commenceVote($votingSubject, IVotingContext $votingContext) |
46
|
|
|
{ |
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 |
|
case StrategyEnum::EVERYONE_MUST_ALLOW_TO_BE_ALLOWED(): |
58
|
4 |
|
return $this->strategyEveryoneMustComply($votingSubject, $votingContext, VotingDecisionEnum::ALLOWED(), VotingDecisionEnum::DENIED()); |
59
|
4 |
|
|
60
|
4 |
|
case StrategyEnum::EVERYONE_MUST_DENY_TO_BE_DENIED(): |
61
|
4 |
|
return $this->strategyEveryoneMustComply($votingSubject, $votingContext, VotingDecisionEnum::DENIED(), VotingDecisionEnum::ALLOWED()); |
62
|
4 |
|
|
63
|
|
|
default: |
64
|
|
|
throw new InvalidVotingResultException('Unable to decide on result, invalid strategy: '.$this->strategy); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $votingSubject |
71
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
72
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
73
|
|
|
* @throws \SpareParts\Overseer\InvalidVotingResultException |
74
|
|
|
*/ |
75
|
|
|
private function strategyFirstVoteDecides($votingSubject, IVotingContext $votingContext) |
76
|
|
|
{ |
77
|
|
|
foreach ($this->voters as $voter) { |
78
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
79
|
|
|
return new VotingResult($lastResult->getDecision(), [$lastResult]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
throw new InvalidVotingResultException('Voting assembly did not decide on any result!'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $votingSubject |
88
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
89
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
private function strategyAllowUnlessDenied($votingSubject, IVotingContext $votingContext) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$results = []; |
94
|
|
|
foreach ($this->voters as $name => $voter) { |
95
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
96
|
|
|
$results[] = $lastResult; |
97
|
|
|
if ($lastResult->getDecision() === VotingDecisionEnum::DENIED()) { |
98
|
|
|
return new VotingResult(VotingDecisionEnum::DENIED(), $results); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return new VotingResult(VotingDecisionEnum::ALLOWED(), $results); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $votingSubject |
108
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
109
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
private function strategyDenyUnlessAllowed($votingSubject, IVotingContext $votingContext) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$results = []; |
114
|
|
|
foreach ($this->voters as $name => $voter) { |
115
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
116
|
|
|
$results[] = $lastResult; |
117
|
|
|
if ($lastResult->getDecision() === VotingDecisionEnum::ALLOWED()) { |
118
|
|
|
return new VotingResult(VotingDecisionEnum::ALLOWED(), $results); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
return new VotingResult(VotingDecisionEnum::DENIED(), $results); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param mixed $votingSubject |
128
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
129
|
|
|
* @param \SpareParts\Overseer\VotingDecisionEnum $defaultDecision |
130
|
|
|
* @param \SpareParts\Overseer\VotingDecisionEnum $counterDecision |
131
|
|
|
* |
132
|
|
|
* @return \SpareParts\Overseer\VotingResult |
133
|
|
|
*/ |
134
|
|
|
private function strategyEveryoneMustComply( |
135
|
|
|
$votingSubject, |
136
|
|
|
IVotingContext $votingContext, |
137
|
|
|
VotingDecisionEnum $defaultDecision, |
138
|
|
|
VotingDecisionEnum $counterDecision |
139
|
|
|
) { |
140
|
|
|
$results = []; |
141
|
|
|
$decision = $defaultDecision; |
142
|
|
|
foreach ($this->voters as $voter) { |
143
|
|
|
$result = $voter->vote($votingSubject, $votingContext); |
144
|
|
|
if ($result instanceof ISingleVoterResult) { |
145
|
|
|
if ($result->getDecision() !== $defaultDecision) { |
146
|
|
|
$decision = $counterDecision; |
147
|
|
|
} |
148
|
|
|
$results[] = $result; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
return new VotingResult($decision, $results); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.