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 | * @return Circle |
||
| 138 | * @throws CircleTypeDisabledException |
||
| 139 | * @throws \Exception |
||
| 140 | */ |
||
| 141 | public function createCircle($type, $name) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 174 | * |
||
| 175 | * @param string $userId |
||
| 176 | * @param mixed $type |
||
| 177 | * @param string $name |
||
| 178 | * @param int $level |
||
| 179 | * |
||
| 180 | * @param bool $forceAll |
||
| 181 | * |
||
| 182 | * @return Circle[] |
||
| 183 | * @throws CircleTypeDisabledException |
||
| 184 | * @throws Exception |
||
| 185 | */ |
||
| 186 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
| 187 | $type = $this->convertTypeStringToBitValue($type); |
||
| 188 | |||
| 189 | if ($userId === '') { |
||
| 190 | throw new Exception('UserID cannot be null'); |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!$this->configService->isCircleAllowed((int)$type)) { |
||
| 194 | throw new CircleTypeDisabledException( |
||
| 195 | $this->l10n->t('You cannot display this type of circle') |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | $data = []; |
||
| 200 | $result = $this->circlesRequest->getCircles($userId, $type, $name, $level, $forceAll); |
||
| 201 | foreach ($result as $item) { |
||
| 202 | $data[] = $item; |
||
| 203 | } |
||
| 204 | |||
| 205 | return $data; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * returns details on circle and its members if this->userId is a member itself. |
||
| 211 | * |
||
| 212 | * @param string $circleUniqueId |
||
| 213 | * @param bool $forceAll |
||
| 214 | * |
||
| 215 | * @return Circle |
||
| 216 | * @throws Exception |
||
| 217 | */ |
||
| 218 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
| 219 | |||
| 220 | try { |
||
| 221 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId, $forceAll); |
||
| 222 | if ($this->viewerIsAdmin() |
||
| 223 | || $circle->getHigherViewer() |
||
| 224 | ->isLevel(Member::LEVEL_MEMBER) |
||
| 225 | || $forceAll === true |
||
| 226 | ) { |
||
| 227 | $this->detailsCircleMembers($circle); |
||
| 228 | $this->detailsCircleLinkedGroups($circle); |
||
| 229 | $this->detailsCircleFederatedCircles($circle); |
||
| 230 | } |
||
| 231 | } catch (\Exception $e) { |
||
| 232 | throw $e; |
||
| 233 | } |
||
| 234 | |||
| 235 | return $circle; |
||
| 236 | } |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * get the Members list and add the result to the Circle. |
||
| 241 | * |
||
| 242 | * @param Circle $circle |
||
| 243 | * |
||
| 244 | * @throws Exception |
||
| 245 | */ |
||
| 246 | private function detailsCircleMembers(Circle &$circle) { |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * get the Linked Group list and add the result to the Circle. |
||
| 261 | * |
||
| 262 | * @param Circle $circle |
||
| 263 | * |
||
| 264 | * @throws MemberDoesNotExistException |
||
| 265 | */ |
||
| 266 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * get the Federated Circles list and add the result to the Circle. |
||
| 281 | * |
||
| 282 | * @param Circle $circle |
||
| 283 | */ |
||
| 284 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * save new settings if current user is admin. |
||
| 301 | * |
||
| 302 | * @param string $circleUniqueId |
||
| 303 | * @param array $settings |
||
| 304 | * |
||
| 305 | * @return Circle |
||
| 306 | * @throws \Exception |
||
| 307 | */ |
||
| 308 | public function settingsCircle($circleUniqueId, $settings) { |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Join a circle. |
||
| 336 | * |
||
| 337 | * @param string $circleUniqueId |
||
| 338 | * |
||
| 339 | * @return null|Member |
||
| 340 | * @throws \Exception |
||
| 341 | */ |
||
| 342 | public function joinCircle($circleUniqueId) { |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Leave a circle. |
||
| 367 | * |
||
| 368 | * @param string $circleUniqueId |
||
| 369 | * |
||
| 370 | * @return null|Member |
||
| 371 | * @throws \Exception |
||
| 372 | */ |
||
| 373 | public function leaveCircle($circleUniqueId) { |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * destroy a circle. |
||
| 392 | * |
||
| 393 | * @param string $circleUniqueId |
||
| 394 | * |
||
| 395 | * @throws CircleDoesNotExistException |
||
| 396 | * @throws MemberIsNotModeratorException |
||
| 397 | * @throws MemberIsNotOwnerException |
||
| 398 | */ |
||
| 399 | public function removeCircle($circleUniqueId) { |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * @param $circleName |
||
| 414 | * |
||
| 415 | * @return Circle|null |
||
| 416 | * @throws CircleDoesNotExistException |
||
| 417 | */ |
||
| 418 | public function infoCircleByName($circleName) { |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * When a user is removed. |
||
| 425 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
| 426 | * Remove the Circle if it has no admin. |
||
| 427 | * |
||
| 428 | * @param string $userId |
||
| 429 | */ |
||
| 430 | public function onUserRemoved($userId) { |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * switchOlderAdminToOwner(); |
||
| 450 | * |
||
| 451 | * @param Circle $circle |
||
| 452 | * @param Member[] $members |
||
| 453 | */ |
||
| 454 | private function switchOlderAdminToOwner(Circle $circle, $members) { |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * Convert a Type in String to its Bit Value |
||
| 471 | * |
||
| 472 | * @param string $type |
||
| 473 | * |
||
| 474 | * @return int|mixed |
||
| 475 | */ |
||
| 476 | public function convertTypeStringToBitValue($type) { |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * getCircleIcon() |
||
| 495 | * |
||
| 496 | * Return the right imagePath for a type of circle. |
||
| 497 | * |
||
| 498 | * @param string $type |
||
| 499 | * @param bool $png |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | public static function getCircleIcon($type, $png = false) { |
||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * @param string $circleUniqueIds |
||
| 538 | * @param int $limit |
||
| 539 | * @param int $offset |
||
| 540 | * |
||
| 541 | * @return array |
||
| 542 | */ |
||
| 543 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 554 | |||
| 555 | |||
| 556 | /** |
||
| 557 | * @param Circle $circle |
||
| 558 | * |
||
| 559 | * @throws MembersLimitException |
||
| 560 | */ |
||
| 561 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | public function viewerIsAdmin() { |
||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * should be moved. |
||
| 597 | * |
||
| 598 | * @param Member $member |
||
| 599 | * |
||
| 600 | * @throws MemberIsNotOwnerException |
||
| 601 | */ |
||
| 602 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 610 | |||
| 611 | |||
| 612 | /** |
||
| 613 | * should be moved. |
||
| 614 | * |
||
| 615 | * @param Member $member |
||
| 616 | * |
||
| 617 | * @throws MemberIsNotOwnerException |
||
| 618 | */ |
||
| 619 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 627 | } |
||
| 628 |