Completed
Push — master ( 24a31f...40239d )
by Igor
09:42
created

IndexController   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 302
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 80.25%

Importance

Changes 13
Bugs 2 Features 3
Metric Value
wmc 40
c 13
b 2
f 3
lcom 1
cbo 13
dl 0
loc 302
ccs 126
cts 157
cp 0.8025
rs 8.2608

13 Methods

Rating   Name   Duplication   Size   Complexity  
B behaviors() 0 27 1
A actions() 0 13 1
A successCallback() 0 8 1
A actionIndex() 0 4 1
A actionLogin() 0 15 4
B actionSignup() 0 33 4
C actionSignupProvider() 0 61 10
A actionConfirmEmail() 0 20 3
B actionRequestPasswordReset() 0 29 4
B actionResetPassword() 0 27 5
A actionLogout() 0 5 1
A actionMaintenance() 0 9 2
B actionConfirmRequest() 0 29 3

How to fix   Complexity   

Complex Class

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
2
3
namespace app\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\filters\AccessControl;
8
use app\components\BaseController;
9
use app\helpers\Http;
10
use app\models\User;
11
use app\models\UserProvider;
12
use app\models\forms\LoginForm;
13
use app\models\forms\SignupForm;
14
use app\models\forms\SignupProviderForm;
15
use app\models\forms\PasswordResetRequestForm;
16
use app\models\forms\ResetPasswordForm;
17
use app\models\forms\ConfirmEmailForm;
18
19
class IndexController extends BaseController
20
{
21
    /**
22
     * @inheritdoc
23
     */
24 57
    public function behaviors()
25
    {
26
        return [
27
            'access' => [
28 57
                'class' => AccessControl::className(),
29
                'only' => ['logout', 'signup', 'auth'],
30
                'rules' => [
31
                    [
32
                        'actions' => ['signup', 'auth'],
33
                        'allow' => true,
34
                        'roles' => ['?'],
35
                    ],
36
                    [
37
                        'actions' => ['logout', 'confirm-request'],
38
                        'allow' => true,
39
                        'roles' => ['@'],
40
                    ],
41
                ],
42 57
            ],
43
            'verbs' => [
44 57
                'class' => VerbFilter::className(),
45
                'actions' => [
46
                    'logout' => ['post'],
47
                ],
48
            ],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 57
    public function actions()
56
    {
57
        return [
58
            'error' => [
59
                'class' => 'yii\web\ErrorAction',
60 57
            ],
61
            'auth' => [
62 57
                'class' => 'yii\authclient\AuthAction',
63 57
                'successCallback' => [$this, 'successCallback'],
64 57
                'successUrl' => 'signup-provider'
65
            ],
66
        ];
67
    }
68
69 8
    public function successCallback($client)
70
    {
71 8
        Yii::$app->session['provider'] = [
72 8
            'type' => UserProvider::getTypeByName($client->id),
73 8
            'profile' => $client->getUserAttributes(),
74 8
            'token' => $client->getAccessToken()->getParams(),
75
        ];
76 8
    }
77
78 25
    public function actionIndex()
79
    {
80 25
        return $this->render('index');
81
    }
82
83 16
    public function actionLogin()
84
    {
85 16
        if (!Yii::$app->user->isGuest) {
86 1
            return $this->goHome();
87
        }
88
89 16
        $model = new LoginForm();
90 16
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
91 3
            return $this->goBack();
92
        } else {
93 16
            return $this->render('login', [
94 16
                'model' => $model,
95
            ]);
96
        }
97
    }
98
99 15
    public function actionSignup()
100
    {
101 15
        $model = new SignupForm();
102 15
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
103 3
            if ($model->sendEmail()) {
104
                Yii::$app->session->setFlash(
105
                    'success',
106
                    Yii::t(
107
                        'app.messages',
108
                        'Please activate your account'
109
                    ) . '. ' .
110
                    Yii::t(
111
                        'app.messages',
112
                        'A letter for activation was sent to {email}',
113
                        ['email' => $model->email]
114
                    )
115
                );
116
            } else {
117 3
                Yii::$app->session->setFlash(
118 3
                    'error',
119 3
                    Yii::t(
120 3
                        'app.messages',
121 3
                        'An error occurred while sending a message to activate account'
122
                    )
123
                );
124
            }
125 3
            return $this->goHome();
126
        }
127
128 15
        return $this->render('signup', [
129 15
            'model' => $model,
130
        ]);
131
    }
132
133 9
    public function actionSignupProvider()
134
    {
135 9
        $session = Yii::$app->session;
136 9
        $provider = $session['provider'];
137 9
        if (!Yii::$app->user->isGuest || $provider === null) {
138 1
            return $this->goHome();
139
        }
140
141 8
        $model = new SignupProviderForm($provider);
142
143
        // check exist user and provider
144 8
        if ($user = User::findByProvider($provider['type'], $provider['profile']['id'])) {
145 2
            $session['provider'] = null;
146 2
            if ($user->isActive()) {
147 1
                $user->updateProvider($model->parseProvider());
0 ignored issues
show
Bug introduced by
The method updateProvider cannot be called on $user (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
148 1
                $user->authorize(true);
149 1
                return $this->goHome();
150
            } else {
151 1
                $session->setFlash('error', $user->getStatusDescription());
0 ignored issues
show
Bug introduced by
The method getStatusDescription cannot be called on $user (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method setFlash cannot be called on $session (of type array<string,null,{"provider":"null"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
152 1
                return $this->goHome();
153
            }
154
        }
155
156 8
        $model->prepareUser();
157
158 8
        if ($model->isVerifiedEmail() && $model->signup(false)) {
159 1
            $session['provider'] = null;
160 1
            return $this->goHome();
161
        }
162
163 7
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
164 5
            if ($model->sendEmail()) {
165
                $session->setFlash(
166
                    'success',
167
                    Yii::t(
168
                        'app.messages',
169
                        'Please activate your account'
170
                    ) . '. ' .
171
                    Yii::t(
172
                        'app.messages',
173
                        'A letter for activation was sent to {email}',
174
                        ['email' => $model->email]
175
                    )
176
                );
177
            } else {
178 5
                $session->setFlash(
179 5
                    'error',
180 5
                    Yii::t(
181 5
                        'app.messages',
182 5
                        'An error occurred while sending a message to activate account'
183
                    )
184
                );
185
            }
186 5
            $session['provider'] = null;
187 5
            return $this->goHome();
188
        }
189
190 7
        return $this->render('signupProvider', [
191 7
            'model' => $model
192
        ]);
193
    }
194
195 3
    public function actionConfirmRequest()
196
    {
197 3
        $user = Yii::$app->user->identity;
198 3
        if ($user->isConfirmed()) {
199 1
            Http::exception(403);
200
        } // @codeCoverageIgnore
201
202 2
        $model = new ConfirmEmailForm();
203
204 2
        if ($model->sendEmail($user)) {
205
            Yii::$app->session->setFlash(
206
                'success',
207
                Yii::t(
208
                    'app.messages',
209
                    'A letter for activation was sent to {email}',
210
                    ['email' => $user->email]
211
                )
212
            );
213
        } else {
214 2
            Yii::$app->session->setFlash(
215 2
                'error',
216 2
                Yii::t(
217 2
                    'app.messages',
218 2
                    'An error occurred while sending a message to activate account'
219
                )
220
            );
221
        }
222 2
        return $this->goHome();
223
    }
224
225 3
    public function actionConfirmEmail($token)
226
    {
227 3
        $model = new ConfirmEmailForm();
228
229 3
        if (!$model->validateToken($token)) {
230 2
            Yii::$app->session->setFlash(
231 2
                'error',
232 2
                Yii::t('app.messages', 'Invalid link for activate account')
233
            );
234 2
            return $this->goHome();
235
        }
236
237 1
        if ($model->confirmEmail()) {
238 1
            Yii::$app->session->setFlash(
239 1
                'success',
240 1
                Yii::t('app.messages', 'Your account is successfully activated')
241
            );
242
        }
243 1
        return $this->goHome();
244
    }
245
246 8
    public function actionRequestPasswordReset()
247
    {
248 8
        $model = new PasswordResetRequestForm();
249
250 8
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
251 2
            if ($model->sendEmail()) {
252
                Yii::$app->session->setFlash(
253
                    'success',
254
                    Yii::t(
255
                        'app.messages',
256
                        'We\'ve sent you an email with instructions to reset your password'
257
                    )
258
                );
259
            } else {
260 2
                Yii::$app->session->setFlash(
261 2
                    'error',
262 2
                    Yii::t(
263 2
                        'app.messages',
264 2
                        'An error occurred while sending a message to reset your password'
265
                    )
266
                );
267
            }
268 2
            return $this->goHome();
269
        }
270
271 8
        return $this->render('requestPasswordResetToken', [
272 8
            'model' => $model,
273
        ]);
274
    }
275
276 6
    public function actionResetPassword($token)
277
    {
278 6
        $model = new ResetPasswordForm();
279
280 6
        if (!$model->validateToken($token)) {
281 2
            Yii::$app->session->setFlash(
282 2
                'error',
283 2
                Yii::t('app.messages', 'Invalid link for reset password')
284
            );
285 2
            return $this->goHome();
286
        }
287
288 6
        if ($model->load(Yii::$app->request->post()) &&
289 6
            $model->validate() &&
290 6
            $model->resetPassword()
291
        ) {
292 1
            Yii::$app->session->setFlash(
293 1
                'success',
294 1
                Yii::t('app', 'New password was saved')
295
            );
296 1
            return $this->goHome();
297
        }
298
299 6
        return $this->render('resetPassword', [
300 6
            'model' => $model,
301
        ]);
302
    }
303
304 1
    public function actionLogout()
305
    {
306 1
        Yii::$app->user->logout();
307 1
        return $this->goHome();
308
    }
309
310
    /** @see commands/MaintenanceController **/
311 2
    public function actionMaintenance()
312
    {
313 2
        if (!Yii::$app->catchAll) {
314 1
            Http::exception(404);
315
        } // @codeCoverageIgnore
316
317 1
        $this->layout = 'maintenance';
318 1
        return $this->render('maintenance');
319
    }
320
}
321