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 |
||
| 55 | class CirclesService { |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | private $userId; |
||
| 59 | |||
| 60 | /** @var IL10N */ |
||
| 61 | private $l10n; |
||
| 62 | |||
| 63 | /** @var IGroupManager */ |
||
| 64 | private $groupManager; |
||
| 65 | |||
| 66 | /** @var ConfigService */ |
||
| 67 | private $configService; |
||
| 68 | |||
| 69 | /** @var CirclesRequest */ |
||
| 70 | private $circlesRequest; |
||
| 71 | |||
| 72 | /** @var MembersRequest */ |
||
| 73 | private $membersRequest; |
||
| 74 | |||
| 75 | /** @var SharesRequest */ |
||
| 76 | private $sharesRequest; |
||
| 77 | |||
| 78 | /** @var FederatedLinksRequest */ |
||
| 79 | private $federatedLinksRequest; |
||
| 80 | |||
| 81 | /** @var GSUpstreamService */ |
||
| 82 | private $gsUpstreamService; |
||
| 83 | |||
| 84 | /** @var EventsService */ |
||
| 85 | private $eventsService; |
||
| 86 | |||
| 87 | /** @var CircleProviderRequest */ |
||
| 88 | private $circleProviderRequest; |
||
| 89 | |||
| 90 | /** @var MiscService */ |
||
| 91 | private $miscService; |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * CirclesService constructor. |
||
| 96 | * |
||
| 97 | * @param string $userId |
||
| 98 | * @param IL10N $l10n |
||
| 99 | * @param IGroupManager $groupManager |
||
| 100 | * @param ConfigService $configService |
||
| 101 | * @param CirclesRequest $circlesRequest |
||
| 102 | * @param MembersRequest $membersRequest |
||
| 103 | * @param SharesRequest $sharesRequest |
||
| 104 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 105 | * @param GSUpstreamService $gsUpstreamService |
||
| 106 | * @param EventsService $eventsService |
||
| 107 | * @param CircleProviderRequest $circleProviderRequest |
||
| 108 | * @param MiscService $miscService |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function __construct( |
|
| 111 | $userId, |
||
| 112 | IL10N $l10n, |
||
| 113 | IGroupManager $groupManager, |
||
| 114 | ConfigService $configService, |
||
| 115 | CirclesRequest $circlesRequest, |
||
| 116 | MembersRequest $membersRequest, |
||
| 117 | SharesRequest $sharesRequest, |
||
| 118 | FederatedLinksRequest $federatedLinksRequest, |
||
| 119 | GSUpstreamService $gsUpstreamService, |
||
| 120 | EventsService $eventsService, |
||
| 121 | CircleProviderRequest $circleProviderRequest, |
||
| 122 | MiscService $miscService |
||
| 123 | ) { |
||
| 124 | $this->userId = $userId; |
||
| 125 | $this->l10n = $l10n; |
||
| 126 | $this->groupManager = $groupManager; |
||
| 127 | $this->configService = $configService; |
||
| 128 | $this->circlesRequest = $circlesRequest; |
||
| 129 | $this->membersRequest = $membersRequest; |
||
| 130 | $this->sharesRequest = $sharesRequest; |
||
| 131 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
| 132 | $this->gsUpstreamService = $gsUpstreamService; |
||
| 133 | $this->eventsService = $eventsService; |
||
| 134 | $this->circleProviderRequest = $circleProviderRequest; |
||
| 135 | $this->miscService = $miscService; |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Create circle using this->userId as owner |
||
| 141 | * |
||
| 142 | * @param int|string $type |
||
| 143 | * @param string $name |
||
| 144 | * |
||
| 145 | * @return Circle |
||
| 146 | * @throws CircleTypeDisabledException |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | public function createCircle($type, $name) { |
||
| 150 | $type = $this->convertTypeStringToBitValue($type); |
||
| 151 | $type = (int)$type; |
||
| 152 | |||
| 153 | if ($type === '') { |
||
|
|
|||
| 154 | throw new CircleTypeDisabledException( |
||
| 155 | $this->l10n->t('You need a specify a type of circle') |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (!$this->configService->isCircleAllowed($type)) { |
||
| 160 | throw new CircleTypeDisabledException( |
||
| 161 | $this->l10n->t('You cannot create this type of circle') |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | $circle = new Circle($type, $name); |
||
| 166 | |||
| 167 | // try { |
||
| 168 | $circle->generateUniqueId(); |
||
| 169 | // TODO: check uniqueness !! |
||
| 170 | // TODO: check uniqueness !! |
||
| 171 | // TODO: check uniqueness !! |
||
| 172 | // TODO: check uniqueness !! |
||
| 173 | // $this->circlesRequest->createCircle($circle, $this->userId); |
||
| 174 | |||
| 175 | $owner = new Member($this->userId, Member::TYPE_USER); |
||
| 176 | $owner->setCircleId($circle->getUniqueId()) |
||
| 177 | ->setLevel(Member::LEVEL_OWNER) |
||
| 178 | ->setStatus(Member::STATUS_MEMBER); |
||
| 179 | $circle->setOwner($owner) |
||
| 180 | ->setViewer($owner); |
||
| 181 | |||
| 182 | // $this->membersRequest->createMember($circle->getOwner()); |
||
| 183 | |||
| 184 | $event = new GSevent(GSEvent::CIRCLE_CREATE, true); |
||
| 185 | $event->setCircle($circle); |
||
| 186 | $this->gsUpstreamService->newEvent($event); |
||
| 187 | |||
| 188 | // } catch (CircleAlreadyExistsException $e) { |
||
| 189 | // throw $e; |
||
| 190 | // } |
||
| 191 | |||
| 192 | // $this->eventsService->onCircleCreation($circle); |
||
| 193 | |||
| 194 | return $circle; |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 200 | * |
||
| 201 | * @param string $userId |
||
| 202 | * @param mixed $type |
||
| 203 | * @param string $name |
||
| 204 | * @param int $level |
||
| 205 | * |
||
| 206 | * @param bool $forceAll |
||
| 207 | * |
||
| 208 | * @return Circle[] |
||
| 209 | * @throws CircleTypeDisabledException |
||
| 210 | * @throws Exception |
||
| 211 | */ |
||
| 212 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
| 213 | $type = $this->convertTypeStringToBitValue($type); |
||
| 214 | |||
| 215 | if ($userId === '') { |
||
| 216 | throw new Exception('UserID cannot be null'); |
||
| 217 | } |
||
| 218 | |||
| 219 | if (!$this->configService->isCircleAllowed((int)$type)) { |
||
| 220 | throw new CircleTypeDisabledException( |
||
| 221 | $this->l10n->t('You cannot display this type of circle') |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | |||
| 225 | $data = []; |
||
| 226 | $result = $this->circlesRequest->getCircles($userId, $type, $name, $level, $forceAll); |
||
| 227 | foreach ($result as $item) { |
||
| 228 | $data[] = $item; |
||
| 229 | } |
||
| 230 | |||
| 231 | return $data; |
||
| 232 | } |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * returns details on circle and its members if this->userId is a member itself. |
||
| 237 | * |
||
| 238 | * @param string $circleUniqueId |
||
| 239 | * @param bool $forceAll |
||
| 240 | * |
||
| 241 | * @return Circle |
||
| 242 | * @throws Exception |
||
| 243 | */ |
||
| 244 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
| 245 | |||
| 246 | try { |
||
| 247 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId, '', $forceAll); |
||
| 248 | if ($this->viewerIsAdmin() |
||
| 249 | || $circle->getHigherViewer() |
||
| 250 | ->isLevel(Member::LEVEL_MEMBER) |
||
| 251 | || $forceAll === true |
||
| 252 | ) { |
||
| 253 | $this->detailsCircleMembers($circle); |
||
| 254 | $this->detailsCircleLinkedGroups($circle); |
||
| 255 | $this->detailsCircleFederatedCircles($circle); |
||
| 256 | } |
||
| 257 | } catch (Exception $e) { |
||
| 258 | throw $e; |
||
| 259 | } |
||
| 260 | |||
| 261 | return $circle; |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * get the Members list and add the result to the Circle. |
||
| 267 | * |
||
| 268 | * @param Circle $circle |
||
| 269 | * |
||
| 270 | * @throws Exception |
||
| 271 | */ |
||
| 272 | private function detailsCircleMembers(Circle &$circle) { |
||
| 273 | if ($this->viewerIsAdmin()) { |
||
| 274 | $members = $this->membersRequest->forceGetMembers($circle->getUniqueId(), 0); |
||
| 275 | } else { |
||
| 276 | $members = $this->membersRequest->getMembers( |
||
| 277 | $circle->getUniqueId(), $circle->getHigherViewer() |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | |||
| 281 | $circle->setMembers($members); |
||
| 282 | } |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * get the Linked Group list and add the result to the Circle. |
||
| 287 | * |
||
| 288 | * @param Circle $circle |
||
| 289 | * |
||
| 290 | * @throws MemberDoesNotExistException |
||
| 291 | */ |
||
| 292 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 293 | $groups = []; |
||
| 294 | if ($this->configService->isLinkedGroupsAllowed()) { |
||
| 295 | $groups = |
||
| 296 | $this->membersRequest->getGroupsFromCircle( |
||
| 297 | $circle->getUniqueId(), $circle->getHigherViewer() |
||
| 298 | ); |
||
| 299 | } |
||
| 300 | |||
| 301 | $circle->setGroups($groups); |
||
| 302 | } |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * get the Federated Circles list and add the result to the Circle. |
||
| 307 | * |
||
| 308 | * @param Circle $circle |
||
| 309 | */ |
||
| 310 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 311 | $links = []; |
||
| 312 | |||
| 313 | try { |
||
| 314 | if ($this->configService->isFederatedCirclesAllowed()) { |
||
| 315 | $circle->hasToBeFederated(); |
||
| 316 | $links = $this->federatedLinksRequest->getLinksFromCircle($circle->getUniqueId()); |
||
| 317 | } |
||
| 318 | } catch (FederatedCircleNotAllowedException $e) { |
||
| 319 | } |
||
| 320 | |||
| 321 | $circle->setLinks($links); |
||
| 322 | } |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * save new settings if current user is admin. |
||
| 327 | * |
||
| 328 | * @param string $circleUniqueId |
||
| 329 | * @param array $settings |
||
| 330 | * |
||
| 331 | * @return Circle |
||
| 332 | * @throws Exception |
||
| 333 | */ |
||
| 334 | public function settingsCircle($circleUniqueId, $settings) { |
||
| 335 | |||
| 336 | try { |
||
| 337 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 338 | $this->hasToBeOwner($circle->getHigherViewer()); |
||
| 339 | |||
| 340 | if (!$this->viewerIsAdmin()) { |
||
| 341 | $settings['members_limit'] = $circle->getSetting('members_limit'); |
||
| 342 | } |
||
| 343 | |||
| 344 | $ak = array_keys($settings); |
||
| 345 | foreach ($ak AS $k) { |
||
| 346 | $circle->setSetting($k, $settings[$k]); |
||
| 347 | } |
||
| 348 | |||
| 349 | $this->circlesRequest->updateCircle($circle, $this->userId); |
||
| 350 | |||
| 351 | $this->eventsService->onSettingsChange($circle); |
||
| 352 | } catch (Exception $e) { |
||
| 353 | throw $e; |
||
| 354 | } |
||
| 355 | |||
| 356 | return $circle; |
||
| 357 | } |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Join a circle. |
||
| 362 | * |
||
| 363 | * @param string $circleUniqueId |
||
| 364 | * |
||
| 365 | * @return null|Member |
||
| 366 | * @throws Exception |
||
| 367 | */ |
||
| 368 | public function joinCircle($circleUniqueId) { |
||
| 369 | try { |
||
| 370 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 371 | $member = $this->membersRequest->getFreshNewMember( |
||
| 372 | $circleUniqueId, $this->userId, Member::TYPE_USER, '' |
||
| 373 | ); |
||
| 374 | |||
| 375 | $event = new GSevent(GSEvent::MEMBER_JOIN); |
||
| 376 | $event->setSeverity(GSEvent::SEVERITY_HIGH); |
||
| 377 | $event->setCircle($circle); |
||
| 378 | $event->setMember($member); |
||
| 379 | $this->gsUpstreamService->newEvent($event); |
||
| 380 | } catch (Exception $e) { |
||
| 381 | throw $e; |
||
| 382 | } |
||
| 383 | |||
| 384 | return $member; |
||
| 385 | } |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * Leave a circle. |
||
| 390 | * |
||
| 391 | * @param string $circleUniqueId |
||
| 392 | * |
||
| 393 | * @return null|Member |
||
| 394 | * @throws Exception |
||
| 395 | */ |
||
| 396 | public function leaveCircle($circleUniqueId) { |
||
| 397 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 398 | $member = $circle->getViewer(); |
||
| 399 | |||
| 400 | $event = new GSevent(GSEvent::MEMBER_LEAVE); |
||
| 401 | $event->setCircle($circle); |
||
| 402 | $event->setMember($member); |
||
| 403 | $this->gsUpstreamService->newEvent($event); |
||
| 404 | |||
| 405 | return $member; |
||
| 406 | } |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * destroy a circle. |
||
| 411 | * |
||
| 412 | * @param string $circleUniqueId |
||
| 413 | * |
||
| 414 | * @throws CircleDoesNotExistException |
||
| 415 | * @throws MemberIsNotModeratorException |
||
| 416 | * @throws MemberIsNotOwnerException |
||
| 417 | */ |
||
| 418 | public function removeCircle($circleUniqueId) { |
||
| 419 | $circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId); |
||
| 420 | |||
| 421 | $this->hasToBeOwner($circle->getHigherViewer()); |
||
| 422 | |||
| 423 | $this->eventsService->onCircleDestruction($circle); |
||
| 424 | |||
| 425 | $this->membersRequest->removeAllFromCircle($circleUniqueId); |
||
| 426 | $this->circlesRequest->destroyCircle($circleUniqueId); |
||
| 427 | } |
||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * @param $circleName |
||
| 432 | * |
||
| 433 | * @return Circle|null |
||
| 434 | * @throws CircleDoesNotExistException |
||
| 435 | */ |
||
| 436 | public function infoCircleByName($circleName) { |
||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * When a user is removed. |
||
| 443 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
| 444 | * Remove the Circle if it has no admin. |
||
| 445 | * |
||
| 446 | * @param string $userId |
||
| 447 | */ |
||
| 448 | public function onUserRemoved($userId) { |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * switchOlderAdminToOwner(); |
||
| 468 | * |
||
| 469 | * @param Circle $circle |
||
| 470 | * @param Member[] $members |
||
| 471 | */ |
||
| 472 | private function switchOlderAdminToOwner(Circle $circle, $members) { |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * Convert a Type in String to its Bit Value |
||
| 489 | * |
||
| 490 | * @param string $type |
||
| 491 | * |
||
| 492 | * @return int|mixed |
||
| 493 | */ |
||
| 494 | public function convertTypeStringToBitValue($type) { |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * getCircleIcon() |
||
| 513 | * |
||
| 514 | * Return the right imagePath for a type of circle. |
||
| 515 | * |
||
| 516 | * @param string $type |
||
| 517 | * @param bool $png |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | public static function getCircleIcon($type, $png = false) { |
||
| 552 | |||
| 553 | |||
| 554 | /** |
||
| 555 | * @param string $circleUniqueIds |
||
| 556 | * @param int $limit |
||
| 557 | * @param int $offset |
||
| 558 | * |
||
| 559 | * @return array |
||
| 560 | */ |
||
| 561 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 572 | |||
| 573 | |||
| 574 | /** |
||
| 575 | * @param Circle $circle |
||
| 576 | * |
||
| 577 | * @throws MembersLimitException |
||
| 578 | */ |
||
| 579 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return bool |
||
| 603 | */ |
||
| 604 | public function viewerIsAdmin() { |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * should be moved. |
||
| 615 | * |
||
| 616 | * @param Member $member |
||
| 617 | * |
||
| 618 | * @throws MemberIsNotOwnerException |
||
| 619 | */ |
||
| 620 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * should be moved. |
||
| 632 | * |
||
| 633 | * @param Member $member |
||
| 634 | * |
||
| 635 | * @throws MemberIsNotOwnerException |
||
| 636 | */ |
||
| 637 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 645 | } |
||
| 646 |