Completed
Push — master ( bff5f7...caa188 )
by Lena
12:09
created

RolloutAbstract::getFeatures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace QueerCodingGirl\Rollout;
4
5
6
use QueerCodingGirl\Rollout\Feature\FeatureFactoryAbstract;
7
use QueerCodingGirl\Rollout\Interfaces\DeterminableUserInterface;
8
use QueerCodingGirl\Rollout\Interfaces\KeyValueStorageInterface;
9
use QueerCodingGirl\Rollout\Interfaces\RolloutableInterface;
10
11
/**
12
 * Class RolloutAbstract
13
 * @package QueerCodingGirl\Rollout
14
 */
15
abstract class RolloutAbstract
16
{
17
    /**
18
     * @var KeyValueStorageInterface
19
     */
20
    protected $storage;
21
22
    /**
23
     * @var FeatureFactoryAbstract
24
     */
25
    protected $featureFactory;
26
27
    /**
28
     * @param string $featureName
29
     * @return RolloutableInterface
30
     */
31
    public function getFeature($featureName)
32
    {
33
        $featureConfigString = $this->storage->get($this->getKey($featureName));
34
        $feature = $this->featureFactory->getFeature($featureName);
35
        if (false === empty($featureConfigString)) {
36
            $feature->configureByConfigString($featureConfigString);
37
        }
38
        return $feature;
39
    }
40
41
    /**
42
     * Get all feature names
43
     * @return string[]
44
     */
45
    public function getFeatures()
46
    {
47
        $featuresString = $this->storage->get($this->getFeaturesKey());
48
        $features = explode(',', $featuresString);
49
        return $features;
50
    }
51
52
    /**
53
     * Saves a feature to the storage
54
     * @param RolloutableInterface $feature
55
     */
56
    protected function saveFeature(RolloutableInterface $feature)
57
    {
58
        $this->storage->set($this->getKey($feature->getName()), (string)$feature);
59
        $features = $this->getFeatures();
60
        if (false === in_array($feature->getName(), $features)) {
61
            $features[] = $feature->getName();
62
        }
63
        $this->storage->set($this->getFeaturesKey(), implode(',', $features));
64
    }
65
66
    /**
67
     * Get the storage key for a feature name
68
     * @param string $featureName
69
     * @return string
70
     */
71
    protected function getKey($featureName)
72
    {
73
        return 'feature:' . $featureName;
74
    }
75
76
    /**
77
     * Get the storage key for all features (the feature list)
78
     * @return string
79
     */
80
    protected function getFeaturesKey()
81
    {
82
        return 'feature:__features__';
83
    }
84
85
    /**
86
     * Activates a feature (for everyone)
87
     * @param string $featureName
88
     */
89
    public function activate($featureName)
90
    {
91
        $feature = $this->getFeature($featureName);
92
        $feature->setPercentage(100);
93
        $this->saveFeature($feature);
94
    }
95
96
    /**
97
     * Deactivates a feature (for everyone)
98
     * @param string $featureName
99
     */
100
    public function deactivate($featureName)
101
    {
102
        $feature = $this->getFeature($featureName);
103
        $feature->clearConfig();
104
        $this->saveFeature($feature);
105
    }
106
107
    /**
108
     * Activates a feature for a specific role
109
     * @param string $featureName
110
     * @param string $roleName
111
     */
112
    public function activateRole($featureName, $roleName)
113
    {
114
        $feature = $this->getFeature($featureName);
115
        $feature->addRole($roleName);
116
        $this->saveFeature($feature);
117
    }
118
119
    /**
120
     * Deactivates a feature for a specific role.
121
     * @param string $featureName
122
     * @param string $roleName
123
     */
124
    public function deactivateRole($featureName, $roleName)
125
    {
126
        $feature = $this->getFeature($featureName);
127
        $feature->removeRole($roleName);
128
        $this->saveFeature($feature);
129
    }
130
131
    /**
132
     * Activates a feature for a specific group
133
     * @param string $featureName
134
     * @param string $groupName
135
     */
136
    public function activateGroup($featureName, $groupName)
137
    {
138
        $feature = $this->getFeature($featureName);
139
        $feature->addGroup($groupName);
140
        $this->saveFeature($feature);
141
    }
142
143
    /**
144
     * Deactivates a feature for a specific group.
145
     * @param string $featureName
146
     * @param string $groupName
147
     */
148
    public function deactivateGroup($featureName, $groupName)
149
    {
150
        $feature = $this->getFeature($featureName);
151
        $feature->removeGroup($groupName);
152
        $this->saveFeature($feature);
153
    }
154
155
    /**
156
     * Activates a feature for a specific user
157
     * @param string $featureName
158
     * @param integer $userId
159
     */
160
    public function activateUser($featureName, $userId)
161
    {
162
        $feature = $this->getFeature($featureName);
163
        $feature->addUser($userId);
164
        $this->saveFeature($feature);
165
    }
166
167
    /**
168
     * Deactivates a feature for a specific user
169
     * @param string $featureName
170
     * @param integer $userId
171
     */
172
    public function deactivateUser($featureName, $userId)
173
    {
174
        $feature = $this->getFeature($featureName);
175
        $feature->removeUser($userId);
176
        $this->saveFeature($feature);
177
    }
178
179
    /**
180
     * Activates a feature for a percentage of users
181
     * @param string $featureName
182
     * @param integer $percentage
183
     */
184
    public function activatePercentage($featureName, $percentage)
185
    {
186
        $feature = $this->getFeature($featureName);
187
        $feature->setPercentage($percentage);
188
        $this->saveFeature($feature);
189
    }
190
191
    /**
192
     * Deactivates the percentage activation for a feature
193
     * @param string $featureName
194
     */
195
    public function deactivatePercentage($featureName)
196
    {
197
        $feature = $this->getFeature($featureName);
198
        $feature->setPercentage(0);
199
        $this->saveFeature($feature);
200
    }
201
202
    /**
203
     * Checks if a feature is active
204
     * @param string $featureName
205
     * @param DeterminableUserInterface $user
206
     * @return bool
207
     */
208
    public function isActive($featureName, DeterminableUserInterface $user = null)
209
    {
210
        $feature = $this->getFeature($featureName);
211
        return $feature->isActive($this, $user);
212
    }
213
214
    /**
215
     * Checks if a user has the given role
216
     * @param string $roleName
217
     * @param DeterminableUserInterface $user
218
     * @return bool
219
     */
220
    public function userHasRole($roleName, DeterminableUserInterface $user)
221
    {
222
        $userHasRole = false;
223
        if (true === in_array($roleName, $user->getRoles())) {
224
            $userHasRole = true;
225
        }
226
        return $userHasRole;
227
    }
228
229
    /**
230
     * Checks if a user has the given group
231
     * @param string $groupName
232
     * @param DeterminableUserInterface $user
233
     * @return bool
234
     */
235
    public function userHasGroup($groupName, DeterminableUserInterface $user)
236
    {
237
        $userHasGroup = false;
238
        if (true === in_array($groupName, $user->getGroups())) {
239
            $userHasGroup = true;
240
        }
241
        return $userHasGroup;
242
    }
243
}