Completed
Push — master ( 55e0f0...d0f882 )
by Alexey
11:22
created

DefaultController::actionResetPassword()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 3
nop 1
1
<?php
2
namespace modules\users\controllers\frontend;
3
4
use Yii;
5
use yii\web\Controller;
6
use modules\users\models\User;
7
use modules\users\models\SignupForm;
8
use modules\users\models\LoginForm;
9
use modules\users\models\EmailConfirmForm;
10
use modules\users\models\ResetPasswordForm;
11
use modules\users\models\PasswordResetRequestForm;
12
use yii\base\InvalidParamException;
13
use yii\web\BadRequestHttpException;
14
use yii\filters\VerbFilter;
15
use yii\web\NotFoundHttpException;
16
use modules\users\Module;
17
18
/**
19
 * Class DefaultController
20
 * @package modules\users\controllers\frontend
21
 */
22
class DefaultController extends Controller
23
{
24
    /**
25
     * @return array
26
     */
27
    public function behaviors()
28
    {
29
        return [
30
            'verbs' => [
31
                'class' => VerbFilter::className(),
32
                'actions' => [
33
                    'logout' => ['post'],
34
                    'delete' => ['post'],
35
                ],
36
            ],
37
        ];
38
    }
39
40
    /**
41
     * @return string
42
     * @throws NotFoundHttpException
43
     */
44
    public function actionIndex()
45
    {
46
        return $this->render('index', [
47
            'model' => $this->findModel(),
48
        ]);
49
    }
50
51
    /**
52
     * Logs in a user.
53
     *
54
     * @return mixed
55
     */
56
    public function actionLogin()
57
    {
58
        if (!Yii::$app->user->isGuest) {
59
            return $this->goHome();
60
        }
61
62
        $model = new LoginForm();
63
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
64
            return $this->goBack();
65
        } else {
66
            return $this->render('login', [
67
                'model' => $model,
68
            ]);
69
        }
70
    }
71
72
    /**
73
     * Logs out the current user.
74
     *
75
     * @return mixed
76
     */
77
    public function actionLogout()
78
    {
79
        Yii::$app->user->logout();
80
81
        return $this->goHome();
82
    }
83
84
    /**
85
     * Signs user up.
86
     *
87
     * @return mixed
88
     */
89
    public function actionSignup()
90
    {
91
        $model = new SignupForm();
92
        if ($model->load(Yii::$app->request->post())) {
93
            if ($user = $model->signup()) {
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
94
                Yii::$app->getSession()->setFlash('success', Module::t('module', 'It remains to activate the account.'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
95
                return $this->goHome();
96
            }
97
        }
98
        return $this->render('signup', [
99
            'model' => $model,
100
        ]);
101
    }
102
103
    /**
104
     * @param $token
105
     * @return \yii\web\Response
106
     * @throws BadRequestHttpException
107
     */
108
    public function actionEmailConfirm($token)
109
    {
110
        try {
111
            $model = new EmailConfirmForm($token);
112
        } catch (InvalidParamException $e) {
113
            throw new BadRequestHttpException($e->getMessage());
114
        }
115
116
        if ($model->confirmEmail()) {
117
            Yii::$app->getSession()->setFlash('success', Module::t('module', 'Thank you for registering!'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
118
        } else {
119
            Yii::$app->getSession()->setFlash('error', Module::t('module', 'Error sending message!'));
120
        }
121
122
        return $this->goHome();
123
    }
124
125
    /**
126
     * Requests password reset.
127
     *
128
     * @return mixed
129
     */
130
    public function actionRequestPasswordReset()
131
    {
132
        $model = new PasswordResetRequestForm();
133
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
134
            if ($model->sendEmail()) {
135
                Yii::$app->session->setFlash('success', Module::t('module', 'Check your email for further instructions.'));
136
137
                return $this->goHome();
138
            } else {
139
                Yii::$app->session->setFlash('error', Module::t('module', 'Sorry, we are unable to reset password.'));
140
            }
141
        }
142
143
        return $this->render('requestPasswordResetToken', [
144
            'model' => $model,
145
        ]);
146
    }
147
148
    /**
149
     * Resets password.
150
     *
151
     * @param string $token
152
     * @return mixed
153
     * @throws BadRequestHttpException
154
     */
155
    public function actionResetPassword($token)
156
    {
157
        try {
158
            $model = new ResetPasswordForm($token);
159
        } catch (InvalidParamException $e) {
160
            throw new BadRequestHttpException($e->getMessage());
161
        }
162
163
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
164
            Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
165
            return $this->goHome();
166
        }
167
168
        return $this->render('resetPassword', [
169
            'model' => $model,
170
        ]);
171
    }
172
173
    /**
174
     * Finds the User model based on its primary key value.
175
     * If the model is not found, a 404 HTTP exception will be thrown.
176
     * @return User the loaded model
177
     * @throws NotFoundHttpException if the model cannot be found
178
     */
179
    protected function findModel()
180
    {
181
        $id = Yii::$app->user->identity->getId();
182
        if (($model = User::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \modules\users\models\User::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 183 which is incompatible with the return type documented by modules\users\controller...ltController::findModel of type modules\users\models\User.
Loading history...
183
            return $model;
184
        } else {
185
            throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
186
        }
187
    }
188
}
189