|
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\Voter\IVotingSubject; |
|
10
|
|
|
use SpareParts\Overseer\VotingDecisionEnum; |
|
11
|
|
|
use SpareParts\Overseer\VotingResult; |
|
12
|
|
|
|
|
13
|
|
|
class VotingAssembly implements IVotingAssembly |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $strategy; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var IVoter[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $voters; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $subjectName; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $actionName; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* VotingAssembly constructor. |
|
39
|
|
|
* @param string $subjectName |
|
40
|
|
|
* @param string $actionName |
|
41
|
|
|
* @param string $strategy |
|
42
|
|
|
* @param \SpareParts\Overseer\Voter\IVoter[] $voters |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
$subjectName, |
|
46
|
|
|
$actionName, |
|
47
|
|
|
$strategy, |
|
48
|
|
|
array $voters |
|
49
|
|
|
) { |
|
50
|
4 |
|
$this->strategy = $strategy; |
|
51
|
|
|
$this->voters = $voters; |
|
52
|
|
|
$this->subjectName = $subjectName; |
|
53
|
|
|
$this->actionName = $actionName; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
4 |
|
/** |
|
58
|
4 |
|
* @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
|
59
|
4 |
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
60
|
4 |
|
* @return null|\SpareParts\Overseer\IVotingResult |
|
61
|
4 |
|
* @throws \SpareParts\Overseer\InvalidVotingResultException |
|
62
|
4 |
|
*/ |
|
63
|
|
|
public function commenceVote(IVotingSubject $votingSubject, IVotingContext $votingContext) |
|
64
|
|
|
{ |
|
65
|
|
|
$result = null; |
|
|
|
|
|
|
66
|
|
|
switch ($this->strategy) { |
|
67
|
|
|
case StrategyEnum::FIRST_VOTE_DECIDES(): |
|
68
|
|
|
return $this->strategyFirstVoteDecides($votingSubject, $votingContext); |
|
69
|
|
|
|
|
70
|
|
|
case StrategyEnum::ALLOW_UNLESS_DENIED(): |
|
71
|
|
|
return $this->strategyAllowUnlessDenied($votingSubject, $votingContext); |
|
72
|
|
|
|
|
73
|
|
|
case StrategyEnum::DENY_UNLESS_ALLOWED(): |
|
74
|
|
|
return $this->strategyDenyUnlessAllowed($votingSubject, $votingContext); |
|
75
|
|
|
|
|
76
|
|
|
default: |
|
77
|
|
|
throw new InvalidVotingResultException('Unable to decide on result, invalid strategy: '.$this->strategy); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
|
84
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
85
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
|
86
|
|
|
* @throws \SpareParts\Overseer\InvalidVotingResultException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function strategyFirstVoteDecides(IVotingSubject $votingSubject, IVotingContext $votingContext) |
|
89
|
|
|
{ |
|
90
|
|
|
foreach ($this->voters as $voter) { |
|
91
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
|
92
|
|
|
return new VotingResult($lastResult->getDecision(), [$lastResult]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
throw new InvalidVotingResultException('Voting assembly did not decide on any result!'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
|
101
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
102
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
|
103
|
|
|
*/ |
|
104
|
|
View Code Duplication |
private function strategyAllowUnlessDenied($votingSubject, $votingContext) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$results = []; |
|
107
|
|
|
foreach ($this->voters as $name => $voter) { |
|
108
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
|
109
|
|
|
$results[] = $lastResult; |
|
110
|
|
|
if ($lastResult->getDecision() == VotingDecisionEnum::DENIED()) { |
|
111
|
|
|
return new VotingResult(VotingDecisionEnum::DENIED(), $results); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
return new VotingResult(VotingDecisionEnum::ALLOWED(), $results); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
|
121
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
122
|
|
|
* @return \SpareParts\Overseer\IVotingResult |
|
123
|
|
|
*/ |
|
124
|
|
View Code Duplication |
private function strategyDenyUnlessAllowed($votingSubject, $votingContext) |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
$results = []; |
|
127
|
|
|
foreach ($this->voters as $name => $voter) { |
|
128
|
|
|
if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
|
129
|
|
|
$results[] = $lastResult; |
|
130
|
|
|
if ($lastResult->getDecision() == VotingDecisionEnum::DENIED()) { |
|
131
|
|
|
return new VotingResult(VotingDecisionEnum::ALLOWED(), $results); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
return new VotingResult(VotingDecisionEnum::DENIED(), $results); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $actionName |
|
141
|
|
|
* @param \SpareParts\Overseer\Voter\IVotingSubject $subject |
|
142
|
|
|
* @param \SpareParts\Overseer\Context\IVotingContext $context |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function canVoteOn($actionName, IVotingSubject $subject, IVotingContext $context) |
|
146
|
|
|
{ |
|
147
|
|
|
if ($subject->getVotingSubjectName() === $this->subjectName && $actionName === $this->actionName) { |
|
148
|
|
|
return true; |
|
149
|
|
|
} |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
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.