Completed
Push — master ( 187a92...992701 )
by Andrii
14:09
created

SiteController::actionLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 6
rs 9.4285
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\controllers;
13
14
use hipanel\models\ContactForm;
15
use hipanel\models\PasswordResetRequestForm;
16
use hipanel\models\ResetPasswordForm;
17
use hipanel\models\User;
18
use Yii;
19
use yii\base\InvalidParamException;
20
use yii\filters\AccessControl;
21
use yii\filters\VerbFilter;
22
use yii\web\BadRequestHttpException;
23
use yii\web\Controller;
24
25
/**
26
 * Site controller.
27
 */
28
class SiteController extends Controller
29
{
30
    //    public $layout = 'site';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
32
    public function behaviors()
33
    {
34
        return [
35
            'access' => [
36
                'class' => AccessControl::class,
37
                'only' => ['logout', 'signup', 'lockscreen'],
38
                'rules' => [
39
                    [
40
                        'actions' => ['signup'],
41
                        'allow' => true,
42
                        'roles' => ['?'],
43
                    ],
44
                    [
45
                        'actions' => ['logout', 'lockscreen'],
46
                        'roles' => ['@'],
47
                        'allow' => true,
48
                    ],
49
                ],
50
            ],
51
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
            'verbs' => [
53
                'class' => VerbFilter::class,
54
                'actions' => [
55
                    'logout' => ['post'],
56
                ],
57
            ],
58
*/
59
        ];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function actions()
66
    {
67
        return [
68
            'auth' => [
69
                'class' => 'yii\authclient\AuthAction',
70
                'successCallback' => [$this, 'successCallback'],
71
            ],
72
            'error' => [
73
                'class' => 'yii\web\ErrorAction',
74
            ],
75
            'captcha' => [
76
                'class' => 'yii\captcha\CaptchaAction',
77
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
78
            ],
79
        ];
80
    }
81
82
    public function successCallback($client)
83
    {
84
        $attributes = $client->getUserAttributes();
85
        $user = new User();
86
        foreach ($user->attributes() as $k) {
87
            $user->{$k} = $attributes[$k];
88
        }
89
        $user->save();
90
        Yii::$app->user->login($user, 3600 * 24 * 30);
91
    }
92
93
    public function actionIndex()
94
    {
95
        return $this->redirect(['/hipanel/index']);
96
        // return $this->render('index');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
    }
98
99
    public function actionLockscreen()
100
    {
101
        return $this->render('lockscreen');
102
    }
103
104
    public function actionLogin()
105
    {
106
        if (!Yii::$app->user->isGuest) {
107
            return $this->redirect(['/hipanel/index']);
108
        }
109
110
        return $this->redirect(['/site/auth', 'authclient' => 'hiam']);
111
    }
112
113
    public function actionProfile()
114
    {
115
        return $this->redirect(['@client/view', 'id' => Yii::$app->user->identity->id]);
116
    }
117
118 View Code Duplication
    public function actionLogout()
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...
119
    {
120
        $back = Yii::$app->request->getHostInfo();
121
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/logout', compact('back'));
122
        Yii::$app->user->logout();
123
124
        return Yii::$app->response->redirect($url);
125
    }
126
127 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...
128
    {
129
        $back = Yii::$app->request->getHostInfo();
130
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/signup', compact('back'));
131
132
        return Yii::$app->response->redirect($url);
133
    }
134
135
    public function actionContact()
136
    {
137
        $model = new ContactForm();
138
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
139
            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
140
                Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
141
            } else {
142
                Yii::$app->session->setFlash('error', 'There was an error sending email.');
143
            }
144
145
            return $this->refresh();
146
        } else {
147
            return $this->render('contact', [
148
                'model' => $model,
149
            ]);
150
        }
151
    }
152
153
    public function actionAbout()
154
    {
155
        return $this->render('about');
156
    }
157
158
    public function actionRequestPasswordReset()
159
    {
160
        $model = new PasswordResetRequestForm();
161
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
162
            if ($model->sendEmail()) {
163
                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...
164
165
                return $this->goHome();
166
            } else {
167
                Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
168
            }
169
        }
170
171
        return $this->render('requestPasswordResetToken', [
172
            'model' => $model,
173
        ]);
174
    }
175
176
    public function actionResetPassword($token)
177
    {
178
        try {
179
            $model = new ResetPasswordForm($token);
180
        } catch (InvalidParamException $e) {
181
            throw new BadRequestHttpException($e->getMessage());
182
        }
183
184
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
185
            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...
186
187
            return $this->goHome();
188
        }
189
190
        return $this->render('resetPassword', [
191
            'model' => $model,
192
        ]);
193
    }
194
195
}
196