Complex classes like IndexController 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 IndexController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class IndexController extends BaseController |
||
20 | { |
||
21 | /** |
||
22 | * @inheritdoc |
||
23 | */ |
||
24 | public function behaviors() |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function actions() |
||
68 | |||
69 | public function successCallback($provider) |
||
70 | { |
||
71 | Yii::$app->session['provider'] = null; |
||
72 | Yii::$app->session['blocked'] = false; |
||
73 | |||
74 | $type = UserProvider::getTypeByName($provider->id); |
||
75 | $profile = $provider->getUserAttributes(); |
||
76 | $token = $provider->getAccessToken()->getParams(); |
||
77 | $data = [ |
||
78 | 'type' => $type, |
||
79 | 'profile' => $profile, |
||
80 | 'token' => $token |
||
81 | ]; |
||
82 | |||
83 | if ($user = User::findByProvider($type, $profile['id'])) { |
||
84 | if ($user->isActive()) { |
||
85 | $user->updateProvider(UserProvider::parseProvider($type, $data)); |
||
86 | $user->authorize(true); |
||
87 | } else { |
||
88 | Yii::$app->session['blocked'] = true; |
||
89 | Yii::$app->session['message'] = $user->getStatusDescription(); |
||
|
|||
90 | } |
||
91 | } else { |
||
92 | Yii::$app->session['provider'] = $data; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function actionIndex() |
||
97 | { |
||
98 | return $this->render('index'); |
||
99 | } |
||
100 | |||
101 | public function actionLogin() |
||
102 | { |
||
103 | if (!Yii::$app->user->isGuest) { |
||
104 | return $this->goHome(); |
||
105 | } |
||
106 | |||
107 | $model = new LoginForm(); |
||
108 | if ($model->load(Yii::$app->request->post()) && $model->login()) { |
||
109 | return $this->goBack(); |
||
110 | } else { |
||
111 | return $this->render('login', [ |
||
112 | 'model' => $model, |
||
113 | ]); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | public function actionSignup() |
||
118 | { |
||
119 | $model = new SignupForm(); |
||
120 | if ($model->load(Yii::$app->request->post()) && $model->signup()) { |
||
121 | if ($model->sendEmail()) { |
||
122 | return $this->alert( |
||
123 | 'success', |
||
124 | Yii::t('app.messages', 'Please activate your account') . '. ' . |
||
125 | Yii::t('app.messages', 'A letter for activation was sent to {email}', ['email' => $model->email]) |
||
126 | ); |
||
127 | } else { |
||
128 | return $this->alert( |
||
129 | 'error', |
||
130 | Yii::t('app.messages', 'An error occurred while sending a message to activate account') |
||
131 | ); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | return $this->render('signup', [ |
||
136 | 'model' => $model, |
||
137 | ]); |
||
138 | } |
||
139 | |||
140 | public function actionSignupProvider() |
||
141 | { |
||
142 | if (Yii::$app->session['blocked']) { |
||
143 | return $this->alert('error', Yii::$app->session['message']); |
||
144 | } |
||
145 | |||
146 | if (!Yii::$app->user->isGuest || Yii::$app->session['provider'] === null) { |
||
147 | return $this->goHome(); |
||
148 | } |
||
149 | |||
150 | $model = new SignupProviderForm(Yii::$app->session['provider']); |
||
151 | |||
152 | if ($model->isVerified() && $model->signup(false)) { |
||
153 | Yii::$app->session['provider'] = null; |
||
154 | return $this->goHome(); |
||
155 | } |
||
156 | |||
157 | if ($model->load(Yii::$app->request->post()) && $model->signup()) { |
||
158 | Yii::$app->session['provider'] = null; |
||
159 | if ($model->sendEmail()) { |
||
160 | return $this->alert( |
||
161 | 'success', |
||
162 | Yii::t('app.messages', 'Please activate your account') . '. ' . |
||
163 | Yii::t('app.messages', 'A letter for activation was sent to {email}', ['email' => $model->email]) |
||
164 | ); |
||
165 | } else { |
||
166 | return $this->alert( |
||
167 | 'error', |
||
168 | Yii::t('app.messages', 'An error occurred while sending a message to activate account') |
||
169 | ); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return $this->render('signupProvider', [ |
||
174 | 'model' => $model |
||
175 | ]); |
||
176 | } |
||
177 | |||
178 | public function actionConfirmRequest() |
||
179 | { |
||
180 | $user = Yii::$app->user->identity; |
||
181 | if ($user->isConfirmed()) { |
||
182 | Http::exception(403); |
||
183 | } // @codeCoverageIgnore |
||
184 | |||
185 | $model = new ConfirmEmailForm(); |
||
186 | |||
187 | if ($model->sendEmail($user)) { |
||
188 | return $this->alert( |
||
189 | 'success', |
||
190 | Yii::t('app.messages', 'A letter for activation was sent to {email}', [ |
||
191 | 'email' => $user->email |
||
192 | ]) |
||
193 | ); |
||
194 | } else { |
||
195 | return $this->alert( |
||
196 | 'error', |
||
197 | Yii::t('app.messages', 'An error occurred while sending a message to activate account') |
||
198 | ); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | public function actionConfirmEmail($token) |
||
203 | { |
||
204 | $model = new ConfirmEmailForm(); |
||
205 | |||
206 | if (!$model->validateToken($token)) { |
||
207 | return $this->alert('error', Yii::t('app.messages', 'Invalid link for activate account')); |
||
208 | } |
||
209 | |||
210 | if ($model->confirmEmail()) { |
||
211 | return $this->alert( |
||
212 | 'success', |
||
213 | Yii::t('app.messages', 'Your account is successfully activated') |
||
214 | ); |
||
215 | } else { |
||
216 | return $this->alert( |
||
217 | 'error', |
||
218 | Yii::t('app.messages', 'An error occurred while activating account') |
||
219 | ); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | public function actionRequestPasswordReset() |
||
224 | { |
||
225 | $model = new PasswordResetRequestForm(); |
||
226 | |||
227 | if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
||
228 | if ($model->sendEmail()) { |
||
229 | return $this->alert( |
||
230 | 'success', |
||
231 | Yii::t('app.messages', 'We\'ve sent you an email with instructions to reset your password') |
||
232 | ); |
||
233 | } else { |
||
234 | return $this->alert( |
||
235 | 'error', |
||
236 | Yii::t('app.messages', 'An error occurred while sending a message to reset your password') |
||
237 | ); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | return $this->render('requestPasswordResetToken', [ |
||
242 | 'model' => $model, |
||
243 | ]); |
||
244 | } |
||
245 | |||
246 | public function actionResetPassword($token) |
||
247 | { |
||
248 | $model = new ResetPasswordForm(); |
||
249 | |||
250 | if (!$model->validateToken($token)) { |
||
251 | return $this->alert('error', Yii::t('app.messages', 'Invalid link for reset password')); |
||
252 | } |
||
253 | |||
254 | if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { |
||
255 | return $this->alert('success', Yii::t('app', 'New password was saved')); |
||
256 | } |
||
257 | |||
258 | return $this->render('resetPassword', [ |
||
259 | 'model' => $model, |
||
260 | ]); |
||
261 | } |
||
262 | |||
263 | public function actionLogout() |
||
268 | |||
269 | /** @see commands/MaintenanceController **/ |
||
270 | public function actionMaintenance() |
||
279 | } |
||
280 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.