SiteController::actionResetPassword()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 18
rs 8.8571
cc 5
eloc 10
nc 3
nop 1
1
<?php
2
namespace frontend\controllers;
3
4
use Yii;
5
use common\models\LoginForm;
6
use frontend\models\PasswordResetRequestForm;
7
use frontend\models\ResetPasswordForm;
8
use frontend\models\SignupForm;
9
use frontend\models\ContactForm;
10
use yii\base\InvalidParamException;
11
use yii\web\BadRequestHttpException;
12
use yii\web\Controller;
13
use yii\filters\VerbFilter;
14
use yii\filters\AccessControl;
15
16
/**
17
 * Site controller
18
 */
19
class SiteController extends Controller
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function behaviors()
25
    {
26
        return [
27
            'access' => [
28
                'class' => AccessControl::className(),
29
                'only' => ['logout', 'signup'],
30
                'rules' => [
31
                    [
32
                        'actions' => ['signup'],
33
                        'allow' => true,
34
                        'roles' => ['?'],
35
                    ],
36
                    [
37
                        'actions' => ['logout'],
38
                        'allow' => true,
39
                        'roles' => ['@'],
40
                    ],
41
                ],
42
            ],
43
            'verbs' => [
44
                'class' => VerbFilter::className(),
45
                'actions' => [
46
                    'logout' => ['post'],
47
                ],
48
            ],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function actions()
56
    {
57
        return [
58
            'error' => [
59
                'class' => 'yii\web\ErrorAction',
60
            ],
61
            'captcha' => [
62
                'class' => 'yii\captcha\CaptchaAction',
63
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
64
            ],
65
        ];
66
    }
67
68
    /**
69
     * Displays homepage.
70
     *
71
     * @return mixed
72
     */
73
    public function actionIndex()
74
    {
75
        return $this->render('index');
76
    }
77
78
    /**
79
     * Logs in a user.
80
     *
81
     * @return mixed
82
     */
83 View Code Duplication
    public function actionLogin()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        if (!\Yii::$app->user->isGuest) {
86
            return $this->goHome();
87
        }
88
89
        $model = new LoginForm();
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
    /**
100
     * Logs out the current user.
101
     *
102
     * @return mixed
103
     */
104
    public function actionLogout()
105
    {
106
        Yii::$app->user->logout();
107
108
        return $this->goHome();
109
    }
110
111
    /**
112
     * Displays contact page.
113
     *
114
     * @return mixed
115
     */
116 View Code Duplication
    public function actionContact()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $model = new ContactForm();
119
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
120
            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
121
                Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
122
            } else {
123
                Yii::$app->session->setFlash('error', 'There was an error sending email.');
124
            }
125
126
            return $this->refresh();
127
        } else {
128
            return $this->render('contact', [
129
                'model' => $model,
130
            ]);
131
        }
132
    }
133
134
    /**
135
     * Displays about page.
136
     *
137
     * @return mixed
138
     */
139
    public function actionAbout()
140
    {
141
        return $this->render('about');
142
    }
143
144
    /**
145
     * Signs user up.
146
     *
147
     * @return mixed
148
     */
149
    public function actionSignup()
150
    {
151
        $model = new SignupForm();
152
        if ($model->load(Yii::$app->request->post())) {
153
            if ($user = $model->signup()) {
154
                if (Yii::$app->getUser()->login($user)) {
0 ignored issues
show
Bug introduced by
The method getUser 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...
155
                    return $this->goHome();
156
                }
157
            }
158
        }
159
160
        return $this->render('signup', [
161
            'model' => $model,
162
        ]);
163
    }
164
165
    /**
166
     * Requests password reset.
167
     *
168
     * @return mixed
169
     */
170 View Code Duplication
    public function actionRequestPasswordReset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        $model = new PasswordResetRequestForm();
173
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
174
            if ($model->sendEmail()) {
175
                Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
176
177
                return $this->goHome();
178
            } else {
179
                Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
180
            }
181
        }
182
183
        return $this->render('requestPasswordResetToken', [
184
            'model' => $model,
185
        ]);
186
    }
187
188
    /**
189
     * Resets password.
190
     *
191
     * @param string $token
192
     * @return mixed
193
     * @throws BadRequestHttpException
194
     */
195
    public function actionResetPassword($token)
196
    {
197
        try {
198
            $model = new ResetPasswordForm($token);
199
        } catch (InvalidParamException $e) {
200
            throw new BadRequestHttpException($e->getMessage());
201
        }
202
203
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
204
            Yii::$app->session->setFlash('success', 'New password was saved.');
205
206
            return $this->goHome();
207
        }
208
209
        return $this->render('resetPassword', [
210
            'model' => $model,
211
        ]);
212
    }
213
}
214