| Total Complexity | 64 |
| Total Lines | 349 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like ParticipantAPI 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 ParticipantAPI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 2 | class ParticipantAPI extends VolunteerAPI |
||
| 3 | { |
||
| 4 | public function __construct() |
||
| 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 | $app->get('/{uid}/ticketStatus', array($this, 'getTicketStatus')); |
||
| 18 | } |
||
| 19 | |||
| 20 | protected function canCreate($request) |
||
| 21 | { |
||
| 22 | $this->validateLoggedIn($request); |
||
| 23 | return true; |
||
| 24 | } |
||
| 25 | |||
| 26 | protected function canRead($request) |
||
| 27 | { |
||
| 28 | if($this->isVolunteerAdmin($request)) |
||
| 29 | { |
||
| 30 | return true; |
||
| 31 | } |
||
| 32 | if($this->user->isInGroupNamed('Leads')) |
||
| 33 | { |
||
| 34 | return true; |
||
| 35 | } |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | |||
| 39 | protected function canUpdate($request, $entity) |
||
| 40 | { |
||
| 41 | if($this->isVolunteerAdmin($request)) |
||
| 42 | { |
||
| 43 | return true; |
||
| 44 | } |
||
| 45 | //TODO give access to department lead |
||
| 46 | return false; |
||
| 47 | } |
||
| 48 | |||
| 49 | protected function canDelete($request, $entity) |
||
| 50 | { |
||
| 51 | return $this->canUpdate($request, $entity); |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function validateCreate(&$obj, $request) |
||
| 55 | { |
||
| 56 | if(isset($obj['uid'])) |
||
| 57 | { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | $uid = $this->user->uid; |
||
| 61 | $dataTable = $this->getDataTable(); |
||
| 62 | $filter = $this->getFilterForPrimaryKey($uid); |
||
| 63 | $users = $dataTable->read($filter); |
||
| 64 | if(!empty($users)) |
||
| 65 | { |
||
| 66 | //User is already created... |
||
| 67 | return false; |
||
| 68 | } |
||
| 69 | $obj['uid'] = $uid; |
||
| 70 | return true; |
||
| 71 | } |
||
| 72 | |||
| 73 | protected function getFilterForPrimaryKey($value) |
||
| 74 | { |
||
| 75 | if($value === 'me') |
||
| 76 | { |
||
| 77 | $value = $this->user->uid; |
||
| 78 | } |
||
| 79 | return parent::getFilterForPrimaryKey($value); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function readEntry($request, $response, $args) |
||
| 83 | { |
||
| 84 | $this->validateLoggedIn($request); |
||
| 85 | $uid = $args['name']; |
||
| 86 | if($uid === 'me') |
||
| 87 | { |
||
| 88 | $uid = $this->user->uid; |
||
| 89 | } |
||
| 90 | else if($uid !== $this->user->uid && $this->canRead($request) === false) |
||
| 91 | { |
||
| 92 | return $response->withStatus(401); |
||
| 93 | } |
||
| 94 | $dataTable = $this->getDataTable(); |
||
| 95 | $odata = $request->getAttribute('odata', new \ODataParams(array())); |
||
| 96 | $filter = $this->getFilterForPrimaryKey($uid); |
||
| 97 | $areas = $dataTable->read($filter, $odata->select, $odata->top, |
||
| 98 | $odata->skip, $odata->orderby); |
||
| 99 | if(empty($areas)) |
||
| 100 | { |
||
| 101 | return $response->withStatus(404); |
||
| 102 | } |
||
| 103 | return $response->withJson($areas[0]); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function getMyShifts($request, $response, $args) |
||
| 107 | { |
||
| 108 | $this->validateLoggedIn($request); |
||
| 109 | $uid = $this->user->uid; |
||
| 110 | $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
||
| 111 | $filter = new \Data\Filter("participant eq '$uid'"); |
||
| 112 | $shifts = $dataTable->read($filter); |
||
| 113 | $format = $request->getAttribute('format', false); |
||
| 114 | if($format === false || $format === 'text/calendar') |
||
| 115 | { |
||
| 116 | $text = "BEGIN:VCALENDAR\r\n"; |
||
| 117 | $text .= "VERSION:2.0\r\n"; |
||
| 118 | $text .= "PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n"; |
||
| 119 | $count = count($shifts); |
||
| 120 | for($i = 0; $i < $count; $i++) |
||
| 121 | { |
||
| 122 | $text .= "BEGIN:VEVENT\r\n"; |
||
| 123 | $text .= "UID:".$this->user->mail."\r\n"; |
||
| 124 | $d = new DateTime($shifts[$i]['startTime']); |
||
| 125 | $d->setTimezone(new \DateTimeZone('UTC')); |
||
| 126 | $text .= "DTSTAMP:".$d->format('Ymd\THis\Z')."\r\n"; |
||
| 127 | $text .= "DTSTART:".$d->format('Ymd\THis\Z')."\r\n"; |
||
| 128 | $d = new DateTime($shifts[$i]['endTime']); |
||
| 129 | $d->setTimezone(new \DateTimeZone('UTC')); |
||
| 130 | $text .= "DTEND:".$d->format('Ymd\THis\Z')."\r\n"; |
||
| 131 | $text .= "SUMMARY:".$shifts[$i]['roleID'].' '.$shifts[$i]['name']."\r\n"; |
||
| 132 | $text .= "END:VEVENT\r\n"; |
||
| 133 | } |
||
| 134 | $text .= "END:VCALENDAR\r\n"; |
||
| 135 | $response = $response->withHeader('Content-type', 'text/calendar'); |
||
| 136 | $response = $response->withHeader('Content-Disposition', 'attachment; filename="MyShifts.ics"'); |
||
| 137 | $body = $response->getBody(); |
||
| 138 | $body->write($text); |
||
| 139 | } |
||
| 140 | else if($format === 'application/pdf') |
||
| 141 | { |
||
| 142 | $pdf = new \Schedules\SimplePDF('My', $shifts); |
||
| 143 | $response = $response->withHeader('Content-Type', 'application/pdf'); |
||
| 144 | $response->getBody()->write($pdf->toPDFBuffer()); |
||
| 145 | } |
||
| 146 | else |
||
| 147 | { |
||
| 148 | throw new \Exception('Unknown format '.$format); |
||
| 149 | } |
||
| 150 | return $response; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function getCerts($request, $response, $args) |
||
| 179 | } |
||
| 180 | |||
| 181 | public function uploadCert($request, $response, $args) |
||
| 182 | { |
||
| 183 | $this->validateLoggedIn($request); |
||
| 184 | $uid = $args['uid']; |
||
| 185 | if($uid === 'me') |
||
| 186 | { |
||
| 187 | $uid = $this->user->uid; |
||
| 188 | } |
||
| 189 | else if($uid !== $this->user->uid && $this->canRead($request) === false) |
||
| 190 | { |
||
| 191 | return $response->withStatus(401); |
||
| 192 | } |
||
| 193 | $dataTable = $this->getDataTable(); |
||
| 194 | $filter = $this->getFilterForPrimaryKey($uid); |
||
| 195 | $users = $dataTable->read($filter); |
||
| 196 | if(empty($users)) |
||
| 197 | { |
||
| 198 | return $response->withStatus(404); |
||
| 199 | } |
||
| 200 | $user = $users[0]; |
||
| 201 | if(!isset($user['certs'])) |
||
| 202 | { |
||
| 203 | $user['certs'] = array(); |
||
| 204 | } |
||
| 205 | $files = $request->getUploadedFiles(); |
||
| 206 | $file = $files['file']; |
||
| 207 | $stream = $file->getStream(); |
||
| 208 | $cert = array('status'=>'pending', 'image'=>base64_encode($stream->getContents()), 'imageType'=>$file->getClientMediaType()); |
||
| 209 | $user['certs'][$args['certId']] = $cert; |
||
| 210 | $ret = $dataTable->update($filter, $user); |
||
| 211 | if($ret) |
||
| 212 | { |
||
| 213 | return $response->withStatus(200); |
||
| 214 | } |
||
| 215 | return $response->withStatus(500); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function rejectCert($request, $response, $args) |
||
| 268 | } |
||
| 269 | |||
| 270 | public function acceptCert($request, $response, $args) |
||
| 310 | } |
||
| 311 | |||
| 312 | public function getTicketStatus($request, $response, $args) |
||
| 351 | } |
||
| 352 | } |
||
| 353 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 354 |