SiteController   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 131
Duplicated Lines 16.03 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 21
loc 131
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 21 21 1
A actions() 0 9 1
A actionIndex() 0 4 1
A actionLogin() 0 17 4
A actionLogout() 0 6 1
A actionRequestPasswordReset() 0 17 4
A actionResetPassword() 0 18 5
A actionEditor() 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\controllers;
4
5
use yii;
6
use yii\web\Controller;
7
use yii\filters\AccessControl;
8
use zacksleo\yii2\backend\models\forms\LoginForm;
9
use zacksleo\yii2\backend\models\forms\PasswordResetRequestForm;
10
use zacksleo\yii2\backend\models\forms\ResetPasswordForm;
11
12
/**
13
 * Default controller for the `backend` module
14
 */
15
class SiteController extends Controller
16
{
17
    public $layout = '@vendor/zacksleo/yii2-backend/src/views/layouts/layout';
18
    public $_viewPath = '@vendor/zacksleo/yii2-backend/src/views/site/';
19
20 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...
21
    {
22
        return [
23
            'access' => [
24
                'class' => AccessControl::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
25
                'only' => ['login', 'logout', 'index'],
26
                'rules' => [
27
                    [
28
                        'allow' => true,
29
                        'actions' => ['login'],
30
                        'roles' => ['?'],
31
                    ],
32
                    [
33
                        'allow' => true,
34
                        'actions' => ['index', 'logout'],
35
                        'roles' => ['@'],
36
                    ]
37
                ],
38
            ]
39
        ];
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function actions()
46
    {
47
        return [
48
            'error' => [
49
                'class' => 'yii\web\ErrorAction',
50
                'view' => '@vendor/zacksleo/yii2-backend/src/views/site/error'
51
            ],
52
        ];
53
    }
54
55
    /**
56
     * Renders the index view for the module
57
     * @return string
58
     */
59
    public function actionIndex()
60
    {
61
        return $this->render($this->_viewPath . 'index');
62
    }
63
64
    public function actionLogin()
65
    {
66
        if (!\Yii::$app->user->isGuest) {
67
            return $this->goHome();
68
        }
69
70
        $model = new LoginForm();
71
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
72
            return $this->goBack();
73
        } else {
74
            $this->layout = 'page';
75
            Yii::$app->params['bodyClass'] = 'login';
76
            return $this->render($this->_viewPath . 'login', [
77
                'model' => $model,
78
            ]);
79
        }
80
    }
81
82
    /**
83
     * User logout
84
     */
85
    public function actionLogout()
86
    {
87
        Yii::$app->user->logout();
88
89
        return $this->goHome();
90
    }
91
92
    /**
93
     * Requests password reset.
94
     *
95
     * @return mixed
96
     */
97
    public function actionRequestPasswordReset()
98
    {
99
        $model = new PasswordResetRequestForm();
100
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
101
            if ($model->sendEmail()) {
102
                Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
103
104
                return $this->goHome();
105
            } else {
106
                Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
107
            }
108
        }
109
110
        return $this->render($this->_viewPath . 'requestPasswordReset', [
111
            'model' => $model,
112
        ]);
113
    }
114
115
    /**
116
     * Resets password.
117
     *
118
     * @param string $token
119
     * @return mixed
120
     * @throws BadRequestHttpException
121
     */
122
    public function actionResetPassword($token)
123
    {
124
        try {
125
            $model = new ResetPasswordForm($token);
126
        } catch (InvalidParamException $e) {
0 ignored issues
show
Bug introduced by
The class zacksleo\yii2\backend\co...s\InvalidParamException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
127
            throw new BadRequestHttpException($e->getMessage());
128
        }
129
130
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
131
            Yii::$app->session->setFlash('success', 'New password saved.');
132
133
            return $this->goHome();
134
        }
135
136
        return $this->render($this->_viewPath . 'resetPassword', [
137
            'model' => $model,
138
        ]);
139
    }
140
141
    public function actionEditor()
142
    {
143
        return $this->render($this->_viewPath . 'editor');
144
    }
145
}
146