Complex classes like Organization 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 Organization, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Organization extends User |
||
| 45 | { |
||
| 46 | use SelfBlameableTrait; |
||
| 47 | |||
| 48 | const TYPE_ORGANIZATION = 1; |
||
| 49 | const TYPE_DEPARTMENT = 2; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var boolean Organization does not need password and corresponding features. |
||
| 53 | */ |
||
| 54 | public $passwordHashAttribute = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var boolean Organization does not need password and corresponding features. |
||
| 58 | */ |
||
| 59 | public $passwordResetTokenAttribute = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var boolean Organization does not need password and corresponding features. |
||
| 63 | */ |
||
| 64 | public $passwordHistoryClass = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var boolean Organization does not need source. |
||
| 68 | */ |
||
| 69 | public $sourceAttribute = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var boolean Organization does not need auth key. |
||
| 73 | */ |
||
| 74 | public $authKeyAttribute = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var boolean Organization does not need access token. |
||
| 78 | */ |
||
| 79 | public $accessTokenAttribute = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * |
||
| 83 | * @var boolean Organization does not need login log. |
||
| 84 | */ |
||
| 85 | public $loginLogClass = false; |
||
| 86 | |||
| 87 | public $profileClass = Profile::class; |
||
| 88 | |||
| 89 | public $memberClass = Member::class; |
||
| 90 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 91 | public $memberLimitClass = MemberLimit::class; |
||
| 92 | /** |
||
| 93 | * @var Member |
||
| 94 | */ |
||
| 95 | private $noInitMember; |
||
| 96 | /** |
||
| 97 | * @var SubordinateLimit |
||
| 98 | */ |
||
| 99 | private $noInitSubordinateLimit; |
||
| 100 | /** |
||
| 101 | * @var MemberLimit |
||
| 102 | */ |
||
| 103 | private $noInitMemberLimit; |
||
| 104 | public $creatorModel; |
||
| 105 | public $profileConfig; |
||
| 106 | /** |
||
| 107 | * @return Member |
||
| 108 | */ |
||
| 109 | 36 | protected function getNoInitMember() |
|
| 110 | { |
||
| 111 | 36 | if (!$this->noInitMember) { |
|
| 112 | 36 | $class = $this->memberClass; |
|
| 113 | 36 | $this->noInitMember = $class::buildNoInitModel(); |
|
| 114 | } |
||
| 115 | 36 | return $this->noInitMember; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return SubordinateLimit |
||
| 120 | */ |
||
| 121 | protected function getNoInitSubordinateLimit() |
||
| 122 | { |
||
| 123 | if (!$this->noInitSubordinateLimit) { |
||
| 124 | $class = $this->subordinateLimitClass; |
||
| 125 | $this->noInitSubordinateLimit = $class::buildNoInitModel(); |
||
| 126 | } |
||
| 127 | return $this->noInitSubordinateLimit; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return MemberLimit |
||
| 132 | */ |
||
| 133 | protected function getNoInitMemberLimit() |
||
| 134 | { |
||
| 135 | if (!$this->noInitMemberLimit) { |
||
| 136 | $class = $this->memberLimitClass; |
||
| 137 | $this->noInitMemberLimit = $class::buildNoInitModel(); |
||
| 138 | } |
||
| 139 | return $this->noInitMemberLimit; |
||
| 140 | } |
||
| 141 | |||
| 142 | 38 | public function init() |
|
| 143 | { |
||
| 144 | 38 | $this->parentAttribute = 'parent_guid'; |
|
| 145 | 38 | if (class_exists($this->memberClass)) { |
|
| 146 | 38 | $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]); |
|
| 147 | } |
||
| 148 | 38 | if ($this->skipInit) { |
|
| 149 | 38 | return; |
|
| 150 | } |
||
| 151 | 38 | $this->on(static::$eventAfterRegister, [$this, 'onAddProfile'], $this->profileConfig); |
|
| 152 | 38 | $this->on(static::$eventAfterRegister, [$this, 'onAssignCreator'], $this->creatorModel); |
|
| 153 | 38 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokeCreator']); |
|
| 154 | 38 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokeAdministrators']); |
|
| 155 | 38 | $this->on(static::EVENT_BEFORE_DELETE, [$this, 'onRevokePermissions']); |
|
| 156 | 38 | $this->initSelfBlameableEvents(); |
|
| 157 | 38 | parent::init(); |
|
| 158 | 38 | } |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @inheritdoc |
||
| 162 | */ |
||
| 163 | 1 | public function attributeLabels() |
|
| 164 | { |
||
| 165 | return [ |
||
| 166 | 1 | 'guid' => Yii::t('user', 'GUID'), |
|
| 167 | 1 | 'id' => Yii::t('user', 'ID'), |
|
| 168 | 1 | 'ip' => Yii::t('user', 'IP Address'), |
|
| 169 | 1 | 'ip_type' => Yii::t('user', 'IP Address Type'), |
|
| 170 | 1 | 'parent' => Yii::t('organization', 'Parent'), |
|
| 171 | 1 | 'created_at' => Yii::t('user', 'Creation Time'), |
|
| 172 | 1 | 'updated_at' => Yii::t('user', 'Last Updated Time'), |
|
| 173 | 1 | 'status' => Yii::t('user', 'Status'), |
|
| 174 | 1 | 'type' => Yii::t('user', 'Type'), |
|
| 175 | ]; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | 38 | public static function tableName() |
|
| 182 | { |
||
| 183 | 38 | return '{{%organization}}'; |
|
| 184 | } |
||
| 185 | |||
| 186 | 37 | protected function getTypeRules() |
|
| 187 | { |
||
| 188 | return [ |
||
| 189 | 37 | ['type', 'default', 'value' => static::TYPE_ORGANIZATION], |
|
| 190 | ['type', 'required'], |
||
| 191 | 37 | ['type', 'in', 'range' => [static::TYPE_ORGANIZATION, static::TYPE_DEPARTMENT]], |
|
| 192 | ]; |
||
| 193 | } |
||
| 194 | |||
| 195 | 37 | public function rules() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get Member Query. |
||
| 202 | * @return MemberQuery |
||
| 203 | */ |
||
| 204 | 36 | public function getMembers() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get organization member users' query. |
||
| 211 | * @return BaseUserQuery |
||
| 212 | */ |
||
| 213 | 4 | public function getMemberUsers() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get subordinate limit query. |
||
| 223 | * @return null|BaseBlameableQuery |
||
| 224 | */ |
||
| 225 | public function getSubordinateLimit() |
||
| 226 | { |
||
| 227 | if (empty($this->subordinateLimitClass)) { |
||
| 228 | return null; |
||
| 229 | } |
||
| 230 | return $this->hasOne($this->subordinateLimitClass, [$this->guidAttribute => $this->getNoInitSubordinateLimit()->createdByAttribute]); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get member limit query. |
||
| 235 | * @return null|BaseBlameableQuery |
||
| 236 | */ |
||
| 237 | public function getMemberLimit() |
||
| 238 | { |
||
| 239 | if (empty($this->memberLimitClass)) { |
||
| 240 | return null; |
||
| 241 | } |
||
| 242 | return $this->hasOne($this->memberLimitClass, [$this->guidAttribute => $this->getNoInitMemberLimit()->createdByAttribute]); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get member with specified user. |
||
| 247 | * @param User|string|integer $user |
||
| 248 | * @return Member Null if `user` is not in this organization. |
||
| 249 | */ |
||
| 250 | 36 | public function getMember($user) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Add member to organization. |
||
| 257 | * @param Member|User|string|integer $member |
||
| 258 | * @see createMemberModel |
||
| 259 | * @see createMemberModelWithUser |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | 36 | public function addMember(&$member) |
|
| 263 | { |
||
| 264 | 36 | if ($this->getIsNewRecord()) { |
|
| 265 | return false; |
||
| 266 | } |
||
| 267 | 36 | $model = null; |
|
| 268 | 36 | if ($member instanceof Member) { |
|
| 269 | if (!$member->getIsNewRecord()) { |
||
| 270 | return false; |
||
| 271 | } |
||
| 272 | $model = $this->createMemberModel($member); |
||
| 273 | } |
||
| 274 | 36 | if (($member instanceof User) || is_string($member) || is_int($member)) { |
|
| 275 | 36 | if ($this->hasMember($member)) { |
|
| 276 | return false; |
||
| 277 | } |
||
| 278 | 36 | $model = $this->createMemberModelWithUser($member); |
|
| 279 | } |
||
| 280 | 36 | $member = $model; |
|
| 281 | 36 | return ($member instanceof Member) ? $member->save() : false; |
|
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Create member model, and set organization with this. |
||
| 286 | * @param Member $member If this parameter is not new record, it's organization |
||
| 287 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 288 | * model and create new `Member` model. |
||
| 289 | * @see createMemberModelWithUser |
||
| 290 | * @return Member |
||
| 291 | */ |
||
| 292 | public function createMemberModel($member) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Create member model with user, and set organization with this. |
||
| 303 | * @param User|string|integer $user |
||
| 304 | * @return Member |
||
| 305 | */ |
||
| 306 | 36 | public function createMemberModelWithUser($user) |
|
| 307 | { |
||
| 308 | $config = [ |
||
| 309 | 36 | 'memberUser' => $user, |
|
| 310 | 36 | 'organization' => $this, |
|
| 311 | 36 | 'nickname' => '', |
|
| 312 | ]; |
||
| 313 | 36 | $member = $this->createMember($config); |
|
| 314 | 36 | $member->nickname = $member->memberUser->profile->nickname; |
|
| 315 | 36 | return $member; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Remove member. |
||
| 320 | * @param Member|User $member |
||
| 321 | * @return boolean |
||
| 322 | */ |
||
| 323 | 1 | public function removeMember(&$member) |
|
| 324 | { |
||
| 325 | 1 | if ($this->getIsNewRecord()) { |
|
| 326 | return false; |
||
| 327 | } |
||
| 328 | 1 | if ($member instanceof $this->memberClass) { |
|
| 329 | 1 | $member = $member->{$member->memberAttribute}; |
|
| 330 | } |
||
| 331 | 1 | $member = $this->getMember($member); |
|
| 332 | 1 | return $member && $member->delete() > 0; |
|
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Remove administrator. |
||
| 337 | * @param Member|User $member |
||
| 338 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
| 339 | * @return boolean |
||
| 340 | * @throws IntegrityException |
||
| 341 | */ |
||
| 342 | public function removeAdministrator(&$member, $keepMember = true) |
||
| 343 | { |
||
| 344 | if ($this->getIsNewRecord()) { |
||
| 345 | return false; |
||
| 346 | } |
||
| 347 | if ($member instanceof $this->memberClass) { |
||
| 348 | $member = $member->{$member->memberAttribute}; |
||
| 349 | } |
||
| 350 | $member = $this->getMember($member); |
||
| 351 | if ($member && $member->isAdministrator()) { |
||
| 352 | if ($keepMember) { |
||
| 353 | return $member->revokeAdministrator(); |
||
| 354 | } |
||
| 355 | return $this->removeMember($member); |
||
| 356 | } |
||
| 357 | return false; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * |
||
| 362 | * @param Event $event |
||
| 363 | */ |
||
| 364 | 37 | public function onAddProfile($event) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * |
||
| 375 | * @param Event $event |
||
| 376 | */ |
||
| 377 | 37 | public function onAssignCreator($event) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * |
||
| 384 | * @param Event $event |
||
| 385 | * @return boolean |
||
| 386 | */ |
||
| 387 | 17 | public function onRevokeCreator($event) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * |
||
| 399 | * @param Event $event |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | 17 | public function onRevokeAdministrators($event) |
|
| 403 | { |
||
| 404 | 17 | $sender = $event->sender; |
|
| 405 | /* @var $sender static */ |
||
| 406 | 17 | $members = $sender->getMemberAdministrators()->all(); |
|
| 407 | /* @var $members Member[] */ |
||
| 408 | 17 | foreach ($members as $member) |
|
| 409 | { |
||
| 410 | 1 | $member->revokeAdministrator(); |
|
| 411 | } |
||
| 412 | 17 | return true; |
|
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * |
||
| 417 | * @param Event $event |
||
| 418 | */ |
||
| 419 | 17 | public function onRevokePermissions($event) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Check whether current instance is an organization. |
||
| 426 | * @return boolean |
||
| 427 | */ |
||
| 428 | 2 | public function isOrganization() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Check whether current instance if a department. |
||
| 435 | * @return boolean |
||
| 436 | */ |
||
| 437 | 2 | public function isDepartment() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Check whether the current organization has a member. |
||
| 444 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 445 | * @return boolean |
||
| 446 | */ |
||
| 447 | 36 | public function hasMember($user) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get member query which role is specified `Creator`. |
||
| 454 | * @return MemberQuery |
||
| 455 | */ |
||
| 456 | 18 | public function getMemberCreators() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Get member query which role is specified `Administrator`. |
||
| 463 | * @return MemberQuery |
||
| 464 | */ |
||
| 465 | 18 | public function getMemberAdministrators() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get user query which role is specified `Creator`. |
||
| 472 | * @return BaseUserQuery |
||
| 473 | */ |
||
| 474 | 1 | public function getCreator() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get user query which role is specified `Administrator`. |
||
| 484 | * @return BaseUserQuery |
||
| 485 | */ |
||
| 486 | 1 | public function getAdministrators() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * |
||
| 496 | * @param User $user |
||
| 497 | * @return boolean |
||
| 498 | * @throws \Exception |
||
| 499 | * @throws IntegrityException |
||
| 500 | */ |
||
| 501 | 37 | protected function addCreator($user) |
|
| 502 | { |
||
| 503 | 37 | if (!$user) { |
|
| 504 | 1 | throw new InvalidParamException('Creator Invalid.'); |
|
| 505 | } |
||
| 506 | 36 | $member = $user; |
|
| 507 | 36 | $transaction = Yii::$app->db->beginTransaction(); |
|
| 508 | try { |
||
| 509 | 36 | if (!$this->addMember($member)) { |
|
| 510 | throw new IntegrityException('Failed to add member.'); |
||
| 511 | } |
||
| 512 | 36 | $role = $this->type == static::TYPE_ORGANIZATION ? (new OrganizationCreator)->name : (new DepartmentCreator)->name; |
|
| 513 | 36 | $member->assignRole($role); |
|
| 514 | 36 | if (!$member->save()) { |
|
| 515 | throw new IntegrityException('Failed to assign creator.'); |
||
| 516 | } |
||
| 517 | 36 | $transaction->commit(); |
|
| 518 | } catch (\Exception $ex) { |
||
| 519 | $transaction->rollBack(); |
||
| 520 | Yii::error($ex->getMessage(), __METHOD__); |
||
| 521 | throw $ex; |
||
| 522 | } |
||
| 523 | 36 | return true; |
|
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * |
||
| 528 | * @param User $user |
||
| 529 | * @return boolean |
||
| 530 | * @throws \Exception |
||
| 531 | * @throws IntegrityException |
||
| 532 | */ |
||
| 533 | 7 | public function addAdministrator($user) |
|
| 534 | { |
||
| 535 | 7 | $transaction = Yii::$app->db->beginTransaction(); |
|
| 536 | try { |
||
| 537 | 7 | if (!$this->hasMember($user) && !$this->addMember($user)) { |
|
| 538 | throw new IntegrityException('Failed to add member.'); |
||
| 539 | } |
||
| 540 | 7 | $member = $this->getMember($user); |
|
| 541 | 7 | $member->assignAdministrator(); |
|
| 542 | 7 | $transaction->commit(); |
|
| 543 | } catch (\Exception $ex) { |
||
| 544 | $transaction->rollBack(); |
||
| 545 | Yii::error($ex->getMessage(), __METHOD__); |
||
| 546 | throw $ex; |
||
| 547 | } |
||
| 548 | 7 | return true; |
|
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * |
||
| 553 | * @param type $user |
||
| 554 | * @return boolean |
||
| 555 | */ |
||
| 556 | 2 | public function hasAdministrator($user) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 567 | * @return boolean |
||
| 568 | */ |
||
| 569 | public function hasReachedSubordinateLimit() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Check whether this organization has reached the upper limit of members. |
||
| 585 | * @return boolean |
||
| 586 | */ |
||
| 587 | public function hasReachedMemberLimit() |
||
| 600 | } |
||
| 601 |