Total Complexity | 114 |
Total Lines | 602 |
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('/Actions/RemoveGroupSignupLink', array($this, 'removeGroupLink')); |
||
18 | $app->post('/{shift}/Actions/Signup[/]', array($this, 'signup')); |
||
19 | $app->post('/{shift}/Actions/Abandon[/]', array($this, 'abandon')); |
||
20 | $app->post('/{shift}/Actions/Approve[/]', array($this, 'approvePending')); |
||
21 | $app->post('/{shift}/Actions/Disapprove[/]', array($this, 'disapprovePending')); |
||
22 | $app->post('/{shift}/Actions/StartGroupSignup', array($this, 'startGroupSignup')); |
||
23 | $app->post('/{shift}/Actions/GenerateGroupLink', array($this, 'generateGroupLink')); |
||
24 | $app->post('/{shift}/Actions/EmptyShift[/]', array($this, 'emptyShift')); |
||
25 | $app->post('/{shift}/Actions/ForceShiftEmpty[/]', array($this, 'forceEmpty')); |
||
26 | } |
||
27 | |||
28 | protected function canCreate($request) |
||
29 | { |
||
30 | //Check is handled by validateCreate... |
||
31 | return true; |
||
32 | } |
||
33 | |||
34 | protected function canUpdate($request, $entity) |
||
35 | { |
||
36 | if($this->isVolunteerAdmin($request)) |
||
37 | { |
||
38 | return true; |
||
39 | } |
||
40 | return $this->isUserDepartmentLead($entity['departmentID'], $this->user); |
||
41 | } |
||
42 | |||
43 | protected function canDelete($request, $entity) |
||
44 | { |
||
45 | return $this->canUpdate($request, $entity); |
||
46 | } |
||
47 | |||
48 | protected function validateCreate(&$obj, $request) |
||
49 | { |
||
50 | if($this->isVolunteerAdmin($request)) |
||
51 | { |
||
52 | return true; |
||
53 | } |
||
54 | if(!isset($obj['departmentID'])) |
||
55 | { |
||
56 | return false; |
||
57 | } |
||
58 | if(isset($obj['unbounded']) && $obj['unbounded']) |
||
59 | { |
||
60 | if(!isset($obj['minShifts']) || $obj['minShifts'] === 0 || $obj['minShifts'] === '') |
||
61 | { |
||
62 | $obj['minShifts'] = '1'; |
||
63 | } |
||
64 | } |
||
65 | return $this->isUserDepartmentLead($obj['departmentID'], $this->user); |
||
66 | } |
||
67 | |||
68 | protected function processEntry($entry, $request) |
||
69 | { |
||
70 | return $this->processShift($entry, $request); |
||
71 | } |
||
72 | |||
73 | protected function postUpdateAction($newObj, $request, $oldObj) |
||
74 | { |
||
75 | $oldShift = new \VolunteerShift(false, $oldObj); |
||
76 | if($oldShift->isFilled() && ($oldObj['startTime'] != $newObj['startTime'] || $oldObj['endTime'] != $newObj['endTime'])) |
||
77 | { |
||
78 | $email = new \Emails\ShiftEmail($oldShift, 'shiftChangedSource'); |
||
79 | $this->sendEmail($email); |
||
80 | } |
||
81 | return true; |
||
82 | } |
||
83 | |||
84 | protected function postDeleteAction($entry) |
||
85 | { |
||
86 | if(empty($entry)) |
||
87 | { |
||
88 | return true; |
||
89 | } |
||
90 | $shift = new \VolunteerShift(false, $entry[0]); |
||
91 | if($shift->isFilled()) |
||
92 | { |
||
93 | $email = new \Emails\ShiftEmail($shift, 'shiftCanceledSource'); |
||
94 | $this->sendEmail($email); |
||
95 | } |
||
96 | return true; |
||
97 | } |
||
98 | |||
99 | protected function genUUID() |
||
100 | { |
||
101 | return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
||
102 | // 32 bits for "time_low" |
||
103 | mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
||
104 | |||
105 | // 16 bits for "time_mid" |
||
106 | mt_rand(0, 0xffff), |
||
107 | |||
108 | // 16 bits for "time_hi_and_version", |
||
109 | // four most significant bits holds version number 4 |
||
110 | mt_rand(0, 0x0fff) | 0x4000, |
||
111 | |||
112 | // 16 bits, 8 bits for "clk_seq_hi_res", |
||
113 | // 8 bits for "clk_seq_low", |
||
114 | // two most significant bits holds zero and one for variant DCE1.1 |
||
115 | mt_rand(0, 0x3fff) | 0x8000, |
||
116 | |||
117 | // 48 bits for "node" |
||
118 | mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | public function createGroup($request, $response) |
||
123 | { |
||
124 | $array = $request->getParsedBody(); |
||
125 | $count = count($array); |
||
126 | $entArray = array(); |
||
127 | $uuid = $this->genUUID(); |
||
128 | $dataTable = $this->getDataTable(); |
||
129 | //User must be able to edit all shifts |
||
130 | for($i = 0; $i < $count; $i++) |
||
131 | { |
||
132 | $filter = $this->getFilterForPrimaryKey($array[$i]); |
||
133 | $entity = $dataTable->read($filter); |
||
134 | if($entity === false || !isset($entity[0])) |
||
135 | { |
||
136 | return $response->withStatus(404); |
||
137 | } |
||
138 | $entity = $entity[0]; |
||
139 | if(!$this->canUpdate($request, $entity)) |
||
140 | { |
||
141 | return $response->withStatus(401); |
||
142 | } |
||
143 | $entity['groupID'] = $uuid; |
||
144 | array_push($entArray, $entity); |
||
145 | } |
||
146 | //If we got here we can update them all |
||
147 | $myRet = true; |
||
148 | $errors = array(); |
||
149 | for($i = 0; $i < $count; $i++) |
||
150 | { |
||
151 | $filter = $this->getFilterForPrimaryKey($array[$i]); |
||
152 | $ret = $dataTable->update($filter, $entArray[$i]); |
||
153 | if($ret === false) |
||
154 | { |
||
155 | $myRet = false; |
||
156 | array_push($errors, $array[$i]); |
||
157 | } |
||
158 | } |
||
159 | if($myRet) |
||
160 | { |
||
161 | return $response->withJson($myRet); |
||
162 | } |
||
163 | else |
||
164 | { |
||
165 | return $response->withJson(array('res'=>$myRet, 'errors'=>$errors)); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public function newGroup($request, $response) |
||
170 | { |
||
171 | if(!$this->canCreate($request)) |
||
172 | { |
||
173 | return $response->withStatus(401); |
||
174 | } |
||
175 | $data = $request->getParsedBody(); |
||
176 | $shift = array(); |
||
177 | $shift['groupID'] = $this->genUUID(); |
||
178 | $shift['departmentID'] = $data['groupDepartmentID']; |
||
179 | $shift['earlyLate'] = $data['groupEarlyLate']; |
||
180 | $shift['enabled'] = $data['groupEnabled']; |
||
181 | $shift['approvalNeeded'] = $data['groupApprovalNeeded']; |
||
182 | $shift['endTime'] = $data['groupEndTime']; |
||
183 | $shift['eventID'] = $data['groupEvent']; |
||
184 | $shift['name'] = $data['groupName']; |
||
185 | $shift['startTime'] = $data['groupStartTime']; |
||
186 | $dataTable = $this->getDataTable(); |
||
187 | $ret = true; |
||
188 | foreach($data['roles'] as $role=>$count) |
||
189 | { |
||
190 | $count = intval($count); |
||
191 | for($i = 0; $i < $count; $i++) |
||
192 | { |
||
193 | $shift['roleID'] = $role; |
||
194 | if($dataTable->create($shift) === false) |
||
195 | { |
||
196 | $ret = false; |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | return $response->withJSON($ret); |
||
201 | } |
||
202 | |||
203 | public function deleteGroup($request, $response) |
||
204 | { |
||
205 | $data = $request->getParsedBody(); |
||
206 | $dataTable = $this->getDataTable(); |
||
207 | $filter = new \Data\Filter('groupID eq '.$data['groupID']); |
||
208 | $entities = $dataTable->read($filter); |
||
209 | if(empty($entities)) |
||
210 | { |
||
211 | return $response->withStatus(404); |
||
212 | } |
||
213 | if(!$this->canUpdate($request, $entities[0])) |
||
214 | { |
||
215 | return $response->withStatus(401); |
||
216 | } |
||
217 | $res = $dataTable->delete($filter); |
||
218 | if($res) |
||
219 | { |
||
220 | return $response->withJSON($res); |
||
221 | } |
||
222 | return $response->withJSON($res, 500); |
||
223 | } |
||
224 | |||
225 | protected function doSignup($uid, $status, $entity, $filter, $dataTable) |
||
226 | { |
||
227 | if(isset($entity['earlyLate']) && $entity['earlyLate'] !== '-1') |
||
228 | { |
||
229 | $event = new \VolunteerEvent($entity['eventID']); |
||
230 | if(!$event->hasVolOnEEList($uid, intval($entity['earlyLate']))) |
||
231 | { |
||
232 | $status = 'pending'; |
||
233 | $entity['needEEApproval'] = true; |
||
234 | $event->addToEEList($uid, intval($entity['earlyLate'])); |
||
235 | } |
||
236 | } |
||
237 | else if(isset($entity['approvalNeeded']) && $entity['approvalNeeded']) |
||
238 | { |
||
239 | $status = 'pending'; |
||
240 | } |
||
241 | $entity['participant'] = $uid; |
||
242 | $entity['status'] = $status; |
||
243 | $entity['signupTime'] = date('c'); |
||
244 | return $dataTable->update($filter, $entity); |
||
245 | } |
||
246 | |||
247 | public function signup($request, $response, $args) |
||
248 | { |
||
249 | $this->validateLoggedIn($request); |
||
250 | $shiftId = $args['shift']; |
||
251 | $dataTable = $this->getDataTable(); |
||
252 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
253 | $entity = $dataTable->read($filter); |
||
254 | if(empty($entity)) |
||
255 | { |
||
256 | return $response->withStatus(404); |
||
257 | } |
||
258 | $entity = $entity[0]; |
||
259 | if(isset($entity['participant']) && strlen($entity['participant']) > 0) |
||
260 | { |
||
261 | return $response->withStatus(401); |
||
262 | } |
||
263 | $shift = new \VolunteerShift($shiftId, $entity); |
||
264 | $entity = $this->processShift($entity, $request); |
||
265 | if(isset($entity['minShifts']) && $entity['minShifts'] > 0) |
||
266 | { |
||
267 | $shift->makeCopy($dataTable); |
||
268 | } |
||
269 | if(isset($entity['overlap']) && $entity['overlap']) |
||
270 | { |
||
271 | $overlaps = $shift->findOverlaps($this->user->uid); |
||
272 | $count = count($overlaps); |
||
273 | $leads = array(); |
||
274 | for($i = 0; $i < $count; $i++) |
||
275 | { |
||
276 | $dept = new \VolunteerDepartment($overlaps[$i]->departmentID); |
||
277 | $leads = array_merge($leads, $dept->getLeadEmails()); |
||
278 | $overlaps[$i]->status = 'pending'; |
||
279 | $tmp = new \Data\Filter('_id eq '.$overlaps[$i]->{'_id'}); |
||
280 | $res = $dataTable->update($tmp, $overlaps[$i]); |
||
281 | if($res === false) |
||
282 | { |
||
283 | return $response->withJSON(array('err'=>'Unable to update overlap with id '.$overlaps[$i]->{'_id'})); |
||
284 | } |
||
285 | } |
||
286 | $dept = new \VolunteerDepartment($entity['departmentID']); |
||
287 | $leads = array_merge($leads, $dept->getLeadEmails()); |
||
288 | $leads = array_unique($leads); |
||
289 | $ret = $this->doSignup($this->user->uid, 'pending', $entity, $filter, $dataTable); |
||
290 | $profile = new \VolunteerProfile($this->user->uid); |
||
291 | $email = new \Emails\TwoShiftsAtOnceEmail($profile); |
||
292 | $email->addLeads($leads); |
||
293 | $emailProvider = \EmailProvider::getInstance(); |
||
294 | if($emailProvider->sendEmail($email) === false) |
||
295 | { |
||
296 | throw new \Exception('Unable to send duplicate email!'); |
||
297 | } |
||
298 | return $response->withJSON($ret); |
||
299 | } |
||
300 | if(isset($entity['available']) && $entity['available']) |
||
301 | { |
||
302 | $ret = $this->doSignup($this->user->uid, 'filled', $entity, $filter, $dataTable); |
||
303 | return $response->withJSON($ret); |
||
304 | } |
||
305 | if(isset($entity['status']) && $entity['status'] === 'groupPending') |
||
306 | { |
||
307 | $ret = $this->doSignup($this->user->uid, 'filled', $entity, $filter, $dataTable); |
||
308 | return $response->withJSON($ret); |
||
309 | } |
||
310 | print_r($entity); die(); |
||
311 | } |
||
312 | |||
313 | public function abandon($request, $response, $args) |
||
314 | { |
||
315 | $this->validateLoggedIn($request); |
||
316 | $shiftId = $args['shift']; |
||
317 | $dataTable = $this->getDataTable(); |
||
318 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
319 | $entity = $dataTable->read($filter); |
||
320 | if(empty($entity)) |
||
321 | { |
||
322 | return $response->withStatus(404); |
||
323 | } |
||
324 | $entity = $entity[0]; |
||
325 | if(!isset($entity['participant']) || $entity['participant'] !== $this->user->uid) |
||
326 | { |
||
327 | return $response->withStatus(401); |
||
328 | } |
||
329 | $entity['participant'] = ''; |
||
330 | $entity['status'] = 'unfilled'; |
||
331 | $entity['signupTime'] = ''; |
||
332 | if(isset($entity['needEEApproval'])) |
||
333 | { |
||
334 | unset($entity['needEEApproval']); |
||
335 | } |
||
336 | return $response->withJSON($dataTable->update($filter, $entity)); |
||
337 | } |
||
338 | |||
339 | public function approvePending($request, $response, $args) |
||
356 | } |
||
357 | |||
358 | public function disapprovePending($request, $response, $args) |
||
359 | { |
||
360 | if(!$this->canCreate($request)) |
||
361 | { |
||
362 | return $response->withStatus(401); |
||
363 | } |
||
364 | $shiftId = $args['shift']; |
||
365 | $dataTable = $this->getDataTable(); |
||
366 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
367 | $entity = $dataTable->read($filter); |
||
368 | if(empty($entity)) |
||
369 | { |
||
370 | return $response->withStatus(404); |
||
371 | } |
||
372 | $entity['participant'] = ''; |
||
373 | $entity['status'] = 'unfilled'; |
||
374 | $profile = new \VolunteerProfile($this->user->uid); |
||
375 | $email = new \Emails\PendingRejectedEmail($profile); |
||
376 | $email->setShift($entity); |
||
377 | $this->sendEmail($email); |
||
378 | return $response->withJSON($dataTable->update($filter, $entity)); |
||
379 | } |
||
380 | |||
381 | public function startGroupSignup($request, $response, $args) |
||
382 | { |
||
383 | $this->validateLoggedIn($request); |
||
384 | $shiftId = $args['shift']; |
||
385 | $dataTable = $this->getDataTable(); |
||
386 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
387 | $entity = $dataTable->read($filter); |
||
388 | if(empty($entity)) |
||
389 | { |
||
390 | return $response->withStatus(404); |
||
391 | } |
||
392 | $entity = $entity[0]; |
||
393 | if(isset($entity['participant']) && strlen($entity['participant']) > 0) |
||
394 | { |
||
395 | return $response->withStatus(401); |
||
396 | } |
||
397 | $filter = new \Data\Filter('groupID eq '.$entity['groupID'].' and enabled eq true'); |
||
398 | $entities = $dataTable->read($filter); |
||
399 | $count = count($entities); |
||
400 | $dept = new \VolunteerDepartment($entity['departmentID']); |
||
401 | $res = array(); |
||
402 | $res['department'] = $dept->departmentName; |
||
403 | $res['earlyLate'] = $entity['earlyLate']; |
||
404 | $res['endTime'] = $entity['endTime']; |
||
405 | $res['eventID'] = $entity['eventID']; |
||
406 | $res['name'] = $entity['name']; |
||
407 | $res['startTime'] = $entity['startTime']; |
||
408 | $res['groupID'] = $entity['groupID']; |
||
409 | $res['shifts'] = array(); |
||
410 | $roles = array(); |
||
411 | for($i = 0; $i < $count; $i++) |
||
412 | { |
||
413 | if(isset($entities[$i]['status']) && ($entities[$i]['status'] === 'filled' || $entities[$i]['status'] === 'pending')) |
||
414 | { |
||
415 | continue; |
||
416 | } |
||
417 | if(!isset($roles[$entities[$i]['roleID']])) |
||
418 | { |
||
419 | $roles[$entities[$i]['roleID']] = new \VolunteerRole($entities[$i]['roleID']); |
||
420 | } |
||
421 | $role = $roles[$entities[$i]['roleID']]; |
||
422 | $entities[$i]['role'] = $role->display_name; |
||
423 | array_push($res['shifts'], $entities[$i]); |
||
424 | } |
||
425 | return $response->withJSON($res); |
||
426 | } |
||
427 | |||
428 | public function generateGroupLink($request, $response, $args) |
||
508 | } |
||
509 | |||
510 | public function removeGroupLink($request, $response, $args) |
||
547 | } |
||
548 | |||
549 | function emptyShift($request, $response, $args) |
||
550 | { |
||
551 | $this->validateLoggedIn($request); |
||
552 | $shiftId = $args['shift']; |
||
553 | $dataTable = $this->getDataTable(); |
||
554 | $filter = $this->getFilterForPrimaryKey($shiftId); |
||
555 | $entity = $dataTable->read($filter); |
||
556 | if(empty($entity)) |
||
557 | { |
||
558 | return $response->withStatus(404); |
||
559 | } |
||
560 | $entity = $entity[0]; |
||
561 | if(!$this->canUpdate($request, $entity)) |
||
562 | { |
||
563 | return $response->withStatus(401); |
||
564 | } |
||
565 | $shift = new \VolunteerShift(false, $entity); |
||
566 | $entity['participant'] = ''; |
||
567 | $entity['status'] = 'unfilled'; |
||
568 | if(isset($entity['needEEApproval'])) |
||
569 | { |
||
570 | unset($entity['needEEApproval']); |
||
571 | } |
||
572 | $ret = $dataTable->update($filter, $entity); |
||
573 | if($ret) |
||
574 | { |
||
575 | $email = new \Emails\ShiftEmail($shift, 'shiftEmptiedSource'); |
||
576 | $this->sendEmail($email); |
||
577 | } |
||
578 | return $response->withJSON($ret); |
||
579 | } |
||
580 | |||
581 | function forceEmpty($request, $response, $args) |
||
604 | } |
||
605 | } |
||
606 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
607 |