Completed
Push — master ( 8354e3...c94616 )
by zacksleo
06:12
created

SiteController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 25.3 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
dl 21
loc 83
ccs 15
cts 19
cp 0.7895
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 21 21 1
A actions() 0 9 1
A actionLogin() 0 5 2
A actionRequestpasswordreset() 0 5 3
A actionResetpassword() 0 10 4
A actionLogout() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace zacksleo\yii2\backend\tests\controllers;
4
5
use yii;
6
use yii\base\InvalidParamException;
7
use yii\web\BadRequestHttpException;
8
use yii\web\Controller;
9
use yii\filters\AccessControl;
10
use zacksleo\yii2\backend\models\forms\LoginForm;
11
use zacksleo\yii2\backend\models\forms\PasswordResetRequestForm;
12
use zacksleo\yii2\backend\models\forms\ResetPasswordForm;
13
14
/**
15
 * Default controller for the `backend` module
16
 */
17
class SiteController extends Controller
18
{
19
    public $layout = '@vendor/zacksleo/yii2-backend/src/views/layouts/layout';
20
    public $_viewPath = '@vendor/zacksleo/yii2-backend/src/views/site/';
21
22 4 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...
23
    {
24
        return [
25 4
            'access' => [
26 4
                'class' => AccessControl::className(),
27
                'only' => ['login', 'logout', 'index'],
28
                'rules' => [
29
                    [
30
                        'allow' => true,
31
                        'actions' => ['login'],
32
                        'roles' => ['?'],
33
                    ],
34
                    [
35
                        'allow' => true,
36
                        'actions' => ['index', 'logout'],
37
                        'roles' => ['@'],
38
                    ]
39
                ],
40
            ]
41
        ];
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 4
    public function actions()
48
    {
49
        return [
50 4
            'error' => [
51
                'class' => 'yii\web\ErrorAction',
52
                'view' => '@vendor/zacksleo/yii2-backend/src/views/site/error'
53
            ],
54
        ];
55
    }
56
57 2
    public function actionLogin()
58
    {
59 2
        $model = new LoginForm();
60 2
        return $model->load(Yii::$app->request->post()) && $model->login();
61
    }
62
63
    /**
64
     * User logout
65
     */
66
    public function actionLogout()
67
    {
68
        return Yii::$app->user->logout();
69
    }
70
71
    /**
72
     * Requests password reset.
73
     *
74
     * @return mixed
75
     */
76 1
    public function actionRequestpasswordreset()
77
    {
78 1
        $model = new PasswordResetRequestForm();
79 1
        return $model->load(Yii::$app->request->bodyParams) && $model->validate() && $model->sendEmail();
80
    }
81
82
    /**
83
     * Resets password.
84
     *
85
     * @param string $token
86
     * @return mixed
87
     * @throws BadRequestHttpException
88
     */
89 1
    public function actionResetpassword($token)
90
    {
91
        try {
92 1
            $model = new ResetPasswordForm($token);
93
        } catch (InvalidParamException $e) {
94
            throw new BadRequestHttpException($e->getMessage());
95
        }
96 1
        $model->load(Yii::$app->request->bodyParams);
97 1
        return $model->load(Yii::$app->request->bodyParams) && $model->validate() && $model->resetPassword();
98
    }
99
}
100