Completed
Pull Request — master (#1)
by Ondrej
01:51
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\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;
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...
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)
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...
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)
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...
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