Total Complexity | 40 |
Total Lines | 195 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like EventAPI 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 EventAPI, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
2 | class EventAPI extends VolunteerAPI |
||
3 | { |
||
4 | use Processor; |
||
|
|||
5 | |||
6 | public function __construct() |
||
9 | } |
||
10 | |||
11 | public function setup($app) |
||
12 | { |
||
13 | parent::setup($app); |
||
14 | $app->get('/{event}/shifts[/]', array($this, 'getShiftsForEvent')); |
||
15 | $app->post('/{event}/shifts[/]', array($this, 'createShiftForEvent')); |
||
16 | $app->get('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent')); |
||
17 | $app->post('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent')); |
||
18 | $app->post('/{event}/Actions/ApproveEE', array($this, 'approveEEForEvent')); |
||
19 | } |
||
20 | |||
21 | protected function getFilterForPrimaryKey($value) |
||
24 | } |
||
25 | |||
26 | protected function canUpdate($request, $entity) |
||
27 | { |
||
28 | if($this->isVolunteerAdmin($request)) |
||
29 | { |
||
30 | return true; |
||
31 | } |
||
32 | return false; |
||
33 | } |
||
34 | |||
35 | protected function canDelete($request, $entity) |
||
36 | { |
||
37 | if($this->isVolunteerAdmin($request)) |
||
38 | { |
||
39 | return true; |
||
40 | } |
||
41 | return false; |
||
42 | } |
||
43 | |||
44 | public function processEntry($entry, $request) |
||
45 | { |
||
46 | $entry['available'] = true; |
||
47 | $endTime = new DateTime($entry['endTime']); |
||
48 | $now = new DateTime(); |
||
49 | if($endTime < $now) |
||
50 | { |
||
51 | $entry['available'] = false; |
||
52 | $entry['why'] = 'Event is in the past'; |
||
53 | } |
||
54 | if(isset($entry['volList']) && !is_array($entry['volList'])) |
||
55 | { |
||
56 | $entry['volList'] = explode(',', $entry['volList']); |
||
57 | $count = count($entry['volList']); |
||
58 | for($i = 0; $i < $count; $i++) |
||
59 | { |
||
60 | $entry['volList'][$i] = trim($entry['volList'][$i]); |
||
61 | } |
||
62 | } |
||
63 | if($entry['private'] && !in_array($this->user->mail, $entry['volList'])) |
||
64 | { |
||
65 | $entry['available'] = false; |
||
66 | $entry['why'] = 'Event is private and you are not invited'; |
||
67 | } |
||
68 | if(!$entry['available'] && !$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user)) |
||
69 | { |
||
70 | return null; |
||
71 | } |
||
72 | if(!$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user) && isset($entry['eeLists'])) |
||
73 | { |
||
74 | unset($entry['eeLists']); |
||
75 | } |
||
76 | return $entry; |
||
77 | } |
||
78 | |||
79 | public function getShiftsForEvent($request, $response, $args) |
||
80 | { |
||
81 | $this->validateLoggedIn($request); |
||
82 | $eventId = $args['event']; |
||
83 | $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
||
84 | $odata = $request->getAttribute('odata', new \ODataParams(array())); |
||
85 | $filter = $this->addRequiredFilter('eventID', $eventId, $odata); |
||
86 | if($filter === false) |
||
87 | { |
||
88 | return $response->withStatus(409); |
||
89 | } |
||
90 | $shifts = $dataTable->read($filter, $odata->select, $odata->top, |
||
91 | $odata->skip, $odata->orderby); |
||
92 | if($shifts === false) |
||
93 | { |
||
94 | $shifts = array(); |
||
95 | } |
||
96 | $count = count($shifts); |
||
97 | for($i = 0; $i < $count; $i++) |
||
98 | { |
||
99 | $shifts[$i] = $this->processShift($shifts[$i], $request); |
||
100 | } |
||
101 | $shifts = array_values(array_filter($shifts)); |
||
102 | return $response->withJson($shifts); |
||
103 | } |
||
104 | |||
105 | public function createShiftForEvent($request, $response, $args) |
||
106 | { |
||
107 | $eventId = $args['event']; |
||
108 | if($this->canUpdate($request, null) === false) |
||
109 | { |
||
110 | return $response->withStatus(401); |
||
111 | } |
||
112 | $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
||
113 | $obj = $request->getParsedBody(); |
||
114 | if($obj == NULL) |
||
115 | { |
||
116 | $obj = json_decode($request->getBody()->getContents(), true); |
||
117 | } |
||
118 | $obj['eventID'] = $eventId; |
||
119 | $ret = $dataTable->create($obj); |
||
120 | return $response->withJson($ret); |
||
121 | } |
||
122 | |||
123 | public function getEEShiftReportForEvent($request, $response, $args) |
||
124 | { |
||
125 | $eventId = $args['event']; |
||
126 | if($this->canUpdate($request, null) === false) |
||
127 | { |
||
128 | return $response->withStatus(401); |
||
129 | } |
||
130 | $shiftDataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
||
131 | $obj = $request->getParsedBody(); |
||
132 | if($obj == NULL) |
||
133 | { |
||
134 | $obj = json_decode($request->getBody()->getContents(), true); |
||
135 | } |
||
136 | $filterStr = 'eventID eq '.$eventId.' and status eq filled'; |
||
137 | if(isset($obj['earlyLate'])) |
||
138 | { |
||
139 | $filterStr .= ' and earlyLate eq \''.$obj['earlyLate'].'\''; |
||
140 | } |
||
141 | else |
||
142 | { |
||
143 | $filterStr .= " and earlyLate ne '-1'"; |
||
144 | } |
||
145 | $filter = new \Data\Filter($filterStr); |
||
146 | $shifts = $shiftDataTable->read($filter); |
||
147 | $ret = array(); |
||
148 | $count = count($shifts); |
||
149 | for($i = 0; $i < $count; $i++) |
||
150 | { |
||
151 | $shift = new \VolunteerShift(false, $shifts[$i]); |
||
152 | $vol = $shift->participantObj; |
||
153 | $role = $shift->role; |
||
154 | $entry = array('name' => $vol->getDisplayName('paperName'), 'email'=> $vol->email, 'dept'=> $shift->departmentID, 'role' => $role->display_name, 'earlyLate'=>$shift->earlyLate); |
||
155 | array_push($ret, $entry); |
||
156 | } |
||
157 | return $response->withJson($ret); |
||
158 | } |
||
159 | |||
160 | protected function userCanAuth($type) |
||
173 | } |
||
174 | } |
||
175 | |||
176 | public function approveEEForEvent($request, $response, $args) |
||
197 | } |
||
198 | } |
||
199 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
200 |