Total Complexity | 105 |
Total Lines | 551 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 0 | Features | 0 |
Complex classes like ShiftAPI 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.
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 ShiftAPI, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
2 | class ShiftAPI extends VolunteerAPI |
||
3 | { |
||
4 | use Processor; |
||
|
|||
5 | |||
6 | public function __construct() |
||
7 | { |
||
8 | parent::__construct('shifts'); |
||
9 | } |
||
10 | |||
11 | public function setup($app) |
||
12 | { |
||
13 | parent::setup($app); |
||
14 | $app->post('/Actions/CreateGroup', array($this, 'createGroup')); |
||
15 | $app->post('/Actions/NewGroup', array($this, 'newGroup')); |
||
16 | $app->post('/Actions/DeleteGroup', array($this, 'deleteGroup')); |
||
17 | $app->post('/{shift}/Actions/Signup[/]', array($this, 'signup')); |
||
18 | $app->post('/{shift}/Actions/Abandon[/]', array($this, 'abandon')); |
||
19 | $app->post('/{shift}/Actions/Approve[/]', array($this, 'approvePending')); |
||
20 | $app->post('/{shift}/Actions/Disapprove[/]', array($this, 'disapprovePending')); |
||
21 | $app->post('/{shift}/Actions/StartGroupSignup', array($this, 'startGroupSignup')); |
||
22 | $app->post('/{shift}/Actions/GenerateGroupLink', array($this, 'generateGroupLink')); |
||
23 | $app->post('/{shift}/Actions/EmptyShift[/]', array($this, 'emptyShift')); |
||
24 | $app->post('/{shift}/Actions/ForceShiftEmpty[/]', array($this, 'forceEmpty')); |
||
25 | } |
||
26 | |||
27 | protected function canCreate($request) |
||
28 | { |
||
29 | //Check is handled by validateCreate... |
||
30 | return true; |
||
31 | } |
||
32 | |||
33 | protected function canUpdate($request, $entity) |
||
34 | { |
||
35 | if($this->isVolunteerAdmin($request)) |
||
36 | { |
||
37 | return true; |
||
38 | } |
||
39 | return $this->isUserDepartmentLead($entity['departmentID'], $this->user); |
||
40 | } |
||
41 | |||
42 | protected function canDelete($request, $entity) |
||
45 | } |
||
46 | |||
47 | protected function validateCreate(&$obj, $request) |
||
65 | } |
||
66 | |||
67 | protected function processEntry($entry, $request) |
||
68 | { |
||
70 | } |
||
71 | |||
72 | protected function postUpdateAction($newObj, $request, $oldObj) |
||
73 | { |
||
74 | $oldShift = new \VolunteerShift(false, $oldObj); |
||
75 | if($oldShift->isFilled() && ($oldObj['startTime'] != $newObj['startTime'] || $oldObj['endTime'] != $newObj['endTime'])) |
||
76 | { |
||
77 | $email = new \Emails\ShiftEmail($oldShift, 'shiftChangedSource'); |
||
78 | $this->sendEmail($email); |
||
79 | } |
||
80 | return true; |
||
81 | } |
||
82 | |||
83 | protected function postDeleteAction($entry) |
||
84 | { |
||
85 | if(empty($entry)) |
||
86 | { |
||
87 | return true; |
||
88 | } |
||
89 | $shift = new \VolunteerShift(false, $entry[0]); |
||
90 | if($shift->isFilled()) |
||
91 | { |
||
92 | $email = new \Emails\ShiftEmail($shift, 'shiftCanceledSource'); |
||
93 | $this->sendEmail($email); |
||
94 | } |
||
95 | return true; |
||
96 | } |
||
97 | |||
98 | protected function genUUID() |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | public function createGroup($request, $response) |
||
122 | { |
||
123 | $array = $request->getParsedBody(); |
||
124 | $count = count($array); |
||
125 | $entArray = array(); |
||
126 | $uuid = $this->genUUID(); |
||
127 | $dataTable = $this->getDataTable(); |
||
128 | //User must be able to edit all shifts |
||
129 | for($i = 0; $i < $count; $i++) |
||
130 | { |
||
131 | $filter = $this->getFilterForPrimaryKey($array[$i]); |
||
132 | $entity = $dataTable->read($filter); |
||
133 | if($entity === false || !isset($entity[0])) |
||
134 | { |
||
135 | return $response->withStatus(404); |
||
136 | } |
||
137 | $entity = $entity[0]; |
||
138 | if(!$this->canUpdate($request, $entity)) |
||
139 | { |
||
140 | return $response->withStatus(401); |
||
141 | } |
||
142 | $entity['groupID'] = $uuid; |
||
143 | array_push($entArray, $entity); |
||
144 | } |
||
145 | //If we got here we can update them all |
||
146 | $myRet = true; |
||
147 | $errors = array(); |
||
148 | for($i = 0; $i < $count; $i++) |
||
149 | { |
||
150 | $filter = $this->getFilterForPrimaryKey($array[$i]); |
||
151 | $ret = $dataTable->update($filter, $entArray[$i]); |
||
152 | if($ret === false) |
||
153 | { |
||
154 | $myRet = false; |
||
155 | array_push($errors, $array[$i]); |
||
156 | } |
||
157 | } |
||
158 | if($myRet) |
||
159 | { |
||
160 | return $response->withJson($myRet); |
||
161 | } |
||
162 | else |
||
163 | { |
||
164 | return $response->withJson(array('res'=>$myRet, 'errors'=>$errors)); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | public function newGroup($request, $response) |
||
169 | { |
||
170 | if(!$this->canCreate($request)) |
||
171 | { |
||
172 | return $response->withStatus(401); |
||
173 | } |
||
174 | $data = $request->getParsedBody(); |
||
175 | $shift = array(); |
||
176 | $shift['groupID'] = $this->genUUID(); |
||
177 | $shift['departmentID'] = $data['groupDepartmentID']; |
||
178 | $shift['earlyLate'] = $data['groupEarlyLate']; |
||
179 | $shift['enabled'] = $data['groupEnabled']; |
||
180 | $shift['endTime'] = $data['groupEndTime']; |
||
181 | $shift['eventID'] = $data['groupEvent']; |
||
182 | $shift['name'] = $data['groupName']; |
||
183 | $shift['startTime'] = $data['groupStartTime']; |
||
184 | $dataTable = $this->getDataTable(); |
||
185 | $ret = true; |
||
186 | foreach($data['roles'] as $role=>$count) |
||
187 | { |
||
188 | $count = intval($count); |
||
189 | for($i = 0; $i < $count; $i++) |
||
190 | { |
||
191 | $shift['roleID'] = $role; |
||
192 | if($dataTable->create($shift) === false) |
||
193 | { |
||
194 | $ret = false; |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | return $response->withJSON($ret); |
||
199 | } |
||
200 | |||
201 | public function deleteGroup($request, $response) |
||
202 | { |
||
203 | $data = $request->getParsedBody(); |
||
204 | $dataTable = $this->getDataTable(); |
||
205 | $filter = new \Data\Filter('groupID eq '.$data['groupID']); |
||
206 | $entities = $dataTable->read($filter); |
||
207 | if(empty($entities)) |
||
208 | { |
||
209 | return $response->withStatus(404); |
||
210 | } |
||
211 | if(!$this->canUpdate($request, $entities[0])) |
||
212 | { |
||
213 | return $response->withStatus(401); |
||
214 | } |
||
215 | $res = $dataTable->delete($filter); |
||
216 | if($res) |
||
217 | { |
||
218 | return $response->withJSON($res); |
||
219 | } |
||
220 | return $response->withJSON($res, 500); |
||
221 | } |
||
222 | |||
223 | protected function doSignup($uid, $status, $entity, $filter, $dataTable) |
||
224 | { |
||
225 | if(isset($entity['earlyLate']) && $entity['earlyLate'] !== '-1') |
||
226 | { |
||
227 | $event = new \VolunteerEvent($entity['eventID']); |
||
228 | if(!$event->hasVolOnEEList($uid, intval($entity['earlyLate']))) |
||
229 | { |
||
230 | $status = 'pending'; |
||
231 | $entity['needEEApproval'] = true; |
||
232 | $event->addToEEList($uid, intval($entity['earlyLate'])); |
||
233 | } |
||
234 | } |
||
235 | $entity['participant'] = $uid; |
||
236 | $entity['status'] = $status; |
||
237 | return $dataTable->update($filter, $entity); |
||
238 | } |
||
239 | |||
240 | public function signup($request, $response, $args) |
||
241 | { |
||
242 | $this->validateLoggedIn($request); |
||
243 | $shiftId = $args['shift']; |
||
244 | $dataTable = $this->getDataTable(); |
||
245 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
246 | $entity = $dataTable->read($filter); |
||
247 | if(empty($entity)) |
||
248 | { |
||
249 | return $response->withStatus(404); |
||
250 | } |
||
251 | $entity = $entity[0]; |
||
252 | if(isset($entity['participant']) && strlen($entity['participant']) > 0) |
||
253 | { |
||
254 | return $response->withStatus(401); |
||
255 | } |
||
256 | $shift = new \VolunteerShift($shiftId, $entity); |
||
257 | $entity = $this->processShift($entity, $request); |
||
258 | if(isset($entity['minShifts']) && $entity['minShifts'] > 0) |
||
259 | { |
||
260 | $shift->makeCopy($dataTable); |
||
261 | } |
||
262 | if(isset($entity['overlap']) && $entity['overlap']) |
||
263 | { |
||
264 | $overlaps = $shift->findOverlaps($this->user->uid); |
||
265 | $count = count($overlaps); |
||
266 | $leads = array(); |
||
267 | for($i = 0; $i < $count; $i++) |
||
268 | { |
||
269 | $dept = new \VolunteerDepartment($overlaps[$i]->departmentID); |
||
270 | $leads = array_merge($leads, $dept->getLeadEmails()); |
||
271 | $overlaps[$i]->status = 'pending'; |
||
272 | $tmp = new \Data\Filter('_id eq '.$overlaps[$i]->{'_id'}); |
||
273 | $res = $dataTable->update($tmp, $overlaps[$i]); |
||
274 | if($res === false) |
||
275 | { |
||
276 | return $response->withJSON(array('err'=>'Unable to update overlap with id '.$overlaps[$i]->{'_id'})); |
||
277 | } |
||
278 | } |
||
279 | $dept = new \VolunteerDepartment($entity['departmentID']); |
||
280 | $leads = array_merge($leads, $dept->getLeadEmails()); |
||
281 | $leads = array_unique($leads); |
||
282 | $ret = $this->doSignup($this->user->uid, 'pending', $entity, $filter, $dataTable); |
||
283 | $profile = new \VolunteerProfile($this->user->uid); |
||
284 | $email = new \Emails\TwoShiftsAtOnceEmail($profile); |
||
285 | $email->addLeads($leads); |
||
286 | $emailProvider = \EmailProvider::getInstance(); |
||
287 | if($emailProvider->sendEmail($email) === false) |
||
288 | { |
||
289 | throw new \Exception('Unable to send duplicate email!'); |
||
290 | } |
||
291 | return $response->withJSON($ret); |
||
292 | } |
||
293 | if(isset($entity['available']) && $entity['available']) |
||
294 | { |
||
295 | $ret = $this->doSignup($this->user->uid, 'filled', $entity, $filter, $dataTable); |
||
296 | return $response->withJSON($ret); |
||
297 | } |
||
298 | if(isset($entity['status']) && $entity['status'] === 'groupPending') |
||
299 | { |
||
300 | $ret = $this->doSignup($this->user->uid, 'filled', $entity, $filter, $dataTable); |
||
301 | return $response->withJSON($ret); |
||
302 | } |
||
303 | print_r($entity); die(); |
||
304 | } |
||
305 | |||
306 | public function abandon($request, $response, $args) |
||
307 | { |
||
308 | $this->validateLoggedIn($request); |
||
309 | $shiftId = $args['shift']; |
||
310 | $dataTable = $this->getDataTable(); |
||
311 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
312 | $entity = $dataTable->read($filter); |
||
313 | if(empty($entity)) |
||
314 | { |
||
315 | return $response->withStatus(404); |
||
316 | } |
||
317 | $entity = $entity[0]; |
||
318 | if(!isset($entity['participant']) || $entity['participant'] !== $this->user->uid) |
||
319 | { |
||
320 | return $response->withStatus(401); |
||
321 | } |
||
322 | $entity['participant'] = ''; |
||
323 | $entity['status'] = 'unfilled'; |
||
324 | if(isset($entity['needEEApproval'])) |
||
325 | { |
||
326 | unset($entity['needEEApproval']); |
||
327 | } |
||
328 | return $response->withJSON($dataTable->update($filter, $entity)); |
||
329 | } |
||
330 | |||
331 | public function approvePending($request, $response, $args) |
||
332 | { |
||
333 | if(!$this->canCreate($request)) |
||
334 | { |
||
335 | return $response->withStatus(401); |
||
336 | } |
||
337 | $shiftId = $args['shift']; |
||
338 | $dataTable = $this->getDataTable(); |
||
339 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
340 | $entity = $dataTable->read($filter); |
||
341 | if(empty($entity)) |
||
342 | { |
||
343 | return $response->withStatus(404); |
||
344 | } |
||
345 | $entity = $entity[0]; |
||
346 | $entity['status'] = 'filled'; |
||
347 | return $response->withJSON($dataTable->update($filter, $entity)); |
||
348 | } |
||
349 | |||
350 | public function disapprovePending($request, $response, $args) |
||
351 | { |
||
352 | if(!$this->canCreate($request)) |
||
353 | { |
||
354 | return $response->withStatus(401); |
||
355 | } |
||
356 | $shiftId = $args['shift']; |
||
357 | $dataTable = $this->getDataTable(); |
||
358 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
359 | $entity = $dataTable->read($filter); |
||
360 | if(empty($entity)) |
||
361 | { |
||
362 | return $response->withStatus(404); |
||
363 | } |
||
364 | $entity['participant'] = ''; |
||
365 | $entity['status'] = 'unfilled'; |
||
366 | $profile = new \VolunteerProfile($this->user->uid); |
||
367 | $email = new \Emails\PendingRejectedEmail($profile); |
||
368 | $email->setShift($entity); |
||
369 | $this->sendEmail($email); |
||
370 | return $response->withJSON($dataTable->update($filter, $entity)); |
||
371 | } |
||
372 | |||
373 | public function startGroupSignup($request, $response, $args) |
||
374 | { |
||
375 | $this->validateLoggedIn($request); |
||
376 | $shiftId = $args['shift']; |
||
377 | $dataTable = $this->getDataTable(); |
||
378 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
379 | $entity = $dataTable->read($filter); |
||
380 | if(empty($entity)) |
||
381 | { |
||
382 | return $response->withStatus(404); |
||
383 | } |
||
384 | $entity = $entity[0]; |
||
385 | if(isset($entity['participant']) && strlen($entity['participant']) > 0) |
||
386 | { |
||
387 | return $response->withStatus(401); |
||
388 | } |
||
389 | $filter = new \Data\Filter('groupID eq '.$entity['groupID'].' and enabled eq true'); |
||
390 | $entities = $dataTable->read($filter); |
||
391 | $count = count($entities); |
||
392 | $dept = new \VolunteerDepartment($entity['departmentID']); |
||
393 | $res = array(); |
||
394 | $res['department'] = $dept->departmentName; |
||
395 | $res['earlyLate'] = $entity['earlyLate']; |
||
396 | $res['endTime'] = $entity['endTime']; |
||
397 | $res['eventID'] = $entity['eventID']; |
||
398 | $res['name'] = $entity['name']; |
||
399 | $res['startTime'] = $entity['startTime']; |
||
400 | $res['groupID'] = $entity['groupID']; |
||
401 | $res['shifts'] = array(); |
||
402 | $roles = array(); |
||
403 | for($i = 0; $i < $count; $i++) |
||
404 | { |
||
405 | if(isset($entities[$i]['status']) && ($entities[$i]['status'] === 'filled' || $entities[$i]['status'] === 'pending')) |
||
406 | { |
||
407 | continue; |
||
408 | } |
||
409 | if(!isset($roles[$entities[$i]['roleID']])) |
||
410 | { |
||
411 | $roles[$entities[$i]['roleID']] = new \VolunteerRole($entities[$i]['roleID']); |
||
412 | } |
||
413 | $role = $roles[$entities[$i]['roleID']]; |
||
414 | $entities[$i]['role'] = $role->display_name; |
||
415 | array_push($res['shifts'], $entities[$i]); |
||
416 | } |
||
417 | return $response->withJSON($res); |
||
418 | } |
||
419 | |||
420 | public function generateGroupLink($request, $response, $args) |
||
496 | } |
||
497 | |||
498 | function emptyShift($request, $response, $args) |
||
499 | { |
||
500 | $this->validateLoggedIn($request); |
||
501 | $shiftId = $args['shift']; |
||
502 | $dataTable = $this->getDataTable(); |
||
503 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
504 | $entity = $dataTable->read($filter); |
||
505 | if(empty($entity)) |
||
506 | { |
||
507 | return $response->withStatus(404); |
||
508 | } |
||
509 | $entity = $entity[0]; |
||
510 | if(!$this->canUpdate($request, $entity)) |
||
511 | { |
||
512 | return $response->withStatus(401); |
||
513 | } |
||
514 | $shift = new \VolunteerShift(false, $entity); |
||
515 | $entity['participant'] = ''; |
||
516 | $entity['status'] = 'unfilled'; |
||
517 | if(isset($entity['needEEApproval'])) |
||
518 | { |
||
519 | unset($entity['needEEApproval']); |
||
520 | } |
||
521 | $ret = $dataTable->update($filter, $entity); |
||
522 | if($ret) |
||
523 | { |
||
524 | $email = new \Emails\ShiftEmail($shift, 'shiftEmptiedSource'); |
||
525 | $this->sendEmail($email); |
||
526 | } |
||
527 | return $response->withJSON($ret); |
||
528 | } |
||
529 | |||
530 | function forceEmpty($request, $response, $args) |
||
553 | } |
||
554 | } |
||
555 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
556 |