DefaultController   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 25
eloc 75
c 1
b 0
f 1
dl 0
loc 196
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A actionEmailConfirm() 0 17 3
A actionLogout() 0 5 1
A actionSignup() 0 15 3
A actionLogin() 0 13 4
A actionResetPassword() 0 19 4
A behaviors() 0 18 1
A processGoHome() 0 6 2
A actionRequestPasswordReset() 0 24 4
A processResetPassword() 0 6 3
1
<?php
2
3
namespace modules\users\controllers;
4
5
use Yii;
6
use yii\base\Exception;
7
use yii\filters\AccessControl;
8
use yii\web\Controller;
9
use yii\web\Response;
10
use yii\filters\VerbFilter;
11
use yii\base\InvalidArgumentException;
12
use yii\web\BadRequestHttpException;
13
use modules\users\models\LoginForm;
14
use modules\users\models\PasswordResetRequestForm;
15
use modules\users\models\ResetPasswordForm;
16
use modules\users\models\SignupForm;
17
use modules\users\models\EmailConfirmForm;
18
use modules\users\Module;
19
20
/**
21
 * Class DefaultController
22
 * @package modules\users\controllers
23
 */
24
class DefaultController extends Controller
25
{
26
    /**
27
     * @inheritdoc
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
            'access' => [
33
                'class' => AccessControl::class,
34
                'only' => ['logout'],
35
                'rules' => [
36
                    [
37
                        'actions' => ['logout'],
38
                        'allow' => true,
39
                        'roles' => ['@'],
40
                    ],
41
                ],
42
            ],
43
            'verbs' => [
44
                'class' => VerbFilter::class,
45
                'actions' => [
46
                    'logout' => ['post'],
47
                ],
48
            ],
49
        ];
50
    }
51
52
    /**
53
     * Login action.
54
     *
55
     * @return Response|string
56
     */
57
    public function actionLogin()
58
    {
59
        if (!Yii::$app->user->isGuest) {
60
            return $this->goHome();
61
        }
62
63
        $model = new LoginForm();
64
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
65
            return $this->goBack();
66
        }
67
        $model->password = '';
68
        return $this->render('login', [
69
            'model' => $model,
70
        ]);
71
    }
72
73
    /**
74
     * Logout action.
75
     *
76
     * @return Response
77
     */
78
    public function actionLogout()
79
    {
80
        Yii::$app->user->logout();
0 ignored issues
show
Bug introduced by
The method logout() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        Yii::$app->user->/** @scrutinizer ignore-call */ 
81
                         logout();

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.

Loading history...
81
82
        return $this->goHome();
83
    }
84
85
    /**
86
     * Requests password reset.
87
     *
88
     * @return string|Response
89
     */
90
    public function actionRequestPasswordReset()
91
    {
92
        $model = new PasswordResetRequestForm();
93
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
94
            if ($model->sendEmail()) {
95
                return $this->processGoHome(
96
                    Module::t(
97
                        'module',
98
                        'To the address {:Email} We sent you a letter with further instructions, check mail.',
99
                        [':Email' => $model->email]
100
                    )
101
                );
102
            } else {
103
                Yii::$app->session->setFlash(
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
                Yii::$app->session->/** @scrutinizer ignore-call */ 
104
                                    setFlash(

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.

Loading history...
104
                    'error',
105
                    Module::t(
106
                        'module',
107
                        'Sorry, we are unable to reset password.'
108
                    )
109
                );
110
            }
111
        }
112
        return $this->render('requestPasswordResetToken', [
113
            'model' => $model,
114
        ]);
115
    }
116
117
    /**
118
     * @param string $message
119
     * @param string $type
120
     * @return Response
121
     */
122
    public function processGoHome($message = '', $type = 'success')
123
    {
124
        if (!empty($message)) {
125
            Yii::$app->session->setFlash($type, $message);
126
        }
127
        return $this->goHome();
128
    }
129
130
    /**
131
     * Resets password.
132
     *
133
     * @param string $token
134
     * @return string|Response
135
     * @throws BadRequestHttpException
136
     * @throws Exception
137
     */
138
    public function actionResetPassword($token)
139
    {
140
        try {
141
            $model = new ResetPasswordForm($token);
142
        } catch (InvalidArgumentException $e) {
143
            throw new BadRequestHttpException($e->getMessage());
144
        }
145
146
        if ($model->load(Yii::$app->request->post()) && $this->processResetPassword($model)) {
147
            return $this->processGoHome(
148
                Module::t(
149
                    'module',
150
                    'The new password was successfully saved.'
151
                )
152
            );
153
        }
154
155
        return $this->render('resetPassword', [
156
            'model' => $model,
157
        ]);
158
    }
159
160
    /**
161
     * @param ResetPasswordForm $model
162
     * @return bool
163
     * @throws Exception
164
     */
165
    public function processResetPassword($model)
166
    {
167
        if ($model->validate() && $model->resetPassword()) {
168
            return true;
169
        }
170
        return false;
171
    }
172
173
    /**
174
     * Signs users up.
175
     *
176
     * @return string|Response
177
     * @throws Exception
178
     */
179
    public function actionSignup()
180
    {
181
        $model = new SignupForm();
182
        if ($model->load(Yii::$app->request->post())) {
183
            if ($model->signup()) {
184
                return $this->processGoHome(
185
                    Module::t(
186
                        'module',
187
                        'It remains to activate the account, check your mail.'
188
                    )
189
                );
190
            }
191
        }
192
        return $this->render('signup', [
193
            'model' => $model,
194
        ]);
195
    }
196
197
    /**
198
     * @param string $token
199
     * @return Response
200
     * @throws BadRequestHttpException
201
     * @throws \Exception
202
     */
203
    public function actionEmailConfirm($token)
204
    {
205
        try {
206
            $model = new EmailConfirmForm($token);
207
        } catch (\InvalidArgumentException $e) {
208
            throw new BadRequestHttpException($e->getMessage());
209
        }
210
211
        if ($model->confirmEmail()) {
212
            return $this->processGoHome(
213
                Module::t(
214
                    'module',
215
                    'Thank you for registering! Now you can log in using your credentials.'
216
                )
217
            );
218
        }
219
        return $this->processGoHome(Module::t('module', 'Error sending message!'), 'error');
220
    }
221
}
222