Completed
Push — master ( d47dcb...df81e1 )
by Igor
03:40
created

IndexController   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 71.43%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 35
c 4
b 0
f 0
lcom 1
cbo 11
dl 0
loc 249
ccs 75
cts 105
cp 0.7143
rs 9

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B behaviors() 0 42 1
A actions() 0 13 1
A successCallback() 0 4 1
A goHome() 0 5 1
A actionIndex() 0 4 1
A actionLogin() 0 14 4
A actionSignup() 0 22 3
D actionSignupProvider() 0 44 10
A actionConfirmRequest() 0 16 2
A actionConfirmEmail() 0 7 1
A actionRequestPasswordReset() 0 19 3
A actionResetPassword() 0 13 3
A actionLogout() 0 5 1
A actionMaintenance() 0 9 2
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\filters\AccessControl;
8
use yii\web\ForbiddenHttpException;
9
use yii\web\NotFoundHttpException;
10
use app\services\SocialAuth;
11
use app\services\ConfirmEmail;
12
use app\models\forms\LoginForm;
13
use app\models\forms\SignupForm;
14
use app\models\forms\SignupProviderForm;
15
use app\models\forms\PasswordResetRequestForm;
16
use app\models\forms\ResetPasswordForm;
17
18
class IndexController extends \yii\web\Controller
19
{
20
    private $confirmEmail;
21
22 45
    public function __construct($id, $module, ConfirmEmail $confirmEmail, $config = [])
23
    {
24 45
        $this->confirmEmail = $confirmEmail;
25
26 45
        parent::__construct($id, $module, $config);
27 45
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 45
    public function behaviors()
33
    {
34
        return [
35 45
            'access' => [
36
                'class' => AccessControl::class,
37
                'only' => [
38
                    'auth-social',
39
                    'logout',
40
                    'signup',
41
                    'signup-provider',
42
                    'confirm-request',
43
                    'request-password-reset',
44
                ],
45
                'rules' => [
46
                    [
47
                        'actions' => [
48
                            'auth-social',
49
                            'signup',
50
                            'signup-provider',
51
                            'request-password-reset',
52
                        ],
53
                        'allow' => true,
54
                        'roles' => ['?'],
55
                    ],
56
                    [
57
                        'actions' => [
58
                            'logout',
59
                            'confirm-request'
60
                        ],
61
                        'allow' => true,
62
                        'roles' => ['@'],
63
                    ],
64
                ],
65
            ],
66
            'verbs' => [
67
                'class' => VerbFilter::class,
68
                'actions' => [
69
                    'logout' => ['post'],
70
                ],
71
            ],
72
        ];
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 45
    public function actions()
79
    {
80
        return [
81 45
            'error' => [
82
                'class' => 'yii\web\ErrorAction',
83
            ],
84
            'auth-social' => [
85 45
                'class' => 'yii\authclient\AuthAction',
86 45
                'successCallback' => [$this, 'successCallback'],
87 45
                'successUrl' => 'signup-provider'
88
            ],
89
        ];
90
    }
91
92
    public function successCallback($client)
93
    {
94
        Yii::$app->session['authClient'] = $client;
95
    }
96
97 6
    public function goHome()
98
    {
99 6
        Yii::$app->session['authClient'] = null;
100 6
        return Yii::$app->getResponse()->redirect(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...
101
    }
102
103 10
    public function actionIndex()
104
    {
105 10
        return $this->render('index');
106
    }
107
108 16
    public function actionLogin()
109
    {
110 16
        if (!Yii::$app->user->isGuest) {
111 1
            return $this->goHome();
112
        }
113
114 16
        $model = new LoginForm();
115 16
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
116 3
            return $this->goBack();
117
        }
118 16
        return $this->render('login', [
119 16
            'model' => $model,
120
        ]);
121
    }
122
123 13
    public function actionSignup()
124
    {
125 13
        $model = new SignupForm();
126 13
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
127 2
            $model->signup();
128
129 2
            Yii::$app->session->setFlash(
130 2
                'success',
131 2
                Yii::t(
132 2
                    'app.msg',
133 2
                    'Please activate your account. A letter for activation was sent to {email}',
134 2
                    ['email' => $model->email]
135
                )
136
            );
137
138 2
            return $this->goHome();
139
        }
140
141 13
        return $this->render('signup', [
142 13
            'model' => $model,
143
        ]);
144
    }
145
146
    public function actionSignupProvider()
147
    {
148
        try {
149
            $socialAuth = \Yii::$container->get(SocialAuth::class, [Yii::$app->session['authClient']]);
150
        } catch (\Throwable $e) {
151
            Yii::error($e);
152
            return $this->goHome();
153
        }
154
155
        $user = $socialAuth->prepareUser();
156
        if ($user === null) {
157
            return $this->goHome();
158
        }
159
160
        $model = new SignupProviderForm($user);
161
162
        if (!$user->isNewRecord && $user->isActive() === false) {
163
            $session->setFlash('error', $user->getStatusDescription());
0 ignored issues
show
Bug introduced by
The variable $session does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
164
            return $this->goHome();
165
        }
166
167
        if (!$user->isNewRecord && $user->isActive()) {
168
            $model->login();
169
            return $this->goHome();
170
        }
171
172
        if ($model->validate() || ($model->load(Yii::$app->request->post()) && $model->validate())) {
173
            $model->signup();
174
            $model->sendEmail();
175
            Yii::$app->session->setFlash(
176
                'success',
177
                Yii::t(
178
                    'app.msg',
179
                    'Please activate your account. A letter for activation was sent to {email}',
180
                    ['email' => $model->email]
181
                )
182
            );
183
            return $this->goHome();
184
        }
185
186
        return $this->render('signupProvider', [
187
            'model' => $model
188
        ]);
189
    }
190
191 2
    public function actionConfirmRequest()
192
    {
193 2
        $user = Yii::$app->user->identity;
194 2
        if ($user->isConfirmed()) {
195 1
            throw new ForbiddenHttpException(Yii::t('app.msg', 'Access Denied'));
196
        } // @codeCoverageIgnore
197
198 1
        $this->confirmEmail->sendEmail($user);
199
200 1
        Yii::$app->session->setFlash(
201 1
            'success',
202 1
            Yii::t('app.msg', 'A letter for activation was sent to {email}', ['email' => $user->email])
203
        );
204
205 1
        return $this->goHome();
206
    }
207
208 3
    public function actionConfirmEmail($token)
209
    {
210 3
        $this->confirmEmail->setConfirmed($token);
211
212 1
        Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Your account is successfully activated'));
213 1
        return $this->goHome();
214
    }
215
216 7
    public function actionRequestPasswordReset()
217
    {
218 7
        $model = new PasswordResetRequestForm();
219
220 7
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
221 1
            $model->sendEmail();
222
223 1
            Yii::$app->session->setFlash(
224 1
                'success',
225 1
                Yii::t('app.msg', 'We\'ve sent you an email with instructions to reset your password')
226
            );
227
228 1
            return $this->goHome();
229
        }
230
231 7
        return $this->render('requestPasswordResetToken', [
232 7
            'model' => $model,
233
        ]);
234
    }
235
236 6
    public function actionResetPassword($token)
237
    {
238 6
        $model = new ResetPasswordForm($token);
239
240 6
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
241 1
            $model->resetPassword();
242 1
            Yii::$app->session->setFlash('success', Yii::t('app.msg', 'New password was saved'));
243
        }
244
245 6
        return $this->render('resetPassword', [
246 6
            'model' => $model,
247
        ]);
248
    }
249
250 1
    public function actionLogout()
251
    {
252 1
        Yii::$app->user->logout();
253 1
        return $this->goHome();
254
    }
255
256
    /** @see commands/MaintenanceController **/
257 2
    public function actionMaintenance()
258
    {
259 2
        if (!Yii::$app->catchAll) {
260 1
            throw new NotFoundHttpException(Yii::t('app.msg', 'Page not found'));
261
        } // @codeCoverageIgnore
262
263 1
        $this->layout = 'maintenance';
264 1
        return $this->render('maintenance');
265
    }
266
}
267