Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CirclesService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 CirclesService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class CirclesService { |
||
54 | |||
55 | /** @var string */ |
||
56 | private $userId; |
||
57 | |||
58 | /** @var IL10N */ |
||
59 | private $l10n; |
||
60 | |||
61 | /** @var IGroupManager */ |
||
62 | private $groupManager; |
||
63 | |||
64 | /** @var ConfigService */ |
||
65 | private $configService; |
||
66 | |||
67 | /** @var CirclesRequest */ |
||
68 | private $circlesRequest; |
||
69 | |||
70 | /** @var MembersRequest */ |
||
71 | private $membersRequest; |
||
72 | |||
73 | /** @var SharesRequest */ |
||
74 | private $sharesRequest; |
||
75 | |||
76 | /** @var FederatedLinksRequest */ |
||
77 | private $federatedLinksRequest; |
||
78 | |||
79 | /** @var EventsService */ |
||
80 | private $eventsService; |
||
81 | |||
82 | /** @var CircleProviderRequest */ |
||
83 | private $circleProviderRequest; |
||
84 | |||
85 | /** @var MiscService */ |
||
86 | private $miscService; |
||
87 | |||
88 | |||
89 | /** |
||
90 | * CirclesService constructor. |
||
91 | * |
||
92 | * @param string $userId |
||
93 | * @param IL10N $l10n |
||
94 | * @param IGroupManager $groupManager |
||
95 | * @param ConfigService $configService |
||
96 | * @param CirclesRequest $circlesRequest |
||
97 | * @param MembersRequest $membersRequest |
||
98 | * @param SharesRequest $sharesRequest |
||
99 | * @param FederatedLinksRequest $federatedLinksRequest |
||
100 | * @param EventsService $eventsService |
||
101 | * @param CircleProviderRequest $circleProviderRequest |
||
102 | * @param MiscService $miscService |
||
103 | */ |
||
104 | public function __construct( |
||
105 | $userId, |
||
106 | IL10N $l10n, |
||
107 | IGroupManager $groupManager, |
||
108 | ConfigService $configService, |
||
109 | CirclesRequest $circlesRequest, |
||
110 | MembersRequest $membersRequest, |
||
111 | SharesRequest $sharesRequest, |
||
112 | FederatedLinksRequest $federatedLinksRequest, |
||
113 | EventsService $eventsService, |
||
114 | CircleProviderRequest $circleProviderRequest, |
||
115 | MiscService $miscService |
||
116 | ) { |
||
117 | $this->userId = $userId; |
||
118 | $this->l10n = $l10n; |
||
119 | $this->groupManager = $groupManager; |
||
120 | $this->configService = $configService; |
||
121 | $this->circlesRequest = $circlesRequest; |
||
122 | $this->membersRequest = $membersRequest; |
||
123 | $this->sharesRequest = $sharesRequest; |
||
124 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
125 | $this->eventsService = $eventsService; |
||
126 | $this->circleProviderRequest = $circleProviderRequest; |
||
127 | $this->miscService = $miscService; |
||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Create circle using this->userId as owner |
||
133 | * |
||
134 | * @param int|string $type |
||
135 | * @param string $name |
||
136 | * |
||
137 | * @param string $ownerId |
||
138 | * |
||
139 | * @return Circle |
||
140 | * @throws CircleAlreadyExistsException |
||
141 | * @throws CircleTypeDisabledException |
||
142 | * @throws \OCA\Circles\Exceptions\MemberAlreadyExistsException |
||
143 | */ |
||
144 | public function createCircle($type, $name, string $ownerId = '') { |
||
145 | $type = $this->convertTypeStringToBitValue($type); |
||
146 | $type = (int)$type; |
||
147 | |||
148 | if ($type === '') { |
||
|
|||
149 | throw new CircleTypeDisabledException( |
||
150 | $this->l10n->t('You need a specify a type of circle') |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | if (!$this->configService->isCircleAllowed($type)) { |
||
155 | throw new CircleTypeDisabledException( |
||
156 | $this->l10n->t('You cannot create this type of circle') |
||
157 | ); |
||
158 | } |
||
159 | |||
160 | $circle = new Circle($type, $name); |
||
161 | |||
162 | if ($ownerId === '') { |
||
163 | $ownerId = $this->userId; |
||
164 | } |
||
165 | |||
166 | try { |
||
167 | $this->circlesRequest->createCircle($circle, $ownerId); |
||
168 | $this->membersRequest->createMember($circle->getOwner()); |
||
169 | } catch (CircleAlreadyExistsException $e) { |
||
170 | throw $e; |
||
171 | } |
||
172 | |||
173 | $this->eventsService->onCircleCreation($circle); |
||
174 | |||
175 | return $circle; |
||
176 | } |
||
177 | |||
178 | |||
179 | /** |
||
180 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
181 | * |
||
182 | * @param string $userId |
||
183 | * @param mixed $type |
||
184 | * @param string $name |
||
185 | * @param int $level |
||
186 | * |
||
187 | * @param bool $forceAll |
||
188 | * |
||
189 | * @return Circle[] |
||
190 | * @throws CircleTypeDisabledException |
||
191 | * @throws Exception |
||
192 | */ |
||
193 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
194 | $type = $this->convertTypeStringToBitValue($type); |
||
195 | |||
196 | if ($userId === '') { |
||
197 | throw new Exception('UserID cannot be null'); |
||
198 | } |
||
199 | |||
200 | if (!$this->configService->isCircleAllowed((int)$type)) { |
||
201 | throw new CircleTypeDisabledException( |
||
202 | $this->l10n->t('You cannot display this type of circle') |
||
203 | ); |
||
204 | } |
||
205 | |||
206 | $data = []; |
||
207 | $result = $this->circlesRequest->getCircles($userId, $type, $name, $level, $forceAll); |
||
208 | foreach ($result as $item) { |
||
209 | $data[] = $item; |
||
210 | } |
||
211 | |||
212 | return $data; |
||
213 | } |
||
214 | |||
215 | |||
216 | /** |
||
217 | * returns details on circle and its members if this->userId is a member itself. |
||
218 | * |
||
219 | * @param string $circleUniqueId |
||
220 | * @param bool $forceAll |
||
221 | * |
||
222 | * @return Circle |
||
223 | * @throws Exception |
||
224 | */ |
||
225 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
226 | |||
227 | try { |
||
228 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId, $forceAll); |
||
229 | if ($this->viewerIsAdmin() |
||
230 | || $circle->getHigherViewer() |
||
231 | ->isLevel(Member::LEVEL_MEMBER) |
||
232 | || $forceAll === true |
||
233 | ) { |
||
234 | $this->detailsCircleMembers($circle); |
||
235 | $this->detailsCircleLinkedGroups($circle); |
||
236 | $this->detailsCircleFederatedCircles($circle); |
||
237 | } |
||
238 | } catch (\Exception $e) { |
||
239 | throw $e; |
||
240 | } |
||
241 | |||
242 | return $circle; |
||
243 | } |
||
244 | |||
245 | |||
246 | /** |
||
247 | * get the Members list and add the result to the Circle. |
||
248 | * |
||
249 | * @param Circle $circle |
||
250 | * |
||
251 | * @throws Exception |
||
252 | */ |
||
253 | private function detailsCircleMembers(Circle &$circle) { |
||
254 | if ($this->viewerIsAdmin()) { |
||
255 | $members = $this->membersRequest->forceGetMembers($circle->getUniqueId(), 0); |
||
256 | } else { |
||
257 | $members = $this->membersRequest->getMembers( |
||
258 | $circle->getUniqueId(), $circle->getHigherViewer() |
||
259 | ); |
||
260 | } |
||
261 | |||
262 | $circle->setMembers($members); |
||
263 | } |
||
264 | |||
265 | |||
266 | /** |
||
267 | * get the Linked Group list and add the result to the Circle. |
||
268 | * |
||
269 | * @param Circle $circle |
||
270 | * |
||
271 | * @throws MemberDoesNotExistException |
||
272 | */ |
||
273 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
274 | $groups = []; |
||
275 | if ($this->configService->isLinkedGroupsAllowed()) { |
||
276 | $groups = |
||
277 | $this->membersRequest->getGroupsFromCircle( |
||
278 | $circle->getUniqueId(), $circle->getHigherViewer() |
||
279 | ); |
||
280 | } |
||
281 | |||
282 | $circle->setGroups($groups); |
||
283 | } |
||
284 | |||
285 | |||
286 | /** |
||
287 | * get the Federated Circles list and add the result to the Circle. |
||
288 | * |
||
289 | * @param Circle $circle |
||
290 | */ |
||
291 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
292 | $links = []; |
||
293 | |||
294 | try { |
||
295 | if ($this->configService->isFederatedCirclesAllowed()) { |
||
296 | $circle->hasToBeFederated(); |
||
297 | $links = $this->federatedLinksRequest->getLinksFromCircle($circle->getUniqueId()); |
||
298 | } |
||
299 | } catch (FederatedCircleNotAllowedException $e) { |
||
300 | } |
||
301 | |||
302 | $circle->setLinks($links); |
||
303 | } |
||
304 | |||
305 | |||
306 | /** |
||
307 | * save new settings if current user is admin. |
||
308 | * |
||
309 | * @param string $circleUniqueId |
||
310 | * @param array $settings |
||
311 | * |
||
312 | * @return Circle |
||
313 | * @throws \Exception |
||
314 | */ |
||
315 | public function settingsCircle($circleUniqueId, $settings) { |
||
316 | |||
317 | try { |
||
318 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
319 | $this->hasToBeOwner($circle->getHigherViewer()); |
||
320 | |||
321 | if (!$this->viewerIsAdmin()) { |
||
322 | $settings['members_limit'] = $circle->getSetting('members_limit'); |
||
323 | } |
||
324 | |||
325 | $ak = array_keys($settings); |
||
326 | foreach ($ak AS $k) { |
||
327 | $circle->setSetting($k, $settings[$k]); |
||
328 | } |
||
329 | |||
330 | $this->circlesRequest->updateCircle($circle, $this->userId); |
||
331 | |||
332 | $this->eventsService->onSettingsChange($circle); |
||
333 | } catch (\Exception $e) { |
||
334 | throw $e; |
||
335 | } |
||
336 | |||
337 | return $circle; |
||
338 | } |
||
339 | |||
340 | |||
341 | /** |
||
342 | * Join a circle. |
||
343 | * |
||
344 | * @param string $circleUniqueId |
||
345 | * |
||
346 | * @return null|Member |
||
347 | * @throws \Exception |
||
348 | */ |
||
349 | public function joinCircle($circleUniqueId) { |
||
350 | |||
351 | try { |
||
352 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
353 | |||
354 | $member = $this->membersRequest->getFreshNewMember( |
||
355 | $circleUniqueId, $this->userId, Member::TYPE_USER |
||
356 | ); |
||
357 | $member->hasToBeAbleToJoinTheCircle(); |
||
358 | $this->checkThatCircleIsNotFull($circle); |
||
359 | |||
360 | $member->joinCircle($circle->getType()); |
||
361 | $this->membersRequest->updateMember($member); |
||
362 | |||
363 | $this->eventsService->onMemberNew($circle, $member); |
||
364 | } catch (\Exception $e) { |
||
365 | throw $e; |
||
366 | } |
||
367 | |||
368 | return $member; |
||
369 | } |
||
370 | |||
371 | |||
372 | /** |
||
373 | * Leave a circle. |
||
374 | * |
||
375 | * @param string $circleUniqueId |
||
376 | * |
||
377 | * @return null|Member |
||
378 | * @throws \Exception |
||
379 | */ |
||
380 | public function leaveCircle($circleUniqueId) { |
||
381 | |||
382 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
383 | $member = $circle->getViewer(); |
||
384 | |||
385 | $member->hasToBeMemberOrAlmost(); |
||
386 | $member->cantBeOwner(); |
||
387 | |||
388 | $this->eventsService->onMemberLeaving($circle, $member); |
||
389 | |||
390 | $this->membersRequest->removeMember($member); |
||
391 | $this->sharesRequest->removeSharesFromMember($member); |
||
392 | |||
393 | return $member; |
||
394 | } |
||
395 | |||
396 | |||
397 | /** |
||
398 | * destroy a circle. |
||
399 | * |
||
400 | * @param string $circleUniqueId |
||
401 | * |
||
402 | * @param bool $force |
||
403 | * |
||
404 | * @throws CircleDoesNotExistException |
||
405 | * @throws MemberIsNotOwnerException |
||
406 | * @throws \OCA\Circles\Exceptions\ConfigNoCircleAvailableException |
||
407 | */ |
||
408 | public function removeCircle($circleUniqueId, bool $force = false) { |
||
409 | |||
410 | View Code Duplication | if ($force) { |
|
411 | $circle = $this->circlesRequest->forceGetCircle($circleUniqueId); |
||
412 | } else { |
||
413 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
414 | $this->hasToBeOwner($circle->getHigherViewer()); |
||
415 | } |
||
416 | |||
417 | |||
418 | $this->eventsService->onCircleDestruction($circle); |
||
419 | |||
420 | $this->membersRequest->removeAllFromCircle($circleUniqueId); |
||
421 | $this->circlesRequest->destroyCircle($circleUniqueId); |
||
422 | } |
||
423 | |||
424 | |||
425 | /** |
||
426 | * @param $circleName |
||
427 | * |
||
428 | * @return Circle|null |
||
429 | * @throws CircleDoesNotExistException |
||
430 | */ |
||
431 | public function infoCircleByName($circleName) { |
||
434 | |||
435 | |||
436 | /** |
||
437 | * When a user is removed. |
||
438 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
439 | * Remove the Circle if it has no admin. |
||
440 | * |
||
441 | * @param string $userId |
||
442 | */ |
||
443 | public function onUserRemoved($userId) { |
||
459 | |||
460 | |||
461 | /** |
||
462 | * switchOlderAdminToOwner(); |
||
463 | * |
||
464 | * @param Circle $circle |
||
465 | * @param Member[] $members |
||
466 | */ |
||
467 | private function switchOlderAdminToOwner(Circle $circle, $members) { |
||
480 | |||
481 | |||
482 | /** |
||
483 | * Convert a Type in String to its Bit Value |
||
484 | * |
||
485 | * @param string $type |
||
486 | * |
||
487 | * @return int|mixed |
||
488 | */ |
||
489 | public function convertTypeStringToBitValue($type) { |
||
504 | |||
505 | |||
506 | /** |
||
507 | * getCircleIcon() |
||
508 | * |
||
509 | * Return the right imagePath for a type of circle. |
||
510 | * |
||
511 | * @param string $type |
||
512 | * @param bool $png |
||
513 | * |
||
514 | * @return string |
||
515 | */ |
||
516 | public static function getCircleIcon($type, $png = false) { |
||
547 | |||
548 | |||
549 | /** |
||
550 | * @param string $circleUniqueIds |
||
551 | * @param int $limit |
||
552 | * @param int $offset |
||
553 | * |
||
554 | * @return array |
||
555 | */ |
||
556 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
567 | |||
568 | |||
569 | /** |
||
570 | * @param Circle $circle |
||
571 | * |
||
572 | * @throws MembersLimitException |
||
573 | */ |
||
574 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
575 | |||
576 | $members = $this->membersRequest->forceGetMembers( |
||
577 | $circle->getUniqueId(), Member::LEVEL_MEMBER, true |
||
578 | ); |
||
579 | |||
580 | $limit = (int) $circle->getSetting('members_limit'); |
||
581 | if ($limit === -1) { |
||
582 | return; |
||
583 | } |
||
584 | if ($limit === 0) { |
||
585 | $limit = $this->configService->getAppValue(ConfigService::CIRCLES_MEMBERS_LIMIT); |
||
586 | } |
||
587 | |||
588 | if (sizeof($members) >= $limit) { |
||
589 | throw new MembersLimitException( |
||
590 | 'This circle already reach its limit on the number of members' |
||
591 | ); |
||
592 | } |
||
593 | |||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @return bool |
||
598 | */ |
||
599 | public function viewerIsAdmin() { |
||
606 | |||
607 | |||
608 | /** |
||
609 | * should be moved. |
||
610 | * |
||
611 | * @param Member $member |
||
612 | * |
||
613 | * @throws MemberIsNotOwnerException |
||
614 | */ |
||
615 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
623 | |||
624 | |||
625 | /** |
||
626 | * should be moved. |
||
627 | * |
||
628 | * @param Member $member |
||
629 | * |
||
630 | * @throws MemberIsNotOwnerException |
||
631 | */ |
||
632 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
640 | } |
||
641 |