1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Opensoft\RolloutBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Opensoft\Rollout\Rollout; |
6
|
|
|
use Opensoft\RolloutBundle\Rollout\UserProviderInterface; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Richard Fullmer <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class DefaultController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return Response |
19
|
|
|
*/ |
20
|
|
|
public function indexAction() |
21
|
|
|
{ |
22
|
|
|
return $this->container->get('templating')->renderResponse('OpensoftRolloutBundle:Default:index.html.twig', array('rollout' => $this->getRollout())); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $feature |
27
|
|
|
* @return RedirectResponse |
28
|
|
|
*/ |
29
|
|
|
public function activateAction($feature) |
30
|
|
|
{ |
31
|
|
|
$this->getRollout()->activate($feature); |
32
|
|
|
|
33
|
|
|
$this->addFlash('success', sprintf("Feature '%s' is now globally activated", $feature)); |
34
|
|
|
|
35
|
|
|
return $this->createRedirectToFeatureListReponse(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $feature |
40
|
|
|
* @return RedirectResponse |
41
|
|
|
*/ |
42
|
|
|
public function deactivateAction($feature) |
43
|
|
|
{ |
44
|
|
|
$this->getRollout()->deactivate($feature); |
45
|
|
|
|
46
|
|
|
$this->addFlash('danger', sprintf("Feature '%s' is now globally deactivated", $feature)); |
47
|
|
|
|
48
|
|
|
return $this->createRedirectToFeatureListReponse(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $feature |
53
|
|
|
* @return RedirectResponse |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function incrementPercentageAction($feature) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$rollout = $this->getRollout(); |
58
|
|
|
$percentage = $rollout->get($feature)->getPercentage() + 10; |
59
|
|
|
$rollout->activatePercentage($feature, $percentage); |
60
|
|
|
|
61
|
|
|
$this->addFlash('info', sprintf("Feature '%s' percentage changed to %d%% of all users", $feature, $percentage)); |
62
|
|
|
|
63
|
|
|
return $this->createRedirectToFeatureListReponse(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $feature |
68
|
|
|
* @return RedirectResponse |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function decrementPercentageAction($feature) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$rollout = $this->getRollout(); |
73
|
|
|
$percentage = $rollout->get($feature)->getPercentage() - 10; |
74
|
|
|
$rollout->activatePercentage($feature, $percentage); |
75
|
|
|
|
76
|
|
|
$this->addFlash('info', sprintf("Feature '%s' percentage changed to %d%% of all users", $feature, $percentage)); |
77
|
|
|
|
78
|
|
|
return $this->createRedirectToFeatureListReponse(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $feature |
83
|
|
|
* @param string $group |
84
|
|
|
* @return RedirectResponse |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function activateGroupAction($feature, $group) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this->getRollout()->activateGroup($feature, $group); |
89
|
|
|
|
90
|
|
|
$this->addFlash('info', sprintf("Feature '%s' is now active in group '%s'", $feature, $group)); |
91
|
|
|
|
92
|
|
|
return $this->createRedirectToFeatureListReponse(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $feature |
97
|
|
|
* @param string $group |
98
|
|
|
* @return RedirectResponse |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function deactivateGroupAction($feature, $group) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$this->getRollout()->deactivateGroup($feature, $group); |
103
|
|
|
|
104
|
|
|
$this->addFlash('info', sprintf("Feature '%s' is no longer active in group '%s'", $feature, $group)); |
105
|
|
|
|
106
|
|
|
return $this->createRedirectToFeatureListReponse(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Request $request |
111
|
|
|
* @param string $feature |
112
|
|
|
* @return RedirectResponse |
113
|
|
|
*/ |
114
|
|
|
public function activateUserAction(Request $request, $feature) |
115
|
|
|
{ |
116
|
|
|
$requestUser = $request->get('user'); |
117
|
|
|
$user = $this->getRolloutUserProvider()->findByRolloutIdentifier($requestUser); |
118
|
|
|
|
119
|
|
|
if ($user) { |
120
|
|
|
$this->getRollout()->activateUser($feature, $user); |
121
|
|
|
|
122
|
|
|
$this->addFlash('info', sprintf("User '%s' was activated in feature '%s'", $user->getRolloutIdentifier(), $feature)); |
123
|
|
|
} else { |
124
|
|
|
$this->addFlash('danger', sprintf("User '%s' not found", $requestUser)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->createRedirectToFeatureListReponse(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $feature |
132
|
|
|
* @param string $id |
133
|
|
|
* @return RedirectResponse |
134
|
|
|
*/ |
135
|
|
|
public function deactivateUserAction($feature, $id) |
136
|
|
|
{ |
137
|
|
|
$user = $this->getRolloutUserProvider()->findByRolloutIdentifier($id); |
138
|
|
|
$this->getRollout()->deactivateUser($feature, $user); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$this->addFlash('info', sprintf("User '%s' was deactivated from feature '%s'", $user->getRolloutIdentifier(), $feature)); |
141
|
|
|
|
142
|
|
|
return $this->createRedirectToFeatureListReponse(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $feature |
147
|
|
|
* @return RedirectResponse |
148
|
|
|
*/ |
149
|
|
|
public function removeAction($feature) |
150
|
|
|
{ |
151
|
|
|
$this->getRollout()->remove($feature); |
152
|
|
|
|
153
|
|
|
$this->addFlash('info', sprintf("Feature '%s' was removed from rollout.", $feature)); |
154
|
|
|
|
155
|
|
|
return $this->createRedirectToFeatureListReponse(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param Request $request |
160
|
|
|
* @param string $feature |
161
|
|
|
* @return RedirectResponse |
162
|
|
|
*/ |
163
|
|
|
public function setRequestParamAction(Request $request, $feature) |
164
|
|
|
{ |
165
|
|
|
$requestParam = $request->get('requestParam'); |
166
|
|
|
if ($requestParam === null) { |
167
|
|
|
$this->addFlash('danger', 'Missing "requestParam" value'); |
168
|
|
|
return $this->createRedirectToFeatureListReponse(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->getRollout()->activateRequestParam($feature, $requestParam); |
172
|
|
|
|
173
|
|
|
$this->addFlash('info', sprintf('Feature "%s" requestParam changed to "%s"', $feature, $requestParam)); |
174
|
|
|
|
175
|
|
|
return $this->createRedirectToFeatureListReponse(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return Rollout |
180
|
|
|
*/ |
181
|
|
|
private function getRollout() |
182
|
|
|
{ |
183
|
|
|
return $this->container->get('rollout'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return UserProviderInterface |
188
|
|
|
*/ |
189
|
|
|
private function getRolloutUserProvider() |
190
|
|
|
{ |
191
|
|
|
return $this->container->get('rollout.user_provider'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return RedirectResponse |
196
|
|
|
*/ |
197
|
|
|
private function createRedirectToFeatureListReponse() |
198
|
|
|
{ |
199
|
|
|
return new RedirectResponse($this->container->get('router')->generate('opensoft_rollout')); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
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.