Completed
Push — master ( 85769a...b67e6b )
by Igor
07:14
created

IndexController::actionSignup()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.2
cc 3
eloc 13
nc 2
nop 0
crap 3
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 $socialAuth;
21
    private $confirmEmail;
22
23 45
    public function __construct($id, $module, SocialAuth $socialAuth, ConfirmEmail $confirmEmail, $config = [])
24
    {
25 45
        $this->socialAuth = $socialAuth;
26 45
        $this->confirmEmail = $confirmEmail;
27 45
        parent::__construct($id, $module, $config);
28 45
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33 45
    public function behaviors()
34
    {
35
        return [
36 45
            'access' => [
37
                'class' => AccessControl::class,
38
                'only' => [
39
                    'auth',
40
                    'logout',
41
                    'signup',
42
                    'signup-provider',
43
                    'confirm-request',
44
                    'request-password-reset',
45
                ],
46
                'rules' => [
47
                    [
48
                        'actions' => [
49
                            'auth',
50
                            'signup',
51
                            'signup-provider',
52
                            'request-password-reset',
53
                        ],
54
                        'allow' => true,
55
                        'roles' => ['?'],
56
                    ],
57
                    [
58
                        'actions' => [
59
                            'logout',
60
                            'confirm-request'
61
                        ],
62
                        'allow' => true,
63
                        'roles' => ['@'],
64
                    ],
65
                ],
66
            ],
67
            'verbs' => [
68
                'class' => VerbFilter::class,
69
                'actions' => [
70
                    'logout' => ['post'],
71
                ],
72
            ],
73
        ];
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 45
    public function actions()
80
    {
81
        return [
82 45
            'error' => [
83
                'class' => 'yii\web\ErrorAction',
84
            ],
85
            'auth' => [
86 45
                'class' => 'yii\authclient\AuthAction',
87 45
                'successCallback' => [$this, 'successCallback'],
88 45
                'successUrl' => 'signup-provider'
89
            ],
90
        ];
91
    }
92
93
    public function successCallback($client)
94
    {
95
        Yii::$app->session['authClient'] = $client;
96
    }
97
98 6
    public function goHome()
99
    {
100 6
        Yii::$app->session['authClient'] = null;
101 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...
102
    }
103
104 10
    public function actionIndex()
105
    {
106 10
        return $this->render('index');
107
    }
108
109 16
    public function actionLogin()
110
    {
111 16
        if (!Yii::$app->user->isGuest) {
112 1
            return $this->goHome();
113
        }
114
115 16
        $model = new LoginForm();
116 16
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
117 3
            return $this->goBack();
118
        }
119 16
        return $this->render('login', [
120 16
            'model' => $model,
121
        ]);
122
    }
123
124 13
    public function actionSignup()
125
    {
126 13
        $model = new SignupForm();
127 13
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
128 2
            $model->signup();
129
130 2
            Yii::$app->session->setFlash(
131 2
                'success',
132 2
                Yii::t(
133 2
                    'app.msg',
134 2
                    'Please activate your account. A letter for activation was sent to {email}',
135 2
                    ['email' => $model->email]
136
                )
137
            );
138
139 2
            return $this->goHome();
140
        }
141
142 13
        return $this->render('signup', [
143 13
            'model' => $model,
144
        ]);
145
    }
146
147
    public function actionSignupProvider()
148
    {
149
        $session = Yii::$app->session;
150
        if ($session['authClient'] === null) {
151
            return $this->goHome();
152
        }
153
154
        $this->socialAuth->execute($session['authClient']);
155
        $user = $this->socialAuth->user();
156
157
        if ($user === null) {
158
            return $this->goHome();
159
        }
160
161
        $model = new SignupProviderForm($user, $this->socialAuth->email());
162
163
        if ($this->socialAuth->isExist() && $user->isActive() === false) {
164
            $session->setFlash('error', $user->getStatusDescription());
165
            return $this->goHome();
166
        }
167
168
        if ($this->socialAuth->isExist() && $user->isActive()) {
169
            $model->login();
170
            return $this->goHome();
171
        }
172
173
        if ($this->socialAuth->isVerified()) {
174
            $model->signup();
175
            return $this->goHome();
176
        }
177
178
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
179
            $model->signup();
180
            $model->sendEmail();
181
            $session->setFlash(
182
                'success',
183
                Yii::t(
184
                    'app.msg',
185
                    'Please activate your account. A letter for activation was sent to {email}',
186
                    ['email' => $model->email]
187
                )
188
            );
189
            return $this->goHome();
190
        }
191
192
        return $this->render('signupProvider', [
193
            'model' => $model
194
        ]);
195
    }
196
197 2
    public function actionConfirmRequest()
198
    {
199 2
        $user = Yii::$app->user->identity;
200 2
        if ($user->isConfirmed()) {
201 1
            throw new ForbiddenHttpException(Yii::t('app.msg', 'Access Denied'));
202
        } // @codeCoverageIgnore
203
204 1
        $this->confirmEmail->sendEmail($user);
205
206 1
        Yii::$app->session->setFlash(
207 1
            'success',
208 1
            Yii::t('app.msg', 'A letter for activation was sent to {email}', ['email' => $user->email])
209
        );
210
211 1
        return $this->goHome();
212
    }
213
214 3
    public function actionConfirmEmail($token)
215
    {
216 3
        $this->confirmEmail->setConfirmed($token);
217
218 1
        Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Your account is successfully activated'));
219 1
        return $this->goHome();
220
    }
221
222 7
    public function actionRequestPasswordReset()
223
    {
224 7
        $model = new PasswordResetRequestForm();
225
226 7
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
227 1
            $model->sendEmail();
228
229 1
            Yii::$app->session->setFlash(
230 1
                'success',
231 1
                Yii::t('app.msg', 'We\'ve sent you an email with instructions to reset your password')
232
            );
233
234 1
            return $this->goHome();
235
        }
236
237 7
        return $this->render('requestPasswordResetToken', [
238 7
            'model' => $model,
239
        ]);
240
    }
241
242 6
    public function actionResetPassword($token)
243
    {
244 6
        $model = new ResetPasswordForm($token);
245
246 6
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
247 1
            $model->resetPassword();
248 1
            Yii::$app->session->setFlash('success', Yii::t('app.msg', 'New password was saved'));
249
        }
250
251 6
        return $this->render('resetPassword', [
252 6
            'model' => $model,
253
        ]);
254
    }
255
256 1
    public function actionLogout()
257
    {
258 1
        Yii::$app->user->logout();
259 1
        return $this->goHome();
260
    }
261
262
    /** @see commands/MaintenanceController **/
263 2
    public function actionMaintenance()
264
    {
265 2
        if (!Yii::$app->catchAll) {
266 1
            throw new NotFoundHttpException(Yii::t('app.msg', 'Page not found'));
267
        } // @codeCoverageIgnore
268
269 1
        $this->layout = 'maintenance';
270 1
        return $this->render('maintenance');
271
    }
272
}
273