| Total Complexity | 56 |
| Total Lines | 304 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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() |
||
| 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) |
||
| 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) |
||
|
|
|||
| 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) |
||
| 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) |
||
| 264 | } |
||
| 265 | |||
| 266 | public function acceptCert($request, $response, $args) |
||
| 306 | } |
||
| 307 | } |
||
| 308 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 309 |