Complex classes like UserIndexesController 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 UserIndexesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class UserIndexesController extends BaseElementsController |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * @var string|null |
||
40 | */ |
||
41 | private $elementType; |
||
42 | |||
43 | /** |
||
44 | * @var string|null |
||
45 | */ |
||
46 | private $context; |
||
47 | |||
48 | /** |
||
49 | * @var string|null |
||
50 | */ |
||
51 | private $sourceKey; |
||
52 | |||
53 | /** |
||
54 | * @var array|null |
||
55 | */ |
||
56 | private $source; |
||
57 | |||
58 | /** |
||
59 | * @var array|null |
||
60 | */ |
||
61 | private $viewState; |
||
62 | |||
63 | /** |
||
64 | * @var ElementQueryInterface|null |
||
65 | */ |
||
66 | private $elementQuery; |
||
67 | |||
68 | /** |
||
69 | * @var ElementActionInterface[]|null |
||
70 | */ |
||
71 | private $actions; |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | public function init() |
||
77 | { |
||
78 | |||
79 | // Register actions for our 'organization' source |
||
80 | Event::on( |
||
81 | User::class, |
||
82 | User::EVENT_REGISTER_ACTIONS, |
||
83 | function (RegisterElementActionsEvent $event) { |
||
84 | if ($event->source == '*') { |
||
85 | $event->actions = [ |
||
86 | [ |
||
87 | 'type' => RemoveUsers::class, |
||
88 | 'organization' => $event->data['organization'] ?? null |
||
89 | ] |
||
90 | ]; |
||
91 | } |
||
92 | }, |
||
93 | [ |
||
94 | 'organization' => $this->getOrganizationIdFromRequest() |
||
95 | ] |
||
96 | ); |
||
97 | |||
98 | // Register actions for our 'organization' source |
||
99 | Event::on( |
||
100 | User::class, |
||
101 | User::EVENT_REGISTER_SOURCES, |
||
102 | function (RegisterElementSourcesEvent $event) { |
||
103 | if ($event->context == 'index') { |
||
104 | $event->sources = [ |
||
105 | [ |
||
106 | 'key' => '*', |
||
107 | 'label' => Craft::t('organization', 'Organization users'), |
||
108 | 'criteria' => ['status' => null], |
||
109 | 'hasThumbs' => true |
||
110 | ] |
||
111 | ]; |
||
112 | } |
||
113 | } |
||
114 | ); |
||
115 | |||
116 | parent::init(); |
||
117 | |||
118 | $this->elementType = $this->elementType(); |
||
119 | $this->context = $this->context(); |
||
120 | $this->sourceKey = Craft::$app->getRequest()->getParam('source'); |
||
121 | $this->source = $this->source(); |
||
122 | $this->viewState = $this->viewState(); |
||
123 | $this->elementQuery = $this->elementQuery(); |
||
124 | |||
125 | if ($this->context === 'index' && $this->sourceKey !== null) { |
||
126 | $this->actions = $this->availableActions(); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return mixed |
||
132 | */ |
||
133 | private function getOrganizationIdFromRequest() |
||
137 | |||
138 | /** |
||
139 | * THE REST IS COPIED FROM 'craft\controllers\ElementIndexesController' AS EVERYTHING IS PRETTY |
||
140 | * MUCH PRIVATE :( |
||
141 | */ |
||
142 | |||
143 | |||
144 | /** |
||
145 | * Returns the element query that’s defining which elements will be returned in the current request. |
||
146 | * |
||
147 | * Other components can fetch this like so: |
||
148 | * |
||
149 | * ```php |
||
150 | * $criteria = Craft::$app->controller->getElementQuery(); |
||
151 | * ``` |
||
152 | * |
||
153 | * @return ElementQueryInterface |
||
154 | */ |
||
155 | public function getElementQuery(): ElementQueryInterface |
||
159 | |||
160 | /** |
||
161 | * Renders and returns an element index container, plus its first batch of elements. |
||
162 | * |
||
163 | * @return Response |
||
164 | */ |
||
165 | public function actionGetElements(): Response |
||
172 | |||
173 | /** |
||
174 | * Renders and returns a subsequent batch of elements for an element index. |
||
175 | * |
||
176 | * @return Response |
||
177 | */ |
||
178 | public function actionGetMoreElements(): Response |
||
184 | |||
185 | /** |
||
186 | * Performs an action on one or more selected elements. |
||
187 | * |
||
188 | * @return Response |
||
189 | * @throws BadRequestHttpException if the requested element action is not supported by the element type, |
||
190 | * or its parameters didn’t validate |
||
191 | */ |
||
192 | public function actionPerformAction(): Response |
||
279 | |||
280 | /** |
||
281 | * Returns the source tree HTML for an element index. |
||
282 | */ |
||
283 | public function actionGetSourceTreeHtml() |
||
295 | |||
296 | // Private Methods |
||
297 | // ========================================================================= |
||
298 | |||
299 | /** |
||
300 | * Returns the selected source info. |
||
301 | * |
||
302 | * @return array|null |
||
303 | * @throws ForbiddenHttpException if the user is not permitted to access the requested source |
||
304 | */ |
||
305 | private function source() |
||
320 | |||
321 | /** |
||
322 | * Returns the current view state. |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | private function viewState(): array |
||
336 | |||
337 | /** |
||
338 | * Returns the element query based on the current params. |
||
339 | * |
||
340 | * @return ElementQueryInterface |
||
341 | */ |
||
342 | private function elementQuery(): ElementQueryInterface |
||
406 | |||
407 | /** |
||
408 | * Returns the element data to be returned to the client. |
||
409 | * |
||
410 | * @param bool $includeContainer Whether the element container should be included in the response data |
||
411 | * @param bool $includeActions Whether info about the available actions should be included in the response data |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | private function elementResponseData(bool $includeContainer, bool $includeActions): array |
||
448 | |||
449 | /** |
||
450 | * Returns the available actions for the current source. |
||
451 | * |
||
452 | * @return ElementActionInterface[]|null |
||
453 | */ |
||
454 | private function availableActions() |
||
477 | |||
478 | /** |
||
479 | * Returns the data for the available actions. |
||
480 | * |
||
481 | * @return array|null |
||
482 | */ |
||
483 | private function actionData() |
||
504 | } |
||
505 |