Completed
Pull Request — master (#6)
by Ondrej
02:53
created

VotingAssembly::strategyFirstVoteDecides()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 3
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 24
    public function __construct(StrategyEnum $strategy, array $voters)
33
    {
34 24
        $this->strategy = $strategy;
35 24
        $this->voters = $voters;
36 24
    }
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 19
    public function commenceVote($votingSubject, IVotingContext $votingContext)
46
    {
47 19
        switch ($this->strategy) {
48 19
            case StrategyEnum::FIRST_VOTE_DECIDES():
49 7
                return $this->strategyFirstVoteDecides($votingSubject, $votingContext);
50
51 12
            case StrategyEnum::ALLOW_UNLESS_DENIED():
52 3
                return $this->strategyAllowUnlessDenied($votingSubject, $votingContext);
53
54 9
            case StrategyEnum::DENY_UNLESS_ALLOWED():
55 3
                return $this->strategyDenyUnlessAllowed($votingSubject, $votingContext);
56
57 6
            case StrategyEnum::EVERYONE_MUST_ALLOW_TO_BE_ALLOWED():
58 3
                return $this->strategyEveryoneMustComply($votingSubject, $votingContext, VotingDecisionEnum::ALLOWED(), VotingDecisionEnum::DENIED());
59
60 3
            case StrategyEnum::EVERYONE_MUST_DENY_TO_BE_DENIED():
61 3
                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 7
    private function strategyFirstVoteDecides($votingSubject, IVotingContext $votingContext)
76
    {
77 7
        foreach ($this->voters as $voter) {
78 6
            if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) {
79 5
                return new VotingResult($lastResult->getDecision(), [$lastResult]);
80
            }
81 6
        }
82 2
        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 3 View Code Duplication
    private function strategyAllowUnlessDenied($votingSubject, IVotingContext $votingContext)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
92
    {
93 3
        $results = [];
94 3
        foreach ($this->voters as $name => $voter) {
95 2
            if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) {
96 1
                $results[] = $lastResult;
97 1
                if ($lastResult->getDecision() === VotingDecisionEnum::DENIED()) {
98 1
                    return new VotingResult(VotingDecisionEnum::DENIED(), $results);
99
                }
100 1
            }
101 3
        }
102 2
        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 3 View Code Duplication
    private function strategyDenyUnlessAllowed($votingSubject, IVotingContext $votingContext)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
112
    {
113 3
        $results = [];
114 3
        foreach ($this->voters as $name => $voter) {
115 2
            if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) {
116 1
                $results[] = $lastResult;
117 1
                if ($lastResult->getDecision() === VotingDecisionEnum::ALLOWED()) {
118 1
                    return new VotingResult(VotingDecisionEnum::ALLOWED(), $results);
119
                }
120 1
            }
121 3
        }
122 2
        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 6
    private function strategyEveryoneMustComply(
135
        $votingSubject,
136
        IVotingContext $votingContext,
137
        VotingDecisionEnum $defaultDecision,
138
        VotingDecisionEnum $counterDecision
139
    ) {
140 6
        $results = [];
141 6
        $decision = $defaultDecision;
142 6
        foreach ($this->voters as $voter) {
143 6
            $result = $voter->vote($votingSubject, $votingContext);
144 6
            if ($result instanceof ISingleVoterResult) {
145 6
                if ($result->getDecision() !== $defaultDecision) {
146 2
                    $decision = $counterDecision;
147 2
                }
148 6
                $results[] = $result;
149 6
            }
150 6
        }
151 6
        return new VotingResult($decision, $results);
152
    }
153
}
154