Completed
Branch master (f58c14)
by Andrey
07:50
created

SiteController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use yii\web\Response;
7
use yii\filters\AccessControl;
8
use yii\helpers\ArrayHelper;
9
use app\models\{LoginForm, RegForm};
10
11
/**
12
 * Class SiteController
13
 *
14
 * @package app\controllers
15
 */
16
class SiteController extends BaseController
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function behaviors()
22
    {
23
        return ArrayHelper::merge(parent::behaviors(), [
24
            'access' => [
25
                'class' => AccessControl::class,
26
                'rules' => [
27
                    [
28
                        'allow'   => true,
29
                        'actions' => ['reg', 'login', 'captcha'],
30
                        'roles'   => ['?'],
31
                    ],
32
                    [
33
                        'allow' => true,
34
                        'actions' => ['logout'],
35
                        'roles' => ['@'],
36
                    ],
37
                ],
38
            ],
39
        ]);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 View Code Duplication
    public function actions()
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...
46
    {
47
        return ArrayHelper::merge(parent::actions(), [
48
            'captcha' => [
49
                'class' => 'yii\captcha\CaptchaAction',
50
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
51
            ],
52
        ]);
53
    }
54
55
    /**
56
     * Login action.
57
     *
58
     * @return Response|string
59
     */
60
    public function actionLogin()
61
    {
62
        if (!Yii::$app->user->isGuest) {
63
            return $this->goHome();
64
        }
65
66
        $model = new LoginForm();
67
68
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
69
            return $this->goBack();
70
        }
71
72
        return $this->render('login', [
73
            'model' => $model,
74
        ]);
75
    }
76
77
    /**
78
     * Register action.
79
     *
80
     * @return Response|string
81
     */
82
    public function actionReg()
83
    {
84
        if (!Yii::$app->user->isGuest) {
85
            return $this->goHome();
86
        }
87
88
        /* @var $settingModel \app\models\Setting */
89
        $settingModel = Yii::$app->get('settings')
90
            ->setModel()
91
            ->getSettings();
92
93
        if ($settingModel->regBlock == 1) {
94
            return $this->render('regBlock');
95
        }
96
97
        $model = new RegForm();
98
99
        if ($model->load(Yii::$app->request->post()) && $model->reg()) {
100
101
            if (Yii::$app->getUser()->login($model->getUser())) {
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...
102
                return $this->goHome();
103
            }
104
        }
105
106
        return $this->render('reg', [
107
            'model' => $model,
108
        ]);
109
    }
110
111
    /**
112
     * Logout action.
113
     *
114
     * @return Response
115
     */
116
    public function actionLogout()
117
    {
118
        Yii::$app->user->logout();
119
120
        return $this->goHome();
121
    }
122
}
123