Completed
Push — master ( cd118f...c77b3e )
by Ondrej
10s
created
src/Assembly/VotingAssembly.php 2 patches
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -13,141 +13,141 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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);
Please login to merge, or discard this patch.
src/VotingDecisionEnum.php 2 patches
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -4,58 +4,58 @@
 block discarded – undo
4 4
 
5 5
 final class VotingDecisionEnum
6 6
 {
7
-    /**
8
-     * @var string
9
-     */
10
-    private $value;
11
-
12
-    /**
13
-     * @var self[]
14
-     */
15
-    static protected $registry = [];
16
-
17
-    private function __construct($value)
18
-    {
19
-        $this->value = $value;
20
-    }
21
-
22
-
23
-    /**
24
-     * @return self
25
-     */
26
-    public static function ALLOWED()
27
-    {
28
-        return static::instance('allowed');
29
-    }
30
-
31
-
32
-    /**
33
-     * @return self
34
-     */
35
-    public static function DENIED()
36
-    {
37
-        return static::instance('denied');
38
-    }
39
-
40
-
41
-    /**
42
-     * @param string $string
43
-     * @return self
44
-     */
45
-    protected static function instance($string)
46
-    {
47
-        if (!isset(static::$registry[$string])) {
48
-            static::$registry[$string] = new static($string);
49
-        }
50
-        return static::$registry[$string];
51
-    }
52
-
53
-
54
-    /**
55
-     * @return string
56
-     */
57
-    public function __toString()
58
-    {
59
-        return $this->value;
60
-    }
7
+	/**
8
+	 * @var string
9
+	 */
10
+	private $value;
11
+
12
+	/**
13
+	 * @var self[]
14
+	 */
15
+	static protected $registry = [];
16
+
17
+	private function __construct($value)
18
+	{
19
+		$this->value = $value;
20
+	}
21
+
22
+
23
+	/**
24
+	 * @return self
25
+	 */
26
+	public static function ALLOWED()
27
+	{
28
+		return static::instance('allowed');
29
+	}
30
+
31
+
32
+	/**
33
+	 * @return self
34
+	 */
35
+	public static function DENIED()
36
+	{
37
+		return static::instance('denied');
38
+	}
39
+
40
+
41
+	/**
42
+	 * @param string $string
43
+	 * @return self
44
+	 */
45
+	protected static function instance($string)
46
+	{
47
+		if (!isset(static::$registry[$string])) {
48
+			static::$registry[$string] = new static($string);
49
+		}
50
+		return static::$registry[$string];
51
+	}
52
+
53
+
54
+	/**
55
+	 * @return string
56
+	 */
57
+	public function __toString()
58
+	{
59
+		return $this->value;
60
+	}
61 61
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
     /**
13 13
      * @var self[]
14 14
      */
15
-    static protected $registry = [];
15
+    static protected $registry = [ ];
16 16
 
17 17
     private function __construct($value)
18 18
     {
@@ -44,10 +44,10 @@  discard block
 block discarded – undo
44 44
      */
45 45
     protected static function instance($string)
46 46
     {
47
-        if (!isset(static::$registry[$string])) {
48
-            static::$registry[$string] = new static($string);
47
+        if (!isset(static::$registry[ $string ])) {
48
+            static::$registry[ $string ] = new static($string);
49 49
         }
50
-        return static::$registry[$string];
50
+        return static::$registry[ $string ];
51 51
     }
52 52
 
53 53
 
Please login to merge, or discard this patch.
src/StrategyEnum.php 2 patches
Indentation   +81 added lines, -81 removed lines patch added patch discarded remove patch
@@ -4,85 +4,85 @@
 block discarded – undo
4 4
 
5 5
 class StrategyEnum
6 6
 {
7
-    /**
8
-     * @var string
9
-     */
10
-    private $value;
11
-
12
-    /**
13
-     * @var self[]
14
-     */
15
-    static protected $registry = [];
16
-
17
-    private function __construct($value)
18
-    {
19
-        $this->value = $value;
20
-    }
21
-
22
-
23
-    /**
24
-     * @return self
25
-     */
26
-    public static function FIRST_VOTE_DECIDES()
27
-    {
28
-        return static::instance('first_vote_decides');
29
-    }
30
-
31
-
32
-    /**
33
-     * @return self
34
-     */
35
-    public static function ALLOW_UNLESS_DENIED()
36
-    {
37
-        return static::instance('allow_unless_denied');
38
-    }
39
-
40
-
41
-    /**
42
-     * @return self
43
-     */
44
-    public static function DENY_UNLESS_ALLOWED()
45
-    {
46
-        return static::instance('deny_unless_allowed');
47
-    }
48
-
49
-
50
-    /**
51
-     * @return self
52
-     */
53
-    public static function EVERYONE_MUST_ALLOW_TO_BE_ALLOWED()
54
-    {
55
-        return static::instance('everyone_must_allow_to_be_allowed');
56
-    }
57
-
58
-
59
-    /**
60
-     * @return self
61
-     */
62
-    public static function EVERYONE_MUST_DENY_TO_BE_DENIED()
63
-    {
64
-        return static::instance('everyone_must_deny_to_be_denied');
65
-    }
66
-
67
-
68
-    /**
69
-     * @param string $string
70
-     * @return self
71
-     */
72
-    protected static function instance($string)
73
-    {
74
-        if (!isset(static::$registry[$string])) {
75
-            static::$registry[$string] = new static($string);
76
-        }
77
-        return static::$registry[$string];
78
-    }
79
-
80
-
81
-    /**
82
-     * @return string
83
-     */
84
-    public function __toString()
85
-    {
86
-        return $this->value;
87
-    }
7
+	/**
8
+	 * @var string
9
+	 */
10
+	private $value;
11
+
12
+	/**
13
+	 * @var self[]
14
+	 */
15
+	static protected $registry = [];
16
+
17
+	private function __construct($value)
18
+	{
19
+		$this->value = $value;
20
+	}
21
+
22
+
23
+	/**
24
+	 * @return self
25
+	 */
26
+	public static function FIRST_VOTE_DECIDES()
27
+	{
28
+		return static::instance('first_vote_decides');
29
+	}
30
+
31
+
32
+	/**
33
+	 * @return self
34
+	 */
35
+	public static function ALLOW_UNLESS_DENIED()
36
+	{
37
+		return static::instance('allow_unless_denied');
38
+	}
39
+
40
+
41
+	/**
42
+	 * @return self
43
+	 */
44
+	public static function DENY_UNLESS_ALLOWED()
45
+	{
46
+		return static::instance('deny_unless_allowed');
47
+	}
48
+
49
+
50
+	/**
51
+	 * @return self
52
+	 */
53
+	public static function EVERYONE_MUST_ALLOW_TO_BE_ALLOWED()
54
+	{
55
+		return static::instance('everyone_must_allow_to_be_allowed');
56
+	}
57
+
58
+
59
+	/**
60
+	 * @return self
61
+	 */
62
+	public static function EVERYONE_MUST_DENY_TO_BE_DENIED()
63
+	{
64
+		return static::instance('everyone_must_deny_to_be_denied');
65
+	}
66
+
67
+
68
+	/**
69
+	 * @param string $string
70
+	 * @return self
71
+	 */
72
+	protected static function instance($string)
73
+	{
74
+		if (!isset(static::$registry[$string])) {
75
+			static::$registry[$string] = new static($string);
76
+		}
77
+		return static::$registry[$string];
78
+	}
79
+
80
+
81
+	/**
82
+	 * @return string
83
+	 */
84
+	public function __toString()
85
+	{
86
+		return $this->value;
87
+	}
88 88
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
     /**
13 13
      * @var self[]
14 14
      */
15
-    static protected $registry = [];
15
+    static protected $registry = [ ];
16 16
 
17 17
     private function __construct($value)
18 18
     {
@@ -71,10 +71,10 @@  discard block
 block discarded – undo
71 71
      */
72 72
     protected static function instance($string)
73 73
     {
74
-        if (!isset(static::$registry[$string])) {
75
-            static::$registry[$string] = new static($string);
74
+        if (!isset(static::$registry[ $string ])) {
75
+            static::$registry[ $string ] = new static($string);
76 76
         }
77
-        return static::$registry[$string];
77
+        return static::$registry[ $string ];
78 78
     }
79 79
 
80 80
 
Please login to merge, or discard this patch.