Completed
Push — master ( df46a8...ddc7c0 )
by Misbahul D
03:24
created

UserController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 13
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace mdm\admin\controllers;
4
5
use Yii;
6
use mdm\admin\models\form\Login;
7
use mdm\admin\models\form\PasswordResetRequest;
8
use mdm\admin\models\form\ResetPassword;
9
use mdm\admin\models\form\Signup;
10
use mdm\admin\models\form\ChangePassword;
11
use mdm\admin\models\User;
12
use mdm\admin\models\searchs\User as UserSearch;
13
use yii\base\InvalidParamException;
14
use yii\web\BadRequestHttpException;
15
use yii\web\Controller;
16
use yii\filters\VerbFilter;
17
use yii\web\NotFoundHttpException;
18
use yii\base\UserException;
19
use yii\mail\BaseMailer;
20
21
/**
22
 * User controller
23
 */
24
class UserController extends Controller
25
{
26
    private $_oldMailPath;
27
28
    /**
29
     * @inheritdoc
30
     */
31 View Code Duplication
    public function behaviors()
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...
32
    {
33
        return [
34
            'verbs' => [
35
                'class' => VerbFilter::className(),
36
                'actions' => [
37
                    'delete' => ['post'],
38
                    'logout' => ['post'],
39
                    'activate' => ['post'],
40
                ],
41
            ],
42
        ];
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function beforeAction($action)
49
    {
50
        if (parent::beforeAction($action)) {
51
            if (Yii::$app->has('mailer') && ($mailer = Yii::$app->getMailer()) instanceof BaseMailer) {
52
                /* @var $mailer BaseMailer */
53
                $this->_oldMailPath = $mailer->getViewPath();
54
                $mailer->setViewPath('@mdm/admin/mail');
55
            }
56
            return true;
57
        }
58
        return false;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function afterAction($action, $result)
65
    {
66
        if ($this->_oldMailPath !== null) {
67
            Yii::$app->getMailer()->setViewPath($this->_oldMailPath);
68
        }
69
        return parent::afterAction($action, $result);
70
    }
71
72
    /**
73
     * Lists all User models.
74
     * @return mixed
75
     */
76 View Code Duplication
    public function actionIndex()
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...
77
    {
78
        $searchModel = new UserSearch();
79
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
80
81
        return $this->render('index', [
82
                'searchModel' => $searchModel,
83
                'dataProvider' => $dataProvider,
84
        ]);
85
    }
86
87
    /**
88
     * Displays a single User model.
89
     * @param integer $id
90
     * @return mixed
91
     */
92
    public function actionView($id)
93
    {
94
        return $this->render('view', [
95
                'model' => $this->findModel($id),
96
        ]);
97
    }
98
99
    /**
100
     * Deletes an existing User model.
101
     * If deletion is successful, the browser will be redirected to the 'index' page.
102
     * @param integer $id
103
     * @return mixed
104
     */
105
    public function actionDelete($id)
106
    {
107
        $this->findModel($id)->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->findModel($id) (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...
108
109
        return $this->redirect(['index']);
110
    }
111
112
    /**
113
     * Login
114
     * @return string
115
     */
116
    public function actionLogin()
117
    {
118
        if (!Yii::$app->getUser()->isGuest) {
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...
119
            return $this->goHome();
120
        }
121
122
        $model = new Login();
123
        if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
124
            return $this->goBack();
125
        } else {
126
            return $this->render('login', [
127
                    'model' => $model,
128
            ]);
129
        }
130
    }
131
132
    /**
133
     * Logout
134
     * @return string
135
     */
136
    public function actionLogout()
137
    {
138
        Yii::$app->getUser()->logout();
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...
139
140
        return $this->goHome();
141
    }
142
143
    /**
144
     * Signup new user
145
     * @return string
146
     */
147 View Code Duplication
    public function actionSignup()
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...
148
    {
149
        $model = new Signup();
150
        if ($model->load(Yii::$app->getRequest()->post())) {
151
            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...
152
                return $this->goHome();
153
            }
154
        }
155
156
        return $this->render('signup', [
157
                'model' => $model,
158
        ]);
159
    }
160
161
    /**
162
     * Request reset password
163
     * @return string
164
     */
165
    public function actionRequestPasswordReset()
166
    {
167
        $model = new PasswordResetRequest();
168
        if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {
169
            if ($model->sendEmail()) {
170
                Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
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...
171
172
                return $this->goHome();
173
            } else {
174
                Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
175
            }
176
        }
177
178
        return $this->render('requestPasswordResetToken', [
179
                'model' => $model,
180
        ]);
181
    }
182
183
    /**
184
     * Reset password
185
     * @return string
186
     */
187
    public function actionResetPassword($token)
188
    {
189
        try {
190
            $model = new ResetPassword($token);
191
        } catch (InvalidParamException $e) {
192
            throw new BadRequestHttpException($e->getMessage());
193
        }
194
195
        if ($model->load(Yii::$app->getRequest()->post()) && $model->validate() && $model->resetPassword()) {
196
            Yii::$app->getSession()->setFlash('success', 'New password was saved.');
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...
197
198
            return $this->goHome();
199
        }
200
201
        return $this->render('resetPassword', [
202
                'model' => $model,
203
        ]);
204
    }
205
206
    /**
207
     * Reset password
208
     * @return string
209
     */
210 View Code Duplication
    public function actionChangePassword()
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...
211
    {
212
        $model = new ChangePassword();
213
        if ($model->load(Yii::$app->getRequest()->post()) && $model->change()) {
214
            return $this->goHome();
215
        }
216
217
        return $this->render('change-password', [
218
                'model' => $model,
219
        ]);
220
    }
221
222
    /**
223
     * Activate new user
224
     * @param integer $id
225
     * @return type
226
     * @throws UserException
227
     * @throws NotFoundHttpException
228
     */
229
    public function actionActivate($id)
230
    {
231
        /* @var $user User */
232
        $user = $this->findModel($id);
233
        if ($user->status == User::STATUS_INACTIVE) {
234
            $user->status = User::STATUS_ACTIVE;
235
            if ($user->save()) {
236
                return $this->goHome();
237
            } else {
238
                $errors = $user->firstErrors;
239
                throw new UserException(reset($errors));
240
            }
241
        }
242
        return $this->goHome();
243
    }
244
245
    /**
246
     * Finds the User model based on its primary key value.
247
     * If the model is not found, a 404 HTTP exception will be thrown.
248
     * @param integer $id
249
     * @return User the loaded model
250
     * @throws NotFoundHttpException if the model cannot be found
251
     */
252
    protected function findModel($id)
253
    {
254
        if (($model = User::findOne($id)) !== null) {
255
            return $model;
256
        } else {
257
            throw new NotFoundHttpException('The requested page does not exist.');
258
        }
259
    }
260
}
261