@@ -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 | } |
@@ -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 | } |
@@ -9,64 +9,64 @@ |
||
| 9 | 9 | class VotingAbilityAwareAssembly extends VotingAssembly implements IVotingAbilityAwareAssembly |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $subjectClassName; |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $subjectClassName; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $contextClassName; |
|
| 17 | + /** |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $contextClassName; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var string[] |
|
| 24 | - */ |
|
| 25 | - protected $actionList; |
|
| 22 | + /** |
|
| 23 | + * @var string[] |
|
| 24 | + */ |
|
| 25 | + protected $actionList; |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @param \SpareParts\Overseer\StrategyEnum $strategy |
|
| 30 | - * @param IVoter[] $voters |
|
| 31 | - * @param string|string[] $actions |
|
| 32 | - * @param string $contextClassName |
|
| 33 | - * @param string $subjectClassName |
|
| 34 | - */ |
|
| 35 | - public function __construct( |
|
| 36 | - StrategyEnum $strategy, |
|
| 37 | - array $voters, |
|
| 38 | - $actions = null, |
|
| 39 | - $subjectClassName = null, |
|
| 40 | - $contextClassName = null |
|
| 41 | - ) { |
|
| 42 | - parent::__construct($strategy, $voters); |
|
| 28 | + /** |
|
| 29 | + * @param \SpareParts\Overseer\StrategyEnum $strategy |
|
| 30 | + * @param IVoter[] $voters |
|
| 31 | + * @param string|string[] $actions |
|
| 32 | + * @param string $contextClassName |
|
| 33 | + * @param string $subjectClassName |
|
| 34 | + */ |
|
| 35 | + public function __construct( |
|
| 36 | + StrategyEnum $strategy, |
|
| 37 | + array $voters, |
|
| 38 | + $actions = null, |
|
| 39 | + $subjectClassName = null, |
|
| 40 | + $contextClassName = null |
|
| 41 | + ) { |
|
| 42 | + parent::__construct($strategy, $voters); |
|
| 43 | 43 | |
| 44 | - $this->actionList = (array) $actions; |
|
| 45 | - $this->subjectClassName = $subjectClassName; |
|
| 46 | - $this->contextClassName = $contextClassName; |
|
| 47 | - } |
|
| 44 | + $this->actionList = (array) $actions; |
|
| 45 | + $this->subjectClassName = $subjectClassName; |
|
| 46 | + $this->contextClassName = $contextClassName; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param string $actionName |
|
| 52 | - * @param object $subject |
|
| 53 | - * @param \SpareParts\Overseer\Context\IVotingContext $context |
|
| 54 | - * @return bool |
|
| 55 | - */ |
|
| 56 | - public function canVoteOn($actionName, $subject, IVotingContext $context) |
|
| 57 | - { |
|
| 58 | - if ($this->subjectClassName && !($subject instanceof $this->subjectClassName)) { |
|
| 59 | - return false; |
|
| 60 | - } |
|
| 50 | + /** |
|
| 51 | + * @param string $actionName |
|
| 52 | + * @param object $subject |
|
| 53 | + * @param \SpareParts\Overseer\Context\IVotingContext $context |
|
| 54 | + * @return bool |
|
| 55 | + */ |
|
| 56 | + public function canVoteOn($actionName, $subject, IVotingContext $context) |
|
| 57 | + { |
|
| 58 | + if ($this->subjectClassName && !($subject instanceof $this->subjectClassName)) { |
|
| 59 | + return false; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - if ($this->contextClassName && !($context instanceof $this->contextClassName)) { |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 62 | + if ($this->contextClassName && !($context instanceof $this->contextClassName)) { |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - if ($this->actionList && !in_array($actionName, $this->actionList)) { |
|
| 67 | - return false; |
|
| 68 | - } |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 66 | + if ($this->actionList && !in_array($actionName, $this->actionList)) { |
|
| 67 | + return false; |
|
| 68 | + } |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | 72 | } |
@@ -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 | } |