Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ApprovalsDispatcher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApprovalsDispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class ApprovalsDispatcher extends Dispatcher { |
||
4 | |||
5 | const ACTION_APPROVALS = 'approvals'; |
||
6 | |||
7 | const ALLOW_APPROVAL = 'ALLOW_APPROVAL'; |
||
8 | |||
9 | const ALLOW_APPROVAL_BYPASS = 'ALLOW_APPROVAL_BYPASS'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private static $allowed_actions = [ |
||
15 | 'approvers', |
||
16 | 'submit', |
||
17 | 'cancel', |
||
18 | 'approve', |
||
19 | 'reject' |
||
20 | ]; |
||
21 | |||
22 | private static $dependencies = [ |
||
23 | 'formatter' => '%$DeploynautAPIFormatter' |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var \DNProject |
||
28 | */ |
||
29 | protected $project = null; |
||
30 | |||
31 | /** |
||
32 | * @var \DNEnvironment |
||
33 | */ |
||
34 | protected $environment = null; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private static $action_types = [ |
||
40 | self::ACTION_APPROVALS |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * This is a per request cache of $this->project()->listMembers() |
||
45 | * @var null|array |
||
46 | */ |
||
47 | private static $_cache_project_members = null; |
||
48 | |||
49 | public function init() { |
||
50 | parent::init(); |
||
51 | |||
52 | $this->project = $this->getCurrentProject(); |
||
53 | if (!$this->project) { |
||
54 | return $this->project404Response(); |
||
55 | } |
||
56 | |||
57 | // Performs canView permission check by limiting visible projects |
||
58 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
59 | if (!$this->environment) { |
||
60 | return $this->environment404Response(); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param \SS_HTTPRequest $request |
||
66 | * @return \SS_HTTPResponse |
||
67 | */ |
||
68 | public function approvers(SS_HTTPRequest $request) { |
||
69 | $list = []; |
||
70 | |||
71 | if (self::$_cache_project_members === null) { |
||
72 | self::$_cache_project_members = $this->project->listMembers(); |
||
73 | } |
||
74 | |||
75 | foreach (self::$_cache_project_members as $data) { |
||
76 | if ($this->project->allowed(self::ALLOW_APPROVAL, Member::get()->byId($data['MemberID']))) { |
||
77 | $list[] = [ |
||
78 | 'id' => $data['MemberID'], |
||
79 | 'email' => $data['Email'], |
||
80 | 'role' => $data['RoleTitle'], |
||
81 | 'name' => $data['FullName'] |
||
82 | ]; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | return $this->getAPIResponse([ |
||
87 | 'approvers' => $list |
||
88 | ], 200); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param \SS_HTTPRequest $request |
||
93 | * @return \SS_HTTPResponse |
||
94 | */ |
||
95 | public function submit(SS_HTTPRequest $request) { |
||
96 | if ($request->httpMethod() !== 'POST') { |
||
97 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
98 | } |
||
99 | |||
100 | $deployment = DNDeployment::get()->byId($request->postVar('id')); |
||
101 | $errorResponse = $this->validateDeployment($deployment); |
||
102 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
103 | return $errorResponse; |
||
104 | } |
||
105 | |||
106 | $approver = Member::get()->byId($request->postVar('approver_id')); |
||
107 | View Code Duplication | if ($approver && $approver->exists()) { |
|
108 | if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) { |
||
109 | return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | try { |
||
114 | if ($approver && $approver->exists()) { |
||
115 | $deployment->ApproverID = $approver->ID; |
||
116 | $deployment->write(); |
||
117 | } |
||
118 | |||
119 | $deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
||
120 | } catch (\Exception $e) { |
||
121 | return $this->getAPIResponse([ |
||
122 | 'message' => $e->getMessage() |
||
123 | ], 400); |
||
124 | } |
||
125 | |||
126 | return $this->getAPIResponse([ |
||
127 | 'message' => 'Deployment request has been submitted', |
||
128 | 'deployment' => $this->formatter->getDeploymentData($deployment) |
||
129 | ], 200); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param \SS_HTTPRequest $request |
||
134 | * @return \SS_HTTPResponse |
||
135 | */ |
||
136 | public function cancel(SS_HTTPRequest $request) { |
||
137 | if ($request->httpMethod() !== 'POST') { |
||
138 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
139 | } |
||
140 | |||
141 | $deployment = DNDeployment::get()->byId($request->postVar('id')); |
||
142 | $errorResponse = $this->validateDeployment($deployment); |
||
143 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
144 | return $errorResponse; |
||
145 | } |
||
146 | |||
147 | // @todo permission checking for cancelling an approval request |
||
148 | try { |
||
149 | $deployment->getMachine()->apply(DNDeployment::TR_NEW); |
||
150 | } catch (\Exception $e) { |
||
151 | return $this->getAPIResponse([ |
||
152 | 'message' => $e->getMessage() |
||
153 | ], 400); |
||
154 | } |
||
155 | |||
156 | return $this->getAPIResponse([ |
||
157 | 'message' => 'Deployment request has been cancelled', |
||
158 | 'deployment' => $this->formatter->getDeploymentData($deployment) |
||
159 | ], 200); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param \SS_HTTPRequest $request |
||
164 | * @return \SS_HTTPResponse |
||
165 | */ |
||
166 | public function approve(SS_HTTPRequest $request) { |
||
167 | if ($request->httpMethod() !== 'POST') { |
||
168 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
169 | } |
||
170 | |||
171 | $deployment = DNDeployment::get()->byId($request->postVar('id')); |
||
172 | $errorResponse = $this->validateDeployment($deployment); |
||
173 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
174 | return $errorResponse; |
||
175 | } |
||
176 | |||
177 | // ensure we have either bypass or approval permission of the logged in user |
||
178 | if ( |
||
179 | !$this->project->allowed(self::ALLOW_APPROVAL_BYPASS, Member::currentUser()) |
||
180 | || !$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser()) |
||
181 | ) { |
||
182 | return $this->getAPIResponse(['message' => 'You are not authorised to approve or bypass this deployment'], 403); |
||
183 | } |
||
184 | |||
185 | // check for specific permission depending on the current state of the deployment: |
||
186 | // submitted => approved requires approval permissions |
||
187 | // new => approved requires bypass permissions. |
||
188 | View Code Duplication | if ( |
|
189 | $deployment->State === DNDeployment::STATE_SUBMITTED |
||
190 | && !$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser()) |
||
191 | ) { |
||
192 | return $this->getAPIResponse(['message' => 'You are not authorised to approve this deployment'], 403); |
||
193 | } |
||
194 | View Code Duplication | if ( |
|
195 | $deployment->State === DNDeployment::STATE_NEW |
||
196 | && !$this->project->allowed(self::ALLOW_APPROVAL_BYPASS, Member::currentUser()) |
||
197 | ) { |
||
198 | return $this->getAPIResponse(['message' => 'You are not authorised to bypass approval of this deployment'], 403); |
||
199 | } |
||
200 | |||
201 | try { |
||
202 | $deployment->getMachine()->apply(DNDeployment::TR_APPROVE); |
||
203 | } catch (\Exception $e) { |
||
204 | return $this->getAPIResponse([ |
||
205 | 'message' => $e->getMessage() |
||
206 | ], 400); |
||
207 | } |
||
208 | |||
209 | return $this->getAPIResponse([ |
||
210 | 'message' => 'Deployment request has been approved', |
||
211 | 'deployment' => $this->formatter->getDeploymentData($deployment) |
||
212 | ], 200); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @param \SS_HTTPRequest $request |
||
217 | * @return \SS_HTTPResponse |
||
218 | */ |
||
219 | public function reject(SS_HTTPRequest $request) { |
||
220 | if ($request->httpMethod() !== 'POST') { |
||
221 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
222 | } |
||
223 | |||
224 | $deployment = DNDeployment::get()->byId($request->postVar('id')); |
||
225 | $errorResponse = $this->validateDeployment($deployment); |
||
226 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
227 | return $errorResponse; |
||
228 | } |
||
229 | // reject permissions are the same as can approve |
||
230 | View Code Duplication | if (!$this->project->allowed(self::ALLOW_APPROVAL, Member::currentUser())) { |
|
231 | return $this->getAPIResponse(['message' => 'You are not authorised to reject this deployment'], 403); |
||
232 | } |
||
233 | |||
234 | try { |
||
235 | $deployment->getMachine()->apply(DNDeployment::TR_REJECT); |
||
236 | } catch (\Exception $e) { |
||
237 | return $this->getAPIResponse([ |
||
238 | 'message' => $e->getMessage() |
||
239 | ], 400); |
||
240 | } |
||
241 | |||
242 | return $this->getAPIResponse([ |
||
243 | 'message' => 'Deployment request has been rejected', |
||
244 | 'deployment' => $this->formatter->getDeploymentData($deployment) |
||
245 | ], 200); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
250 | * an APIResponse with the error, otherwise null. |
||
251 | * |
||
252 | * @param \DNDeployment $deployment |
||
253 | * |
||
254 | * @return null|SS_HTTPResponse |
||
255 | */ |
||
256 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
257 | if (!$deployment || !$deployment->exists()) { |
||
258 | return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
||
259 | } |
||
260 | if ($deployment->EnvironmentID != $this->environment->ID) { |
||
261 | return $this->getAPIResponse(['message' => 'This deployment does not belong to the environment'], 403); |
||
262 | } |
||
263 | if (!$deployment->canView()) { |
||
264 | return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); |
||
265 | } |
||
266 | return null; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $name |
||
271 | * @return array |
||
272 | */ |
||
273 | public function getModel($name = '') { |
||
276 | |||
277 | /** |
||
278 | * @param string $action |
||
279 | * @return string |
||
280 | */ |
||
281 | public function Link($action = '') { |
||
284 | |||
285 | } |
||
286 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.