Completed
Pull Request — master (#3)
by
unknown
02:03
created

VotingAssembly::canVoteOn()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 3
crap 12
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\VotingDecisionEnum;
10
use SpareParts\Overseer\VotingResult;
11
12
class VotingAssembly implements IVotingAssembly
13
{
14
15
	/**
16
	 * @var StrategyEnum
17
	 */
18
	private $strategy;
19
20
	/**
21
	 * @var IVoter[]
22
	 */
23
	private $voters;
24
25
26
	/**
27
	 * VotingAssembly constructor.
28
	 * @param StrategyEnum $strategy
29
	 * @param \SpareParts\Overseer\Voter\IVoter[] $voters
30
	 */
31
	public function __construct(StrategyEnum $strategy, array $voters)
32
	{
33
		$this->strategy = $strategy;
34
		$this->voters = $voters;
35
	}
36
37
38
	/**
39
	 * @param mixed $votingSubject
40
	 * @param \SpareParts\Overseer\Context\IVotingContext $votingContext
41
	 * @return null|\SpareParts\Overseer\IVotingResult
42
	 * @throws \SpareParts\Overseer\InvalidVotingResultException
43
	 */
44
	public function commenceVote($votingSubject, IVotingContext $votingContext)
45
	{
46
		$result = null;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
			default:
58 4
				throw new InvalidVotingResultException('Unable to decide on result, invalid strategy: '.$this->strategy);
59 4
		}
60 4
	}
61 4
62 4
63
	/**
64
	 * @param mixed $votingSubject
65
	 * @param \SpareParts\Overseer\Context\IVotingContext $votingContext
66
	 * @return \SpareParts\Overseer\IVotingResult
67
	 * @throws \SpareParts\Overseer\InvalidVotingResultException
68
	 */
69
	private function strategyFirstVoteDecides($votingSubject, IVotingContext $votingContext)
70
	{
71
		foreach ($this->voters as $voter) {
72
			if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) {
73
                return new VotingResult($lastResult->getDecision(), [$lastResult]);
74
			}
75
		}
76
		throw new InvalidVotingResultException('Voting assembly did not decide on any result!');
77
	}
78
79
80
	/**
81
	 * @param mixed $votingSubject
82
	 * @param \SpareParts\Overseer\Context\IVotingContext $votingContext
83
	 * @return \SpareParts\Overseer\IVotingResult
84
	 */
85 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...
86
	{
87
	    $results = [];
88
		foreach ($this->voters as $name => $voter) {
89
			if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) {
90
                $results[] = $lastResult;
91
			    if ($lastResult->getDecision() === VotingDecisionEnum::DENIED()) {
92
                    return new VotingResult(VotingDecisionEnum::DENIED(), $results);
93
                }
94
			}
95
		}
96
		return new VotingResult(VotingDecisionEnum::ALLOWED(), $results);
97
	}
98
99
100
	/**
101
	 * @param mixed $votingSubject
102
	 * @param \SpareParts\Overseer\Context\IVotingContext $votingContext
103
	 * @return \SpareParts\Overseer\IVotingResult
104
	 */
105 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...
106
	{
107
        $results = [];
108
        foreach ($this->voters as $name => $voter) {
109
            if (($lastResult = $voter->vote($votingSubject, $votingContext)) !== null) {
110
                $results[] = $lastResult;
111
                if ($lastResult->getDecision() === VotingDecisionEnum::ALLOWED()) {
112
                    return new VotingResult(VotingDecisionEnum::ALLOWED(), $results);
113
                }
114
            }
115
        }
116
        return new VotingResult(VotingDecisionEnum::DENIED(), $results);
117
	}
118
}
119