Total Complexity | 40 |
Total Lines | 340 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ApiController 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 ApiController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class ApiController extends Controller { |
||
49 | |||
50 | private $eventMapper; |
||
51 | private $optionsMapper; |
||
52 | private $votesMapper; |
||
53 | private $commentMapper; |
||
54 | |||
55 | /** |
||
56 | * PageController constructor. |
||
57 | * @param string $appName |
||
58 | * @param IRequest $request |
||
59 | * @param string $userId |
||
60 | * @param EventMapper $eventMapper |
||
61 | * @param OptionsMapper $optionsMapper |
||
62 | * @param VotesMapper $VotesMapper |
||
63 | * @param CommentMapper $CommentMapper |
||
64 | */ |
||
65 | public function __construct( |
||
66 | $appName, |
||
67 | IGroupManager $groupManager, |
||
68 | IRequest $request, |
||
69 | IUserManager $userManager, |
||
70 | $userId, |
||
71 | EventMapper $eventMapper, |
||
72 | OptionsMapper $optionsMapper, |
||
73 | VotesMapper $VotesMapper, |
||
74 | CommentMapper $CommentMapper |
||
75 | ) { |
||
76 | parent::__construct($appName, $request); |
||
77 | $this->userId = $userId; |
||
|
|||
78 | $this->groupManager = $groupManager; |
||
79 | $this->userManager = $userManager; |
||
80 | $this->eventMapper = $eventMapper; |
||
81 | $this->optionsMapper = $optionsMapper; |
||
82 | $this->votesMapper = $VotesMapper; |
||
83 | $this->commentMapper = $CommentMapper; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @NoAdminRequired |
||
88 | * @NoCSRFRequired |
||
89 | * @return DataResponse |
||
90 | */ |
||
91 | public function getSiteUsersAndGroups($getGroups = true, $getUsers = true, $skipGroups = array(), $skipUsers = array()) { |
||
123 | } |
||
124 | /** |
||
125 | * @NoAdminRequired |
||
126 | * @NoCSRFRequired |
||
127 | * @return Array |
||
128 | */ |
||
129 | function convertAccessList($item) { |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @NoAdminRequired |
||
156 | * @NoCSRFRequired |
||
157 | * @PublicPage |
||
158 | * @param string $hash |
||
159 | * @return DataResponse |
||
160 | */ |
||
161 | |||
162 | public function getPoll($hash) { |
||
163 | if (!\OC::$server->getUserSession()->getUser() instanceof IUser) { |
||
164 | return new DataResponse(null, Http::STATUS_UNAUTHORIZED); |
||
165 | } |
||
166 | |||
167 | try { |
||
168 | $poll = $this->eventMapper->findByHash($hash); |
||
169 | |||
170 | if ($poll->getExpire() === null) { |
||
171 | $expired = false; |
||
172 | $expiration = false; |
||
173 | } else { |
||
174 | $expired = time() > strtotime($poll->getExpire()); |
||
175 | $expiration = true; |
||
176 | } |
||
177 | |||
178 | if ($poll->getType() == 0) { |
||
179 | $pollType = 'datePoll'; |
||
180 | } else { |
||
181 | $pollType = 'textPoll'; |
||
182 | }; |
||
183 | |||
184 | if ($poll->getOwner() !== \OC::$server->getUserSession()->getUser()->getUID()) { |
||
185 | $mode = 'create'; |
||
186 | } else { |
||
187 | $mode = 'edit'; |
||
188 | } |
||
189 | $accessList = Array(); |
||
190 | $accessType = $poll->getAccess(); |
||
191 | if (!strpos('|public|hidden|registered', $accessType)) { |
||
192 | $accessList = explode(';',$accessType); |
||
193 | $accessList = array_filter($accessList); |
||
194 | $accessList = array_map(Array($this,'convertAccessList'), $accessList); |
||
195 | $accessType = 'select'; |
||
196 | } |
||
197 | |||
198 | $data = array(); |
||
199 | $commentsList = array(); |
||
200 | $optionList = array(); |
||
201 | $votesList = array(); |
||
202 | |||
203 | } catch (DoesNotExistException $e) { |
||
204 | return new DataResponse($e, Http::STATUS_NOT_FOUND); |
||
205 | }; |
||
206 | |||
207 | |||
208 | try { |
||
209 | $options = $this->optionsMapper->findByPoll($poll->getId()); |
||
210 | foreach ($options as $optionElement) { |
||
211 | $optionList[] = [ |
||
212 | 'id' => $optionElement->getId(), |
||
213 | 'text' => $optionElement->getPollOptionText(), |
||
214 | 'timestamp' => $optionElement->getTimestamp() |
||
215 | ]; |
||
216 | }; |
||
217 | } catch (DoesNotExistException $e) { |
||
218 | // ignore |
||
219 | }; |
||
220 | |||
221 | try { |
||
222 | $votes = $this->votesMapper->findByPoll($poll->getId()); |
||
223 | foreach ($votes as $voteElement) { |
||
224 | $votesList[] = [ |
||
225 | 'id' => $voteElement->getId(), |
||
226 | 'userId' => $voteElement->getUserId(), |
||
227 | 'voteOptionId' => $voteElement->getVoteOptionId(), |
||
228 | 'voteOptionText' => $voteElement->getVoteOptionText(), |
||
229 | 'voteAnswer' => $voteElement->getVoteAnswer() |
||
230 | ]; |
||
231 | }; |
||
232 | } catch (DoesNotExistException $e) { |
||
233 | // ignore |
||
234 | }; |
||
235 | |||
236 | try { |
||
237 | $comments = $this->commentMapper->findByPoll($poll->getId()); |
||
238 | foreach ($comments as $commentElement) { |
||
239 | $commentsList[] = [ |
||
240 | 'id' => $commentElement->getId(), |
||
241 | 'userId' => $commentElement->getUserId(), |
||
242 | 'date' => $commentElement->getDt() . ' UTC', |
||
243 | 'comment' => $commentElement->getComment() |
||
244 | ]; |
||
245 | }; |
||
246 | } catch (DoesNotExistException $e) { |
||
247 | // ignore |
||
248 | }; |
||
249 | |||
250 | $data['poll'] = [ |
||
251 | 'result' => 'found', |
||
252 | 'mode' => $mode, |
||
253 | 'comments' => $commentsList, |
||
254 | 'votes' => $votesList, |
||
255 | 'shares' => $accessList, |
||
256 | 'event' => [ |
||
257 | 'id' => $poll->getId(), |
||
258 | 'hash' => $hash, |
||
259 | 'type' => $pollType, |
||
260 | 'title' => $poll->getTitle(), |
||
261 | 'description' => $poll->getDescription(), |
||
262 | 'owner' => $poll->getOwner(), |
||
263 | 'created' => $poll->getCreated(), |
||
264 | 'access' => $accessType, |
||
265 | 'expiration' => $expiration, |
||
266 | 'expired' => $expired, |
||
267 | 'expirationDate' => $poll->getExpire(), |
||
268 | 'isAnonymous' => $poll->getIsAnonymous(), |
||
269 | 'fullAnonymous' => $poll->getFullAnonymous(), |
||
270 | 'disallowMaybe' => $poll->getDisallowMaybe() |
||
271 | ], |
||
272 | 'options' => [ |
||
273 | 'pollDates' => [], |
||
274 | 'pollTexts' => $optionList |
||
275 | ] |
||
276 | ]; |
||
277 | |||
278 | return new DataResponse($data, Http::STATUS_OK); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @NoAdminRequired |
||
283 | * @NoCSRFRequired |
||
284 | * @param string $poll |
||
285 | * @return DataResponse |
||
286 | */ |
||
287 | public function writePoll($event, $options, $shares, $mode) { |
||
388 | |||
389 | } |
||
390 | } |
||
391 |