ShiftAPI   F
last analyzed

Complexity

Total Complexity 114

Size/Duplication

Total Lines 602
Duplicated Lines 0 %

Importance

Changes 15
Bugs 0 Features 0
Metric Value
eloc 348
c 15
b 0
f 0
dl 0
loc 602
rs 2
wmc 114

23 Methods

Rating   Name   Duplication   Size   Complexity  
A approvePending() 0 17 3
A __construct() 0 3 1
A genUUID() 0 20 1
A postDeleteAction() 0 13 3
A canDelete() 0 3 1
A deleteGroup() 0 20 4
A canCreate() 0 4 1
B validateCreate() 0 18 8
B createGroup() 0 44 8
A setup() 0 15 1
A canUpdate() 0 7 2
A newGroup() 0 32 5
A postUpdateAction() 0 9 4
A processEntry() 0 3 1
C generateGroupLink() 0 80 17
C signup() 0 64 15
A abandon() 0 24 5
B removeGroupLink() 0 37 7
A forceEmpty() 0 23 4
A emptyShift() 0 30 5
B startGroupSignup() 0 45 9
A doSignup() 0 20 6
A disapprovePending() 0 21 3

How to fix   Complexity   

Complex Class

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;
0 ignored issues
show
introduced by
The trait Processor requires some properties which are not provided by ShiftAPI: $startTime, $certs, $endTime, $mail, $uid, $title
Loading history...
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)
0 ignored issues
show
introduced by
The condition $myRet is always true.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $overlaps can also be of type true; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

272
            $count = count(/** @scrutinizer ignore-type */ $overlaps);
Loading history...
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();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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)
340
    {
341
        if(!$this->canCreate($request))
342
        {
343
            return $response->withStatus(401);
344
        }
345
        $shiftId = $args['shift'];
346
        $dataTable = $this->getDataTable();
347
        $filter = $this->getFilterForPrimaryKey($shiftId);
348
        $entity = $dataTable->read($filter);
349
        if(empty($entity))
350
        {
351
            return $response->withStatus(404);
352
        }
353
        $entity = $entity[0];
354
        $entity['status'] = 'filled';
355
        return $response->withJSON($dataTable->update($filter, $entity));
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);
0 ignored issues
show
Bug introduced by
It seems like $entities can also be of type boolean; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

399
        $count = count(/** @scrutinizer ignore-type */ $entities);
Loading history...
400
        $dept = new \VolunteerDepartment($entity['departmentID']);
401
        $res = array();
402
        $res['department'] = $dept->departmentName;
0 ignored issues
show
Bug Best Practice introduced by
The property departmentName does not exist on VolunteerDepartment. Since you implemented __get, consider adding a @property annotation.
Loading history...
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)
429
    {
430
        $this->validateLoggedIn($request);
431
        $shiftId = $args['shift'];
432
        $dataTable = $this->getDataTable();
433
        $filter = $this->getFilterForPrimaryKey($shiftId);
434
        $entity = $dataTable->read($filter);
435
        if(empty($entity))
436
        {
437
            return $response->withStatus(404);
438
        }
439
        $entity = $entity[0];
440
        if(isset($entity['participant']) && strlen($entity['participant']) > 0)
441
        {
442
            return $response->withStatus(401);
443
        }
444
        $data = $request->getParsedBody();
445
        $myShift = $data['myshift'];
446
        $roles = array();
447
        foreach($data as $key => $value)
448
        {
449
            if(substr($key, 0, 6) === "roles.")
450
            {
451
                $roles[substr($key, 6)] = $value;
452
            }
453
        }
454
        $filter = new \Data\Filter('groupID eq '.$entity['groupID'].' and enabled eq true');
455
        $entities = $dataTable->read($filter);
456
        $count = count($entities);
0 ignored issues
show
Bug introduced by
It seems like $entities can also be of type boolean; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

456
        $count = count(/** @scrutinizer ignore-type */ $entities);
Loading history...
457
        $uuid = $this->genUUID();
458
        $time = date('c');
459
        for($i = 0; $i < $count; $i++)
460
        {
461
            if(isset($entities[$i]['status']) && ($entities[$i]['status'] === 'filled' || $entities[$i]['status'] === 'pending'))
462
            {
463
                $entities[$i] = false;
464
                continue;
465
            }
466
            if((string)$entities[$i]['_id'] === (string)new \MongoDB\BSON\ObjectId($myShift))
467
            {
468
                $entities[$i]['participant'] = $this->user->uid;
469
                $entities[$i]['status'] = 'filled';
470
                $entities[$i]['signupLink'] = $uuid;
471
                $entities[$i]['groupLinkCreated'] = $time;
472
                $entities[$i]['signupOn'] = $time;
473
            }
474
            else if(isset($roles[$entities[$i]['roleID']]))
475
            {
476
                $entities[$i]['status'] = 'groupPending';
477
                $entities[$i]['signupLink'] = $uuid;
478
                $entities[$i]['groupLinkCreated'] = $time;
479
                $roles[$entities[$i]['roleID']]--;
480
                if($roles[$entities[$i]['roleID']] === 0)
481
                {
482
                    unset($roles[$entities[$i]['roleID']]);
483
                }
484
            }
485
            else
486
            {
487
                $entities[$i] = false;
488
            }
489
        }
490
        if(count($roles) !== 0)
491
        {
492
            throw new \Exception('Not enough shifts to fullfill requests');
493
        }
494
        for($i = 0; $i < $count; $i++)
495
        {
496
            if($entities[$i] === false)
497
            {
498
                continue;
499
            }
500
            $filter = new \Data\Filter('_id eq '.$entities[$i]['_id']);
501
            $res = $dataTable->update($filter, $entities[$i]);
502
            if($res === false)
503
            {
504
                throw new \Exception('Not able to save shift '.$entities[$i]['_id']);
505
            }
506
        }
507
        return $response->withJSON(array('uuid' => $uuid));
508
    }
509
510
    public function removeGroupLink($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

510
    public function removeGroupLink($request, $response, /** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
511
    {
512
        if(!$this->canCreate($request))
513
        {
514
            return $response->withStatus(401);
515
        }
516
        $data = $this->getParsedBody($request);
517
        if(!isset($data['groupID']))
518
        {
519
            return $response->withStatus(400);
520
        }
521
        $groupID = $data['groupID'];
522
        $dataTable = $this->getDataTable();
523
        $filter = new \Data\Filter("groupID eq '$groupID'");
524
        $entity = $dataTable->read($filter);
525
        if(empty($entity))
526
        {
527
            return $response->withStatus(404);
528
        }
529
        $count = count($entity);
0 ignored issues
show
Bug introduced by
It seems like $entity can also be of type boolean; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

529
        $count = count(/** @scrutinizer ignore-type */ $entity);
Loading history...
530
        $res = true;
531
        for($i = 0; $i < $count; $i++)
532
        {
533
            $entity[$i]['signupLink'] = '';
534
            if($entity[$i]['status'] === 'groupPending')
535
            {
536
                $entity['status'] = 'unfilled';
537
            }
538
            $entity[$i]['groupLinkCreated'] = '';
539
            $upFilter = new \Data\Filter('_id eq '.$entity[$i]['_id']);
540
            $tmp = $dataTable->update($upFilter, $entity[$i]);
541
            if($tmp === false)
542
            {
543
                $res = false;
544
            }
545
        }
546
        return $response->withJSON($res);
547
    }
548
549
    function emptyShift($request, $response, $args)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
582
    {
583
        $this->validateLoggedIn($request);
584
        $shiftId = $args['shift'];
585
        $dataTable = $this->getDataTable();
586
        $filter = $this->getFilterForPrimaryKey($shiftId);
587
        $entity = $dataTable->read($filter);
588
        if(empty($entity))
589
        {
590
            return $response->withStatus(404);
591
        }
592
        $entity = $entity[0];
593
        if(!$this->canUpdate($request, $entity))
594
        {
595
            return $response->withStatus(401);
596
        }
597
        $entity['participant'] = '';
598
        $entity['status'] = 'unfilled';
599
        if(isset($entity['needEEApproval']))
600
        {
601
          unset($entity['needEEApproval']);
602
        }
603
        return $response->withJSON($dataTable->update($filter, $entity));
604
    }
605
}
606
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
607