1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2016, HelloFresh GmbH. |
4
|
|
|
* All rights reserved. |
5
|
|
|
* |
6
|
|
|
* This source code is licensed under the MIT license found in the |
7
|
|
|
* LICENSE file in the root directory of this source tree. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace HelloFresh\FeatureToggle; |
11
|
|
|
|
12
|
|
|
use Collections\VectorInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Feature class. |
16
|
|
|
*/ |
17
|
|
|
class Feature implements FeatureInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** @var VectorInterface */ |
23
|
|
|
protected $conditions; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
protected $status = ToggleStatus::CONDITIONALLY_ACTIVE; |
27
|
|
|
|
28
|
|
|
/** @var int */ |
29
|
|
|
protected $strategy = ToggleStrategy::AFFIRMATIVE; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Feature constructor. |
33
|
|
|
* @param $name - The name of the feature |
34
|
|
|
* @param VectorInterface $conditions - The conditions to be fulfilled |
35
|
|
|
* @param int $strategy The strategy to decide if the feature is enabled or not |
36
|
|
|
*/ |
37
|
6 |
|
public function __construct($name, VectorInterface $conditions, $strategy = ToggleStrategy::AFFIRMATIVE) |
38
|
|
|
{ |
39
|
6 |
|
$this->name = $name; |
40
|
6 |
|
$this->conditions = $conditions; |
41
|
6 |
|
$this->strategy = $strategy; |
42
|
6 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
5 |
|
public function getName() |
48
|
|
|
{ |
49
|
5 |
|
return $this->name; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function getStrategy() |
56
|
|
|
{ |
57
|
|
|
return $this->strategy; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function getStatus() |
64
|
|
|
{ |
65
|
|
|
return $this->status; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function getConditions() |
72
|
|
|
{ |
73
|
|
|
return $this->conditions; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function activate($status = ToggleStatus::CONDITIONALLY_ACTIVE) |
80
|
|
|
{ |
81
|
|
|
$this->status = $status; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function deactivate() |
90
|
|
|
{ |
91
|
|
|
$this->status = ToggleStatus::INACTIVE; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Checks whether the toggle is active for the given context. |
98
|
|
|
* |
99
|
|
|
* @param Context $context |
100
|
|
|
* |
101
|
|
|
* @return boolean True, if one of conditions hold for the context. |
102
|
|
|
*/ |
103
|
3 |
|
public function activeFor(Context $context) |
104
|
|
|
{ |
105
|
3 |
|
switch ($this->status) { |
106
|
3 |
|
case ToggleStatus::ALWAYS_ACTIVE: |
107
|
|
|
return true; |
108
|
3 |
|
case ToggleStatus::INACTIVE: |
109
|
|
|
return false; |
110
|
3 |
|
case ToggleStatus::CONDITIONALLY_ACTIVE: |
111
|
3 |
|
switch ($this->strategy) { |
112
|
3 |
|
case ToggleStrategy::AFFIRMATIVE: |
113
|
3 |
|
return $this->atLeastOneConditionHolds($context); |
114
|
|
|
case ToggleStrategy::MAJORITY: |
115
|
|
|
return $this->moreThanHalfConditionsHold($context); |
116
|
|
|
case ToggleStrategy::UNANIMOUS: |
117
|
|
|
return $this->allConditionsHold($context); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Context $context |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
3 |
|
private function atLeastOneConditionHolds(Context $context) |
128
|
|
|
{ |
129
|
|
|
/** @var ConditionInterface $condition */ |
130
|
3 |
|
foreach ($this->conditions as $condition) { |
131
|
3 |
|
if ($condition->holdsFor($context)) { |
132
|
3 |
|
return true; |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
1 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Context $context |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
private function moreThanHalfConditionsHold(Context $context) |
145
|
|
|
{ |
146
|
|
|
$nbPositive = 0; |
147
|
|
|
$nbNegative = 0; |
148
|
|
|
|
149
|
|
|
/** @var ConditionInterface $condition */ |
150
|
|
|
foreach ($this->conditions as $condition) { |
151
|
|
|
$condition->holdsFor($context) ? $nbPositive++ : $nbNegative++; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $nbPositive > $nbNegative; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param Context $context |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
private function allConditionsHold(Context $context) |
163
|
|
|
{ |
164
|
|
|
/** @var ConditionInterface $condition */ |
165
|
|
|
foreach ($this->conditions as $condition) { |
166
|
|
|
if (!$condition->holdsFor($context)) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|