@@ -8,50 +8,50 @@ |
||
| 8 | 8 | |
| 9 | 9 | final class RoleVoter implements IVoter |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var string[] |
|
| 13 | - */ |
|
| 14 | - private $allowedRoles; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var VotingDecisionEnum |
|
| 18 | - */ |
|
| 19 | - private $resultDecision; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var mixed|null |
|
| 23 | - */ |
|
| 24 | - private $reason; |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * RoleVoter constructor. |
|
| 29 | - * @param VotingDecisionEnum $resultDecision |
|
| 30 | - * @param string|string[] $allowedRoles |
|
| 31 | - * @param mixed $reason |
|
| 32 | - */ |
|
| 33 | - public function __construct(VotingDecisionEnum $resultDecision, $allowedRoles, $reason = null) |
|
| 34 | - { |
|
| 35 | - $this->allowedRoles = (array) $allowedRoles; |
|
| 36 | - $this->resultDecision = $resultDecision; |
|
| 37 | - $this->reason = $reason; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param mixed $votingSubject |
|
| 43 | - * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 44 | - * @return ISingleVoterResult |
|
| 45 | - */ |
|
| 46 | - public function vote($votingSubject, IVotingContext $votingContext) |
|
| 47 | - { |
|
| 48 | - if (!($votingContext instanceof IIdentityContext)) { |
|
| 49 | - throw new InvalidArgumentException('RoleVoter can be used only with specific voting context, implementing IIdentityContext.'); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - if (array_intersect($votingContext->getRoles(), $this->allowedRoles)) { |
|
| 53 | - return new SingleVoterResult($this->resultDecision, $this->reason); |
|
| 54 | - } |
|
| 55 | - return null; |
|
| 56 | - } |
|
| 11 | + /** |
|
| 12 | + * @var string[] |
|
| 13 | + */ |
|
| 14 | + private $allowedRoles; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var VotingDecisionEnum |
|
| 18 | + */ |
|
| 19 | + private $resultDecision; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var mixed|null |
|
| 23 | + */ |
|
| 24 | + private $reason; |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * RoleVoter constructor. |
|
| 29 | + * @param VotingDecisionEnum $resultDecision |
|
| 30 | + * @param string|string[] $allowedRoles |
|
| 31 | + * @param mixed $reason |
|
| 32 | + */ |
|
| 33 | + public function __construct(VotingDecisionEnum $resultDecision, $allowedRoles, $reason = null) |
|
| 34 | + { |
|
| 35 | + $this->allowedRoles = (array) $allowedRoles; |
|
| 36 | + $this->resultDecision = $resultDecision; |
|
| 37 | + $this->reason = $reason; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param mixed $votingSubject |
|
| 43 | + * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 44 | + * @return ISingleVoterResult |
|
| 45 | + */ |
|
| 46 | + public function vote($votingSubject, IVotingContext $votingContext) |
|
| 47 | + { |
|
| 48 | + if (!($votingContext instanceof IIdentityContext)) { |
|
| 49 | + throw new InvalidArgumentException('RoleVoter can be used only with specific voting context, implementing IIdentityContext.'); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + if (array_intersect($votingContext->getRoles(), $this->allowedRoles)) { |
|
| 53 | + return new SingleVoterResult($this->resultDecision, $this->reason); |
|
| 54 | + } |
|
| 55 | + return null; |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -32,7 +32,7 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | public function __construct(VotingDecisionEnum $resultDecision, $allowedRoles, $reason = null) |
| 34 | 34 | { |
| 35 | - $this->allowedRoles = (array) $allowedRoles; |
|
| 35 | + $this->allowedRoles = (array)$allowedRoles; |
|
| 36 | 36 | $this->resultDecision = $resultDecision; |
| 37 | 37 | $this->reason = $reason; |
| 38 | 38 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | interface IVoter |
| 7 | 7 | { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @param mixed $votingSubject |
|
| 11 | - * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 12 | - * @return ISingleVoterResult |
|
| 13 | - */ |
|
| 14 | - public function vote($votingSubject, IVotingContext $votingContext); |
|
| 9 | + /** |
|
| 10 | + * @param mixed $votingSubject |
|
| 11 | + * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 12 | + * @return ISingleVoterResult |
|
| 13 | + */ |
|
| 14 | + public function vote($votingSubject, IVotingContext $votingContext); |
|
| 15 | 15 | } |
@@ -8,36 +8,36 @@ |
||
| 8 | 8 | |
| 9 | 9 | class ConstVoter implements IVoter |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var VotingDecisionEnum |
|
| 13 | - */ |
|
| 14 | - private $constDecision; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var mixed |
|
| 18 | - */ |
|
| 19 | - private $constReason; |
|
| 20 | - |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * ConstVoter constructor. |
|
| 24 | - * @param VotingDecisionEnum $constDecision |
|
| 25 | - * @param mixed $constReason |
|
| 26 | - */ |
|
| 27 | - public function __construct(VotingDecisionEnum $constDecision, $constReason = null) |
|
| 28 | - { |
|
| 29 | - $this->constDecision = $constDecision; |
|
| 30 | - $this->constReason = $constReason; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param mixed $votingSubject |
|
| 36 | - * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 37 | - * @return ISingleVoterResult |
|
| 38 | - */ |
|
| 39 | - public function vote($votingSubject, IVotingContext $votingContext) |
|
| 40 | - { |
|
| 41 | - return new SingleVoterResult($this->constDecision, $this->constReason); |
|
| 42 | - } |
|
| 11 | + /** |
|
| 12 | + * @var VotingDecisionEnum |
|
| 13 | + */ |
|
| 14 | + private $constDecision; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var mixed |
|
| 18 | + */ |
|
| 19 | + private $constReason; |
|
| 20 | + |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * ConstVoter constructor. |
|
| 24 | + * @param VotingDecisionEnum $constDecision |
|
| 25 | + * @param mixed $constReason |
|
| 26 | + */ |
|
| 27 | + public function __construct(VotingDecisionEnum $constDecision, $constReason = null) |
|
| 28 | + { |
|
| 29 | + $this->constDecision = $constDecision; |
|
| 30 | + $this->constReason = $constReason; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param mixed $votingSubject |
|
| 36 | + * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 37 | + * @return ISingleVoterResult |
|
| 38 | + */ |
|
| 39 | + public function vote($votingSubject, IVotingContext $votingContext) |
|
| 40 | + { |
|
| 41 | + return new SingleVoterResult($this->constDecision, $this->constReason); |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -5,8 +5,8 @@ |
||
| 5 | 5 | |
| 6 | 6 | interface IVotingResult extends IResult |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * @return ISingleVoterResult[] |
|
| 10 | - */ |
|
| 11 | - public function getPartialResults(); |
|
| 8 | + /** |
|
| 9 | + * @return ISingleVoterResult[] |
|
| 10 | + */ |
|
| 11 | + public function getPartialResults(); |
|
| 12 | 12 | } |
@@ -14,15 +14,15 @@ |
||
| 14 | 14 | final class GenericVotingManager extends AbstractVotingManager |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @param string $action |
|
| 19 | - * @param mixed $votingSubject |
|
| 20 | - * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 21 | - * @return \SpareParts\Overseer\IVotingResult |
|
| 22 | - * @throws \SpareParts\Overseer\InvalidVotingResultException |
|
| 23 | - */ |
|
| 24 | - public function vote($action, $votingSubject, IVotingContext $votingContext) |
|
| 25 | - { |
|
| 26 | - return $this->innerVote($action, $votingSubject, $votingContext); |
|
| 27 | - } |
|
| 17 | + /** |
|
| 18 | + * @param string $action |
|
| 19 | + * @param mixed $votingSubject |
|
| 20 | + * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 21 | + * @return \SpareParts\Overseer\IVotingResult |
|
| 22 | + * @throws \SpareParts\Overseer\InvalidVotingResultException |
|
| 23 | + */ |
|
| 24 | + public function vote($action, $votingSubject, IVotingContext $votingContext) |
|
| 25 | + { |
|
| 26 | + return $this->innerVote($action, $votingSubject, $votingContext); |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -13,141 +13,141 @@ |
||
| 13 | 13 | class VotingAssembly implements IVotingAssembly |
| 14 | 14 | { |
| 15 | 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 | - |
|
| 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 | - case StrategyEnum::EVERYONE_MUST_ALLOW_TO_BE_ALLOWED(): |
|
| 58 | - return $this->strategyEveryoneMustComply($votingSubject, $votingContext, VotingDecisionEnum::ALLOWED(), VotingDecisionEnum::DENIED()); |
|
| 59 | - |
|
| 60 | - case StrategyEnum::EVERYONE_MUST_DENY_TO_BE_DENIED(): |
|
| 61 | - return $this->strategyEveryoneMustComply($votingSubject, $votingContext, VotingDecisionEnum::DENIED(), VotingDecisionEnum::ALLOWED()); |
|
| 62 | - |
|
| 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 | - 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 | - 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 | - } |
|
| 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 | + |
|
| 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 | + case StrategyEnum::EVERYONE_MUST_ALLOW_TO_BE_ALLOWED(): |
|
| 58 | + return $this->strategyEveryoneMustComply($votingSubject, $votingContext, VotingDecisionEnum::ALLOWED(), VotingDecisionEnum::DENIED()); |
|
| 59 | + |
|
| 60 | + case StrategyEnum::EVERYONE_MUST_DENY_TO_BE_DENIED(): |
|
| 61 | + return $this->strategyEveryoneMustComply($votingSubject, $votingContext, VotingDecisionEnum::DENIED(), VotingDecisionEnum::ALLOWED()); |
|
| 62 | + |
|
| 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 | + 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 | + 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 | 153 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | { |
| 77 | 77 | foreach ($this->voters as $voter) { |
| 78 | 78 | if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
| 79 | - return new VotingResult($lastResult->getDecision(), [$lastResult]); |
|
| 79 | + return new VotingResult($lastResult->getDecision(), [ $lastResult ]); |
|
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | 82 | throw new InvalidVotingResultException('Voting assembly did not decide on any result!'); |
@@ -90,10 +90,10 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | private function strategyAllowUnlessDenied($votingSubject, IVotingContext $votingContext) |
| 92 | 92 | { |
| 93 | - $results = []; |
|
| 93 | + $results = [ ]; |
|
| 94 | 94 | foreach ($this->voters as $name => $voter) { |
| 95 | 95 | if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
| 96 | - $results[] = $lastResult; |
|
| 96 | + $results[ ] = $lastResult; |
|
| 97 | 97 | if ($lastResult->getDecision() === VotingDecisionEnum::DENIED()) { |
| 98 | 98 | return new VotingResult(VotingDecisionEnum::DENIED(), $results); |
| 99 | 99 | } |
@@ -110,10 +110,10 @@ discard block |
||
| 110 | 110 | */ |
| 111 | 111 | private function strategyDenyUnlessAllowed($votingSubject, IVotingContext $votingContext) |
| 112 | 112 | { |
| 113 | - $results = []; |
|
| 113 | + $results = [ ]; |
|
| 114 | 114 | foreach ($this->voters as $name => $voter) { |
| 115 | 115 | if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) { |
| 116 | - $results[] = $lastResult; |
|
| 116 | + $results[ ] = $lastResult; |
|
| 117 | 117 | if ($lastResult->getDecision() === VotingDecisionEnum::ALLOWED()) { |
| 118 | 118 | return new VotingResult(VotingDecisionEnum::ALLOWED(), $results); |
| 119 | 119 | } |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | VotingDecisionEnum $defaultDecision, |
| 138 | 138 | VotingDecisionEnum $counterDecision |
| 139 | 139 | ) { |
| 140 | - $results = []; |
|
| 140 | + $results = [ ]; |
|
| 141 | 141 | $decision = $defaultDecision; |
| 142 | 142 | foreach ($this->voters as $voter) { |
| 143 | 143 | $result = $voter->vote($votingSubject, $votingContext); |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | if ($result->getDecision() !== $defaultDecision) { |
| 146 | 146 | $decision = $counterDecision; |
| 147 | 147 | } |
| 148 | - $results[] = $result; |
|
| 148 | + $results[ ] = $result; |
|
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | return new VotingResult($decision, $results); |
@@ -9,8 +9,8 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | final class VotingDecisionEnum extends Enum |
| 11 | 11 | { |
| 12 | - protected static $values = [ |
|
| 13 | - 'ALLOWED', |
|
| 14 | - 'DENIED', |
|
| 15 | - ]; |
|
| 12 | + protected static $values = [ |
|
| 13 | + 'ALLOWED', |
|
| 14 | + 'DENIED', |
|
| 15 | + ]; |
|
| 16 | 16 | } |
@@ -12,11 +12,11 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | final class StrategyEnum extends Enum |
| 14 | 14 | { |
| 15 | - protected static $values = [ |
|
| 16 | - 'FIRST_VOTE_DECIDES', |
|
| 17 | - 'ALLOW_UNLESS_DENIED', |
|
| 18 | - 'DENY_UNLESS_ALLOWED', |
|
| 19 | - 'EVERYONE_MUST_ALLOW_TO_BE_ALLOWED', |
|
| 20 | - 'EVERYONE_MUST_DENY_TO_BE_DENIED', |
|
| 21 | - ]; |
|
| 15 | + protected static $values = [ |
|
| 16 | + 'FIRST_VOTE_DECIDES', |
|
| 17 | + 'ALLOW_UNLESS_DENIED', |
|
| 18 | + 'DENY_UNLESS_ALLOWED', |
|
| 19 | + 'EVERYONE_MUST_ALLOW_TO_BE_ALLOWED', |
|
| 20 | + 'EVERYONE_MUST_DENY_TO_BE_DENIED', |
|
| 21 | + ]; |
|
| 22 | 22 | } |
@@ -8,54 +8,54 @@ |
||
| 8 | 8 | |
| 9 | 9 | class ClosureVoter implements IVoter |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var \Closure |
|
| 13 | - */ |
|
| 14 | - private $authorizationClosure; |
|
| 15 | - |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * ClosureVoter constructor. |
|
| 19 | - * @param \Closure $authorizationClosure |
|
| 20 | - */ |
|
| 21 | - public function __construct(\Closure $authorizationClosure) |
|
| 22 | - { |
|
| 23 | - $this->authorizationClosure = $authorizationClosure; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @param mixed $votingSubject |
|
| 29 | - * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 30 | - * @return ISingleVoterResult |
|
| 31 | - */ |
|
| 32 | - public function vote($votingSubject, IVotingContext $votingContext) |
|
| 33 | - { |
|
| 34 | - $closure = $this->authorizationClosure; |
|
| 35 | - $result = $closure($votingSubject, $votingContext); |
|
| 36 | - |
|
| 37 | - $result = $this->prepareResult($result); |
|
| 38 | - |
|
| 39 | - return $result; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param mixed $result |
|
| 45 | - * @return SingleVoterResult |
|
| 46 | - */ |
|
| 47 | - protected function prepareResult($result) |
|
| 48 | - { |
|
| 49 | - if ($result === true) { |
|
| 50 | - $result = new SingleVoterResult(VotingDecisionEnum::ALLOWED()); |
|
| 51 | - return $result; |
|
| 52 | - } elseif ($result === false) { |
|
| 53 | - $result = new SingleVoterResult(VotingDecisionEnum::DENIED()); |
|
| 54 | - return $result; |
|
| 55 | - } elseif ($result instanceof VotingDecisionEnum) { |
|
| 56 | - $result = new SingleVoterResult($result); |
|
| 57 | - return $result; |
|
| 58 | - } |
|
| 59 | - return $result; |
|
| 60 | - } |
|
| 11 | + /** |
|
| 12 | + * @var \Closure |
|
| 13 | + */ |
|
| 14 | + private $authorizationClosure; |
|
| 15 | + |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * ClosureVoter constructor. |
|
| 19 | + * @param \Closure $authorizationClosure |
|
| 20 | + */ |
|
| 21 | + public function __construct(\Closure $authorizationClosure) |
|
| 22 | + { |
|
| 23 | + $this->authorizationClosure = $authorizationClosure; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @param mixed $votingSubject |
|
| 29 | + * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 30 | + * @return ISingleVoterResult |
|
| 31 | + */ |
|
| 32 | + public function vote($votingSubject, IVotingContext $votingContext) |
|
| 33 | + { |
|
| 34 | + $closure = $this->authorizationClosure; |
|
| 35 | + $result = $closure($votingSubject, $votingContext); |
|
| 36 | + |
|
| 37 | + $result = $this->prepareResult($result); |
|
| 38 | + |
|
| 39 | + return $result; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param mixed $result |
|
| 45 | + * @return SingleVoterResult |
|
| 46 | + */ |
|
| 47 | + protected function prepareResult($result) |
|
| 48 | + { |
|
| 49 | + if ($result === true) { |
|
| 50 | + $result = new SingleVoterResult(VotingDecisionEnum::ALLOWED()); |
|
| 51 | + return $result; |
|
| 52 | + } elseif ($result === false) { |
|
| 53 | + $result = new SingleVoterResult(VotingDecisionEnum::DENIED()); |
|
| 54 | + return $result; |
|
| 55 | + } elseif ($result instanceof VotingDecisionEnum) { |
|
| 56 | + $result = new SingleVoterResult($result); |
|
| 57 | + return $result; |
|
| 58 | + } |
|
| 59 | + return $result; |
|
| 60 | + } |
|
| 61 | 61 | } |