Passed
Push — master ( d7e703...d0dd1b )
by Patrick
09:55
created

ParticipantAPI::rejectCert()   B

Complexity

Conditions 11
Paths 23

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 11
eloc 34
c 2
b 0
f 1
nc 23
nop 3
dl 0
loc 50
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
class ParticipantAPI extends VolunteerAPI
3
{
4
    public function __construct()
5
    {
6
        parent::__construct('participants', 'uid');
7
    }
8
9
    public function setup($app)
10
    {
11
        parent::setup($app);
12
        $app->get('/me/shifts[/]', array($this, 'getMyShifts'));
13
        $app->get('/{uid}/certs[/]', array($this, 'getCerts'));
14
        $app->post('/{uid}/certs/{certId}[/]', array($this, 'uploadCert'));
15
        $app->post('/{uid}/certs/{certId}/Actions/RejectCert', array($this, 'rejectCert'));
16
        $app->post('/{uid}/certs/{certId}/Actions/AcceptCert', array($this, 'acceptCert'));
17
    }
18
19
    protected function canCreate($request)
20
    {
21
        $this->validateLoggedIn($request);
22
        return true;
23
    }
24
25
    protected function canRead($request)
26
    {
27
        if($this->isVolunteerAdmin($request))
28
        {
29
            return true;
30
        }
31
        //TODO give access to department leads
32
        return true;
33
    }
34
35
    protected function canUpdate($request, $entity)
36
    {
37
        if($this->isVolunteerAdmin($request))
38
        {
39
            return true;
40
        }       
41
        //TODO give access to department lead
42
        return false;
43
    }
44
45
    protected function canDelete($request, $entity)
46
    {
47
        return $this->canUpdate($request, $entity);
48
    }
49
50
    protected function validateCreate(&$obj, $request)
51
    {
52
        if(isset($obj['uid']))
53
        {
54
            return false;
55
        }
56
        $uid = $this->user->uid;
57
        $dataTable = $this->getDataTable();
58
        $filter = $this->getFilterForPrimaryKey($uid);
59
        $users = $dataTable->read($filter);
60
        if(!empty($users))
61
        {
62
            //User is already created...
63
            return false;
64
        }
65
        $obj['uid'] = $uid;
66
        return true;
67
    }
68
69
    protected function getFilterForPrimaryKey($value)
70
    {
71
        if($value === 'me')
72
        {
73
            $value = $this->user->uid;
74
        }
75
        return parent::getFilterForPrimaryKey($value);
76
    }
77
78
    public function readEntry($request, $response, $args)
79
    {
80
        $this->validateLoggedIn($request);
81
        $uid = $args['name'];
82
        if($uid === 'me')
83
        {
84
            $uid = $this->user->uid;
85
        }
86
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
0 ignored issues
show
introduced by
The condition $this->canRead($request) === false is always false.
Loading history...
87
        {
88
            return $response->withStatus(401);
89
        }
90
        $dataTable = $this->getDataTable();
91
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
92
        $filter = $this->getFilterForPrimaryKey($uid);
93
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
94
                                    $odata->skip, $odata->orderby);
95
        if(empty($areas))
96
        {
97
            return $response->withStatus(404);
98
        }
99
        return $response->withJson($areas[0]);
100
    }
101
102
    public function getMyShifts($request, $response, $args)
103
    {
104
        $this->validateLoggedIn($request);
105
        $uid = $this->user->uid;
106
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
107
        $filter = new \Data\Filter("participant eq '$uid'");
108
        $shifts = $dataTable->read($filter);
109
        $format = $request->getAttribute('format', false);
110
        if($format === false || $format === 'text/calendar')
111
        {
112
            $text = "BEGIN:VCALENDAR\r\n";
113
            $text .= "VERSION:2.0\r\n";
114
            $text .= "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n";
115
            $count = count($shifts);
116
            for($i = 0; $i < $count; $i++)
117
            {
118
                $text .= "BEGIN:VEVENT\r\n";
119
                $text .= "UID:".$this->user->mail."\r\n";
120
                $d = new DateTime($shifts[$i]['startTime']);
121
                $d->setTimezone(new \DateTimeZone('UTC'));
122
                $text .= "DTSTAMP:".$d->format('Ymd\THis\Z')."\r\n";
123
                $text .= "DTSTART:".$d->format('Ymd\THis\Z')."\r\n";
124
                $d = new DateTime($shifts[$i]['endTime']);
125
                $d->setTimezone(new \DateTimeZone('UTC'));
126
                $text .= "DTEND:".$d->format('Ymd\THis\Z')."\r\n";
127
                $text .= "SUMMARY:".$shifts[$i]['roleID'].' '.$shifts[$i]['name']."\r\n";
128
                $text .= "END:VEVENT\r\n";
129
            }
130
            $text .= "END:VCALENDAR\r\n";
131
            $response = $response->withHeader('Content-type', 'text/calendar');
132
            $response = $response->withHeader('Content-Disposition', 'attachment; filename="MyShifts.ics"');
133
            $body = $response->getBody();
134
            $body->write($text);
135
        }
136
        else if($format === 'application/pdf')
137
        {
138
            $pdf = new \Schedules\SimplePDF('My', $shifts);
139
            $response = $response->withHeader('Content-Type', 'application/pdf');
140
            $response->getBody()->write($pdf->toPDFBuffer());
141
        }
142
        else
143
        {
144
            throw new \Exception('Unknown format '.$format);
145
        }
146
        return $response;
147
    }
148
149
    public function getCerts($request, $response, $args)
150
    {
151
        $this->validateLoggedIn($request);
152
        $uid = $args['uid'];
153
        if($uid === 'me')
154
        {
155
            $uid = $this->user->uid;
156
        }
157
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
158
        {
159
            return $response->withStatus(401);
160
        }
161
        $dataTable = $this->getDataTable();
162
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
163
        $filter = $this->getFilterForPrimaryKey($uid);
164
        $areas = $dataTable->read($filter, array('certs'), $odata->top,
165
                                    $odata->skip, $odata->orderby);
166
        if(empty($areas))
167
        {
168
            return $response->withStatus(404);
169
        }
170
        if(!isset($areas[0]['certs']))
171
        {
172
            return $response->withJson(array());
173
        }
174
        return $response->withJson($areas[0]['certs']);
175
    }
176
177
    public function uploadCert($request, $response, $args)
178
    {
179
        $this->validateLoggedIn($request);
180
        $uid = $args['uid'];
181
        if($uid === 'me')
182
        {
183
            $uid = $this->user->uid;
184
        }
185
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
186
        {
187
            return $response->withStatus(401);
188
        }
189
        $dataTable = $this->getDataTable();
190
        $filter = $this->getFilterForPrimaryKey($uid);
191
        $users = $dataTable->read($filter);
192
        if(empty($users))
193
        {
194
            return $response->withStatus(404);
195
        }
196
        $user = $users[0];
197
        if(!isset($user['certs']))
198
        {
199
            $user['certs'] = array();
200
        }
201
        $files = $request->getUploadedFiles();
202
        $file = $files['file'];
203
        $stream = $file->getStream();
204
        $cert = array('status'=>'pending', 'image'=>base64_encode($stream->getContents()), 'imageType'=>$file->getClientMediaType());
205
        $user['certs'][$args['certId']] = $cert;
206
        $ret = $dataTable->update($filter, $user);
207
        if($ret)
208
        {
209
            return $response->withStatus(200);
210
        }
211
        return $response->withStatus(500);
212
    }
213
214
    public function rejectCert($request, $response, $args)
215
    {
216
        $this->validateLoggedIn($request);
217
        $uid = $args['uid'];
218
        if($uid === 'me')
219
        {
220
            $uid = $this->user->uid;
221
        }
222
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
223
        {
224
            return $response->withStatus(401);
225
        }
226
        $dataTable = $this->getDataTable();
227
        $filter = $this->getFilterForPrimaryKey($uid);
228
        $users = $dataTable->read($filter);
229
        if(empty($users))
230
        {
231
            return $response->withStatus(404);
232
        }
233
        $user = $users[0];
234
        $certType = $args['certId'];
235
        if(!isset($user['certs']) || !isset($user['certs'][$certType]))
236
        {
237
            return $response->withStatus(404);
238
        }
239
        $obj = $this->getParsedBody($request);
240
        $reason = 'Unknown';
241
        switch($obj['reason'])
242
        {
243
            case 'invalid':
244
                $reason = 'the image provided did not seem to show a valid certication of the type selected';
245
                break;
246
            case 'expired':
247
                $reason = 'the image provided was for a certification that had already expired';
248
                break;
249
        }
250
        unset($user['certs'][$certType]);
251
        $ret = $dataTable->update($filter, $user);
252
        if($ret)
253
        {
254
            $profile = new \VolunteerProfile(false, $user);
255
            $email = new \Emails\CertificationEmail($profile, 'certifcationRejected', $certType, array('reason'=>$reason));
256
            $emailProvider = \EmailProvider::getInstance();
257
            if($emailProvider->sendEmail($email) === false)
258
            {
259
                throw new \Exception('Unable to send email!');
260
            }
261
            return $response->withStatus(200);
262
        }
263
        return $response->withStatus(500);
264
    }
265
266
    public function acceptCert($request, $response, $args)
267
    {
268
        $this->validateLoggedIn($request);
269
        $uid = $args['uid'];
270
        if($uid === 'me')
271
        {
272
            $uid = $this->user->uid;
273
        }
274
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
275
        {
276
            return $response->withStatus(401);
277
        }
278
        $dataTable = $this->getDataTable();
279
        $filter = $this->getFilterForPrimaryKey($uid);
280
        $users = $dataTable->read($filter);
281
        if(empty($users))
282
        {
283
            return $response->withStatus(404);
284
        }
285
        $user = $users[0];
286
        $certType = $args['certId'];
287
        $certType = $args['certId'];
288
        if(!isset($user['certs']) || !isset($user['certs'][$certType]))
289
        {
290
            return $response->withStatus(404);
291
        }
292
        $user['certs'][$certType]['status'] = 'current';
293
        $ret = $dataTable->update($filter, $user);
294
        if($ret)
295
        {
296
            $profile = new \VolunteerProfile(false, $user);
297
            $email = new \Emails\CertificationEmail($profile, 'certifcationAccepted', $certType);
298
            $emailProvider = \EmailProvider::getInstance();
299
            if($emailProvider->sendEmail($email) === false)
300
            {
301
                throw new \Exception('Unable to send email!');
302
            }
303
            return $response->withStatus(200);
304
        }
305
        return $response->withStatus(500);
306
    }
307
}
308
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
309