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( | ||
| 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 = '') { | ||
| 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) { | ||
| 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) { | ||
| 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) { | ||
| 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) { | ||
| 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) { | ||
| 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 | $oldSettings = array_merge( | ||
| 322 | $circle->getSettings(), | ||
| 323 | [ | ||
| 324 | 'circle_name' => $circle->getName(), | ||
| 325 | 'circle_desc' => $circle->getDescription(), | ||
| 326 | ] | ||
| 327 | ); | ||
| 328 | |||
| 329 | 			if (!$this->viewerIsAdmin()) { | ||
| 330 | 				$settings['members_limit'] = $circle->getSetting('members_limit'); | ||
| 331 | } | ||
| 332 | |||
| 333 | $ak = array_keys($settings); | ||
| 334 | 			foreach ($ak AS $k) { | ||
| 335 | $circle->setSetting($k, $settings[$k]); | ||
| 336 | } | ||
| 337 | |||
| 338 | $this->circlesRequest->updateCircle($circle, $this->userId); | ||
| 339 | |||
| 340 | $this->eventsService->onSettingsChange($circle, $oldSettings); | ||
| 341 | 		} catch (\Exception $e) { | ||
| 342 | throw $e; | ||
| 343 | } | ||
| 344 | |||
| 345 | return $circle; | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | /** | ||
| 350 | * Join a circle. | ||
| 351 | * | ||
| 352 | * @param string $circleUniqueId | ||
| 353 | * | ||
| 354 | * @return null|Member | ||
| 355 | * @throws \Exception | ||
| 356 | */ | ||
| 357 | 	public function joinCircle($circleUniqueId) { | ||
| 358 | |||
| 359 | 		try { | ||
| 360 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); | ||
| 361 | |||
| 362 | $member = $this->membersRequest->getFreshNewMember( | ||
| 363 | $circleUniqueId, $this->userId, Member::TYPE_USER | ||
| 364 | ); | ||
| 365 | $member->hasToBeAbleToJoinTheCircle(); | ||
| 366 | $this->checkThatCircleIsNotFull($circle); | ||
| 367 | |||
| 368 | $member->joinCircle($circle->getType()); | ||
| 369 | $this->membersRequest->updateMember($member); | ||
| 370 | |||
| 371 | $this->eventsService->onMemberNew($circle, $member); | ||
| 372 | 		} catch (\Exception $e) { | ||
| 373 | throw $e; | ||
| 374 | } | ||
| 375 | |||
| 376 | return $member; | ||
| 377 | } | ||
| 378 | |||
| 379 | |||
| 380 | /** | ||
| 381 | * Leave a circle. | ||
| 382 | * | ||
| 383 | * @param string $circleUniqueId | ||
| 384 | * | ||
| 385 | * @return null|Member | ||
| 386 | * @throws \Exception | ||
| 387 | */ | ||
| 388 | 	public function leaveCircle($circleUniqueId) { | ||
| 389 | |||
| 390 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); | ||
| 391 | $member = $circle->getViewer(); | ||
| 392 | |||
| 393 | $member->hasToBeMemberOrAlmost(); | ||
| 394 | $member->cantBeOwner(); | ||
| 395 | |||
| 396 | $this->eventsService->onMemberLeaving($circle, $member); | ||
| 397 | |||
| 398 | $this->membersRequest->removeMember($member); | ||
| 399 | $this->sharesRequest->removeSharesFromMember($member); | ||
| 400 | |||
| 401 | return $member; | ||
| 402 | } | ||
| 403 | |||
| 404 | |||
| 405 | /** | ||
| 406 | * destroy a circle. | ||
| 407 | * | ||
| 408 | * @param string $circleUniqueId | ||
| 409 | * | ||
| 410 | * @param bool $force | ||
| 411 | * | ||
| 412 | * @throws CircleDoesNotExistException | ||
| 413 | * @throws MemberIsNotOwnerException | ||
| 414 | * @throws \OCA\Circles\Exceptions\ConfigNoCircleAvailableException | ||
| 415 | */ | ||
| 416 | 	public function removeCircle($circleUniqueId, bool $force = false) { | ||
| 431 | |||
| 432 | |||
| 433 | /** | ||
| 434 | * @param $circleName | ||
| 435 | * | ||
| 436 | * @return Circle|null | ||
| 437 | * @throws CircleDoesNotExistException | ||
| 438 | */ | ||
| 439 | 	public function infoCircleByName($circleName) { | ||
| 442 | |||
| 443 | |||
| 444 | /** | ||
| 445 | * When a user is removed. | ||
| 446 | * Before deleting a user from the cloud, we assign a new owner to his Circles. | ||
| 447 | * Remove the Circle if it has no admin. | ||
| 448 | * | ||
| 449 | * @param string $userId | ||
| 450 | */ | ||
| 451 | 	public function onUserRemoved($userId) { | ||
| 467 | |||
| 468 | |||
| 469 | /** | ||
| 470 | * switchOlderAdminToOwner(); | ||
| 471 | * | ||
| 472 | * @param Circle $circle | ||
| 473 | * @param Member[] $members | ||
| 474 | */ | ||
| 475 | 	private function switchOlderAdminToOwner(Circle $circle, $members) { | ||
| 488 | |||
| 489 | |||
| 490 | /** | ||
| 491 | * Convert a Type in String to its Bit Value | ||
| 492 | * | ||
| 493 | * @param string $type | ||
| 494 | * | ||
| 495 | * @return int|mixed | ||
| 496 | */ | ||
| 497 | 	public function convertTypeStringToBitValue($type) { | ||
| 512 | |||
| 513 | |||
| 514 | /** | ||
| 515 | * getCircleIcon() | ||
| 516 | * | ||
| 517 | * Return the right imagePath for a type of circle. | ||
| 518 | * | ||
| 519 | * @param string $type | ||
| 520 | * @param bool $png | ||
| 521 | * | ||
| 522 | * @return string | ||
| 523 | */ | ||
| 524 | 	public static function getCircleIcon($type, $png = false) { | ||
| 555 | |||
| 556 | |||
| 557 | /** | ||
| 558 | * @param string $circleUniqueIds | ||
| 559 | * @param int $limit | ||
| 560 | * @param int $offset | ||
| 561 | * | ||
| 562 | * @return array | ||
| 563 | */ | ||
| 564 | 	public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { | ||
| 575 | |||
| 576 | |||
| 577 | /** | ||
| 578 | * @param Circle $circle | ||
| 579 | * | ||
| 580 | * @throws MembersLimitException | ||
| 581 | */ | ||
| 582 | 	public function checkThatCircleIsNotFull(Circle $circle) { | ||
| 603 | |||
| 604 | /** | ||
| 605 | * @return bool | ||
| 606 | */ | ||
| 607 | 	public function viewerIsAdmin() { | ||
| 614 | |||
| 615 | |||
| 616 | /** | ||
| 617 | * should be moved. | ||
| 618 | * | ||
| 619 | * @param Member $member | ||
| 620 | * | ||
| 621 | * @throws MemberIsNotOwnerException | ||
| 622 | */ | ||
| 623 | View Code Duplication | 	public function hasToBeOwner(Member $member) { | |
| 631 | |||
| 632 | |||
| 633 | /** | ||
| 634 | * should be moved. | ||
| 635 | * | ||
| 636 | * @param Member $member | ||
| 637 | * | ||
| 638 | * @throws MemberIsNotOwnerException | ||
| 639 | */ | ||
| 640 | View Code Duplication | 	public function hasToBeAdmin(Member $member) { | |
| 648 | } | ||
| 649 |