Completed
Push — master ( 01779b...d69358 )
by Dmitry
05:35
created

SiteController::actionSignup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 7
loc 7
ccs 0
cts 6
cp 0
crap 2
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\components\LanguageSwitcher;
15
use hipanel\models\ContactForm;
16
use hipanel\models\PasswordResetRequestForm;
17
use hipanel\models\ResetPasswordForm;
18
use hipanel\models\User;
19
use Yii;
20
use yii\base\InvalidParamException;
21
use yii\filters\AccessControl;
22
use yii\filters\VerbFilter;
23
use yii\web\BadRequestHttpException;
24
use yii\web\Controller;
25
26
/**
27
 * Site controller.
28
 */
29
class SiteController extends Controller
30
{
31
    //    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...
32
33
    public function behaviors()
34
    {
35
        return [
36
            'access' => [
37
                'class' => AccessControl::className(),
38
                'only' => ['logout', 'signup', 'lockscreen'],
39
                'rules' => [
40
                    [
41
                        'actions' => ['signup'],
42
                        'allow' => true,
43
                        'roles' => ['?'],
44
                    ],
45
                    [
46
                        'actions' => ['logout', 'lockscreen'],
47
                        'roles' => ['@'],
48
                        'allow' => true,
49
                    ],
50
                ],
51
            ],
52
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
53
            'verbs' => [
54
                'class' => VerbFilter::className(),
55
                'actions' => [
56
                    'logout' => ['post'],
57
                ],
58
            ],
59
*/
60
        ];
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function actions()
67
    {
68
        return [
69
            'auth' => [
70
                'class' => 'yii\authclient\AuthAction',
71
                'successCallback' => [$this, 'successCallback'],
72
            ],
73
            'error' => [
74
                'class' => 'yii\web\ErrorAction',
75
            ],
76
            'captcha' => [
77
                'class' => 'yii\captcha\CaptchaAction',
78
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
79
            ],
80
        ];
81
    }
82
83
    public function successCallback($client)
84
    {
85
        $attributes = $client->getUserAttributes();
86
        $user = new User();
87
        foreach ($user->attributes() as $k) {
88
            $user->{$k} = $attributes[$k];
89
        }
90
        $user->save();
91
        Yii::$app->user->login($user, 3600 * 24 * 30);
92
    }
93
94
    public function actionIndex()
95
    {
96
        return $this->redirect(['/hipanel/index']);
97
        // 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...
98
    }
99
100
    public function actionLockscreen()
101
    {
102
        return $this->render('lockscreen');
103
    }
104
105
    public function actionLogin()
106
    {
107
        if (!Yii::$app->user->isGuest) {
108
            return $this->redirect(['/hipanel/index']);
109
        }
110
111
        return $this->redirect(['/site/auth', 'authclient' => 'hiam']);
112
    }
113
114
    public function actionProfile()
115
    {
116
        return $this->redirect(['@client/view', 'id' => Yii::$app->user->identity->id]);
117
    }
118
119 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...
120
    {
121
        $back = Yii::$app->request->getHostInfo();
122
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/logout', compact('back'));
123
        Yii::$app->user->logout();
124
125
        return Yii::$app->response->redirect($url);
126
    }
127
128 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...
129
    {
130
        $back = Yii::$app->request->getHostInfo();
131
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/signup', compact('back'));
132
133
        return Yii::$app->response->redirect($url);
134
    }
135
136
    public function actionContact()
137
    {
138
        $model = new ContactForm();
139
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
140
            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
141
                Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
142
            } else {
143
                Yii::$app->session->setFlash('error', 'There was an error sending email.');
144
            }
145
146
            return $this->refresh();
147
        } else {
148
            return $this->render('contact', [
149
                'model' => $model,
150
            ]);
151
        }
152
    }
153
154
    public function actionAbout()
155
    {
156
        return $this->render('about');
157
    }
158
159
    public function actionRequestPasswordReset()
160
    {
161
        $model = new PasswordResetRequestForm();
162
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
163
            if ($model->sendEmail()) {
164
                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...
165
166
                return $this->goHome();
167
            } else {
168
                Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
169
            }
170
        }
171
172
        return $this->render('requestPasswordResetToken', [
173
            'model' => $model,
174
        ]);
175
    }
176
177
    public function actionResetPassword($token)
178
    {
179
        try {
180
            $model = new ResetPasswordForm($token);
181
        } catch (InvalidParamException $e) {
182
            throw new BadRequestHttpException($e->getMessage());
183
        }
184
185
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
186
            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...
187
188
            return $this->goHome();
189
        }
190
191
        return $this->render('resetPassword', [
192
            'model' => $model,
193
        ]);
194
    }
195
196
    public function actionLanguage($language)
197
    {
198
        /** @var LanguageSwitcher $languageSwitcher */
199
        $languageSwitcher = Yii::$app->get('languageSwitcher');
200
        $languageSwitcher->setLanguage($language);
201
202
        $url = Yii::$app->request->referrer;
203
        if ($url === null) {
204
            $url = Yii::$app->getHomeUrl();
0 ignored issues
show
Bug introduced by
The method getHomeUrl 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...
205
        }
206
        return Yii::$app->response->redirect($url);
207
    }
208
}
209