Total Complexity | 42 |
Total Lines | 250 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SiteController 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.
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 SiteController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class SiteController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * @inheritdoc |
||
24 | */ |
||
25 | public function behaviors() |
||
26 | { |
||
27 | return [ |
||
28 | 'access' => [ |
||
29 | 'class' => AccessControl::class, |
||
30 | 'rules' => [ |
||
31 | [ |
||
32 | 'actions' => ['index', 'error', 'privacy', 'terms', 'about', 'captcha', 'contact', 'faq', 'change-email'], |
||
33 | 'allow' => true, |
||
34 | ], |
||
35 | [ |
||
36 | 'actions' => ['login', 'signup', 'reset-password', 'request-password-reset', 'verify-email'], |
||
37 | 'allow' => true, |
||
38 | 'roles' => ['?'], |
||
39 | ], |
||
40 | [ |
||
41 | 'actions' => ['logout', 'welcome'], |
||
42 | 'allow' => true, |
||
43 | 'roles' => ['@'], |
||
44 | ], |
||
45 | ], |
||
46 | ], |
||
47 | 'verbs' => [ |
||
48 | 'class' => VerbFilter::class, |
||
49 | 'actions' => [ |
||
50 | 'logout' => ['post'], |
||
51 | ], |
||
52 | ], |
||
53 | ]; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @inheritdoc |
||
58 | */ |
||
59 | public function actions() |
||
60 | { |
||
61 | return [ |
||
62 | 'error' => [ |
||
63 | 'class' => 'yii\web\ErrorAction', |
||
64 | ], |
||
65 | 'captcha' => [ |
||
66 | 'class' => 'yii\captcha\CaptchaAction', |
||
67 | ], |
||
68 | ]; |
||
69 | } |
||
70 | |||
71 | public function actionIndex() |
||
85 | } |
||
86 | |||
87 | public function actionLogin() |
||
88 | { |
||
89 | $model = Yii::$container->get(\common\models\LoginForm::class); |
||
90 | if ($model->load(Yii::$app->request->post()) && $model->login()) { |
||
91 | return $this->goBack(); |
||
92 | } else { |
||
93 | return $this->render('login', [ |
||
94 | 'model' => $model, |
||
95 | ]); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public function actionLogout() |
||
100 | { |
||
101 | Yii::$app->user->logout(); |
||
|
|||
102 | return $this->goHome(); |
||
103 | } |
||
104 | |||
105 | public function actionContact() |
||
106 | { |
||
107 | $model = new ContactForm(); |
||
108 | if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
||
109 | if($model->sendEmail(Yii::$app->params['adminEmail'])) { |
||
110 | Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.'); |
||
111 | } else { |
||
112 | Yii::$app->session->setFlash('error', 'There was an error sending email.'); |
||
113 | } |
||
114 | |||
115 | return $this->refresh(); |
||
116 | } else { |
||
117 | return $this->render('contact', [ |
||
118 | 'model' => $model, |
||
119 | ]); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | public function actionAbout() |
||
124 | { |
||
125 | return $this->render('about'); |
||
126 | } |
||
127 | |||
128 | public function actionFaq() |
||
129 | { |
||
130 | return $this->render('faq'); |
||
131 | } |
||
132 | |||
133 | public function actionWelcome() |
||
134 | { |
||
135 | return $this->render('welcome'); |
||
136 | } |
||
137 | |||
138 | public function actionSignup() |
||
139 | { |
||
140 | $model = Yii::$container->get(\site\models\SignupForm::class); |
||
141 | if($model->load(Yii::$app->request->post()) && $model->validate()) { |
||
142 | $model->signup(); |
||
143 | Yii::$app->getSession()->setFlash('success', 'We have sent a verification email to the email address you provided. Please check your inbox and follow the instructions to verify your account.'); |
||
144 | return $this->redirect('/',302); |
||
145 | } |
||
146 | |||
147 | return $this->render('signup', [ |
||
148 | 'model' => $model, |
||
149 | ]); |
||
150 | } |
||
151 | |||
152 | public function actionRequestPasswordReset() |
||
167 | ]); |
||
168 | } |
||
169 | |||
170 | public function actionResetPassword($token) |
||
171 | { |
||
172 | try { |
||
173 | $model = Yii::$container->get(\site\models\ResetPasswordForm::class, [$token]); |
||
174 | } catch (InvalidParamException $e) { |
||
175 | throw new BadRequestHttpException($e->getMessage()); |
||
176 | } |
||
177 | |||
178 | if ($model->load(Yii::$app->request->post()) |
||
179 | && $model->validate() |
||
180 | && $model->resetPassword()) { |
||
181 | Yii::$app->getSession()->setFlash('success', 'New password was saved.'); |
||
182 | return $this->goHome(); |
||
183 | } |
||
184 | |||
185 | return $this->render('resetPassword', [ |
||
186 | 'model' => $model, |
||
187 | ]); |
||
188 | } |
||
189 | |||
190 | public function actionVerifyEmail($token) |
||
209 | } |
||
210 | } |
||
211 | |||
212 | public function actionChangeEmail($token) |
||
213 | { |
||
214 | if (empty($token) || !is_string($token)) { |
||
215 | throw new BadRequestHttpException('The email change token cannot be blank.'); |
||
216 | } |
||
217 | |||
218 | $user = Yii::$container->get(\common\interfaces\UserInterface::class)->findByChangeEmailToken($token); |
||
219 | var_dump( $user); |
||
220 | exit(); |
||
221 | if (!$user) { |
||
222 | throw new BadRequestHttpException("Wrong or expired email change token. If you aren't sure why this error occurs perhaps you've already confirmed your change of email. Please try logging in."); |
||
223 | } |
||
224 | |||
225 | $user->email = $user->desired_email; |
||
226 | $user->save(); |
||
227 | // log out the user (if they're logged in) |
||
228 | |||
229 | Yii::$app->getSession()->setFlash('success', 'Your email address has been successfully changed. Please log in.'); |
||
230 | return $this->redirect('/login',302); |
||
231 | } |
||
232 | |||
233 | public function actionPrivacy() |
||
234 | { |
||
235 | return $this->render('privacy'); |
||
236 | } |
||
237 | |||
238 | public function actionTerms() |
||
241 | } |
||
242 | |||
243 | public function actionExport() |
||
270 | } |
||
271 | } |
||
272 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.