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 |
||
41 | class Organization extends User |
||
42 | { |
||
43 | use SelfBlameableTrait; |
||
44 | |||
45 | const TYPE_ORGANIZATION = 1; |
||
46 | const TYPE_DEPARTMENT = 2; |
||
47 | |||
48 | /** |
||
49 | * @var boolean Organization does not need password and corresponding features. |
||
50 | */ |
||
51 | public $passwordHashAttribute = false; |
||
52 | |||
53 | /** |
||
54 | * @var boolean Organization does not need password and corresponding features. |
||
55 | */ |
||
56 | public $passwordResetTokenAttribute = false; |
||
57 | |||
58 | /** |
||
59 | * @var boolean Organization does not need password and corresponding features. |
||
60 | */ |
||
61 | public $passwordHistoryClass = false; |
||
62 | |||
63 | /** |
||
64 | * @var boolean Organization does not need source. |
||
65 | */ |
||
66 | public $sourceAttribute = false; |
||
67 | |||
68 | /** |
||
69 | * @var boolean Organization does not need auth key. |
||
70 | */ |
||
71 | public $authKeyAttribute = false; |
||
72 | |||
73 | /** |
||
74 | * @var boolean Organization does not need access token. |
||
75 | */ |
||
76 | public $accessTokenAttribute = false; |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @var boolean Organization does not need login log. |
||
81 | */ |
||
82 | public $loginLogClass = false; |
||
83 | |||
84 | public $profileClass = Profile::class; |
||
85 | |||
86 | public $memberClass = Member::class; |
||
87 | private $noInitMember; |
||
88 | public $creatorModel; |
||
89 | public $profileConfig; |
||
90 | /** |
||
91 | * @return Member |
||
92 | */ |
||
93 | 26 | protected function getNoInitMember() |
|
94 | { |
||
95 | 26 | if (!$this->noInitMember) { |
|
96 | 26 | $class = $this->memberClass; |
|
97 | 26 | $this->noInitMember = $class::buildNoInitModel(); |
|
98 | } |
||
99 | 26 | return $this->noInitMember; |
|
100 | } |
||
101 | |||
102 | 36 | public function init() |
|
103 | { |
||
104 | 36 | $this->parentAttribute = 'parent_guid'; |
|
105 | 36 | if (class_exists($this->memberClass)) { |
|
106 | 36 | $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]); |
|
107 | } |
||
108 | 36 | if ($this->skipInit) { |
|
109 | 36 | return; |
|
110 | } |
||
111 | 36 | $this->on(static::$eventAfterRegister, [$this, 'onAddProfile'], $this->profileConfig); |
|
112 | 36 | $this->on(static::$eventAfterRegister, [$this, 'onAssignCreator'], $this->creatorModel); |
|
113 | 36 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokeCreator']); |
|
114 | 36 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokeAdministrators']); |
|
115 | 36 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokePermissions']); |
|
116 | 36 | $this->initSelfBlameableEvents(); |
|
117 | 36 | parent::init(); |
|
118 | 36 | } |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | 1 | public function attributeLabels() |
|
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 36 | public static function tableName() |
|
145 | |||
146 | 35 | protected function getTypeRules() |
|
154 | |||
155 | 35 | public function rules() |
|
159 | |||
160 | /** |
||
161 | * Get Member Query. |
||
162 | * @return MemberQuery |
||
163 | */ |
||
164 | 26 | public function getMembers() |
|
168 | |||
169 | /** |
||
170 | * Get organization member users' query. |
||
171 | * @return BaseUserQuery |
||
172 | */ |
||
173 | 4 | public function getMemberUsers() |
|
180 | |||
181 | /** |
||
182 | * Get member with specified user. |
||
183 | * @param User|string|integer $user |
||
184 | * @return Member Null if `user` is not in this organization. |
||
185 | */ |
||
186 | 22 | public function getMember($user) |
|
190 | |||
191 | /** |
||
192 | * Add member to organization. |
||
193 | * @param Member|User|string|integer $member |
||
194 | * @see createMemberModel |
||
195 | * @see createMemberModelWithUser |
||
196 | * @return boolean |
||
197 | */ |
||
198 | 34 | public function addMember(&$member) |
|
213 | |||
214 | /** |
||
215 | * Create member model, and set organization with this. |
||
216 | * @param Member $member If this parameter is not new record, it's organization |
||
217 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
218 | * model and create new `Member` model. |
||
219 | * @see createMemberModelWithUser |
||
220 | * @return Member |
||
221 | */ |
||
222 | public function createMemberModel($member) |
||
230 | |||
231 | /** |
||
232 | * Create member model with user, and set organization with this. |
||
233 | * @param User|string|integer $user |
||
234 | * @return Member |
||
235 | */ |
||
236 | 34 | public function createMemberModelWithUser($user) |
|
248 | |||
249 | /** |
||
250 | * Remove member. |
||
251 | * @param Member|User $member |
||
252 | * @return boolean |
||
253 | */ |
||
254 | 1 | public function removeMember(&$member) |
|
265 | |||
266 | /** |
||
267 | * Remove administrator. |
||
268 | * @param Member|User $member |
||
269 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
270 | * @return boolean |
||
271 | * @throws IntegrityException |
||
272 | */ |
||
273 | public function removeAdministrator(&$member, $keepMember = true) |
||
274 | { |
||
275 | if ($this->getIsNewRecord()) { |
||
276 | return false; |
||
277 | } |
||
278 | if ($member instanceof $this->memberClass) { |
||
279 | $member = $member->{$member->memberAttribute}; |
||
280 | } |
||
281 | $member = $this->getMember($member); |
||
282 | if ($member && $member->isAdministrator()) { |
||
283 | if ($keepMember) { |
||
284 | return $member->revokeAdministrator(); |
||
285 | } |
||
286 | return $this->removeMember($member); |
||
287 | } |
||
288 | return false; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * |
||
293 | * @param Event $event |
||
294 | */ |
||
295 | 35 | public function onAddProfile($event) |
|
296 | { |
||
297 | 35 | $profile = $event->sender->createProfile($event->data); |
|
298 | 35 | if (!$profile->save()) { |
|
299 | throw new IntegrityException('Profile Save Failed.'); |
||
300 | } |
||
301 | 35 | return true; |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * |
||
306 | * @param Event $event |
||
307 | */ |
||
308 | 35 | public function onAssignCreator($event) |
|
312 | |||
313 | /** |
||
314 | * |
||
315 | * @param Event $event |
||
316 | */ |
||
317 | 15 | public function onRevokeCreator($event) |
|
318 | { |
||
319 | 15 | $sender = $event->sender; |
|
320 | /* @var $sender static */ |
||
321 | 15 | $member = $sender->getMemberCreators()->one(); |
|
322 | /* @var $member Member */ |
||
323 | 15 | $role = $this->type == static::TYPE_ORGANIZATION ? (new OrganizationCreator)->name : (new DepartmentCreator)->name; |
|
324 | 15 | return $member->revokeRole($role); |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * |
||
329 | * @param Event $event |
||
330 | */ |
||
331 | 15 | public function onRevokeAdministrators($event) |
|
332 | { |
||
333 | 15 | $sender = $event->sender; |
|
334 | /* @var $sender static */ |
||
335 | 15 | $members = $sender->getMemberAdministrators()->all(); |
|
336 | /* @var $members Member[] */ |
||
337 | 15 | foreach ($members as $member) |
|
338 | { |
||
339 | 1 | $member->revokeAdministrator(); |
|
340 | } |
||
341 | 15 | } |
|
342 | |||
343 | /** |
||
344 | * |
||
345 | * @param Event $event |
||
346 | */ |
||
347 | 15 | public function onRevokePermissions($event) |
|
348 | { |
||
349 | |||
350 | 15 | } |
|
351 | |||
352 | /** |
||
353 | * |
||
354 | * @return boolean |
||
355 | */ |
||
356 | 2 | public function isOrganization() |
|
357 | { |
||
358 | 2 | return $this->type == static::TYPE_ORGANIZATION; |
|
359 | } |
||
360 | |||
361 | /** |
||
362 | * |
||
363 | * @return boolean |
||
364 | */ |
||
365 | 2 | public function isDepartment() |
|
366 | { |
||
367 | 2 | return $this->type == static::TYPE_DEPARTMENT; |
|
368 | } |
||
369 | |||
370 | /** |
||
371 | * Check whether the current organization has a member. |
||
372 | * @param User $user |
||
373 | * @return boolean |
||
374 | */ |
||
375 | 8 | public function hasMember($user) |
|
376 | { |
||
377 | 8 | return !is_null($this->getMember($user)); |
|
378 | } |
||
379 | |||
380 | /** |
||
381 | * Get member query which role is specified `Creator`. |
||
382 | * @return MemberQuery |
||
383 | */ |
||
384 | 16 | public function getMemberCreators() |
|
388 | |||
389 | /** |
||
390 | * Get member query which role is specified `Administrator`. |
||
391 | * @return MemberQuery |
||
392 | */ |
||
393 | 16 | public function getMemberAdministrators() |
|
394 | { |
||
395 | 16 | return $this->getMembers()->andWhere(['role' => [(new DepartmentAdmin)->name, (new OrganizationAdmin)->name]]); |
|
396 | } |
||
397 | |||
398 | /** |
||
399 | * Get user query which role is specified `Creator`. |
||
400 | * @return BaseUserQuery |
||
401 | */ |
||
402 | 1 | public function getCreator() |
|
409 | |||
410 | /** |
||
411 | * Get user query which role is specified `Administrator`. |
||
412 | * @return BaseUserQuery |
||
413 | */ |
||
414 | 1 | public function getAdministrators() |
|
421 | |||
422 | /** |
||
423 | * |
||
424 | * @param User $user |
||
425 | * @return boolean |
||
426 | * @throws \Exception |
||
427 | * @throws IntegrityException |
||
428 | */ |
||
429 | 35 | protected function addCreator($user) |
|
453 | |||
454 | /** |
||
455 | * |
||
456 | * @param User $user |
||
457 | * @return boolean |
||
458 | * @throws \Exception |
||
459 | * @throws IntegrityException |
||
460 | */ |
||
461 | 7 | public function addAdministrator($user) |
|
478 | |||
479 | /** |
||
480 | * |
||
481 | * @param type $user |
||
482 | * @return boolean |
||
483 | */ |
||
484 | 2 | public function hasAdministrator($user) |
|
492 | } |
||
493 |