Completed
Push — master ( 89be15...80fd60 )
by Igor
11:15
created

SocialController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 4
dl 0
loc 92
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 18 1
A actions() 0 10 1
A successCallback() 0 4 1
A redirect() 0 5 1
D actionSignup() 0 42 10
1
<?php
2
3
namespace app\modules\auth\controllers;
4
5
use Yii;
6
use app\modules\auth\services\SocialAuth;
7
use app\modules\auth\models\forms\SignupProviderForm;
8
9
class SocialController extends \yii\web\Controller
10
{
11
    /**
12
     * @inheritdoc
13
     */
14
    public function behaviors()
15
    {
16
        return [
17
            'access' => [
18
                'class' => 'yii\filters\AccessControl',
19
                'rules' => [
20
                    [
21
                        'actions' => [
22
                            'index',
23
                            'signup',
24
                        ],
25
                        'allow' => true,
26
                        'roles' => ['?'],
27
                    ],
28
                ],
29
            ],
30
        ];
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function actions()
37
    {
38
        return [
39
            'index' => [
40
                'class' => 'yii\authclient\AuthAction',
41
                'successCallback' => [$this, 'successCallback'],
42
                'successUrl' => '/auth/social/signup'
43
            ],
44
        ];
45
    }
46
47
    public function successCallback($client)
48
    {
49
        Yii::$app->session['authClient'] = $client;
50
    }
51
52
    public function redirect($url, $statusCode = 302)
53
    {
54
        Yii::$app->session['authClient'] = null;
55
        return parent::redirect($url, $statusCode);
56
    }
57
58
    public function actionSignup()
59
    {
60
        try {
61
            $socialAuth = \Yii::$container->get(SocialAuth::class, [Yii::$app->session['authClient']]);
62
        } catch (\Throwable $e) {
63
            Yii::error($e);
64
            return $this->redirect(['/']);
65
        }
66
67
        $user = $socialAuth->prepareUser();
68
        if ($user === null) {
69
            return $this->redirect(['/']);
70
        }
71
72
        $model = new SignupProviderForm($user);
73
74
        if (!$user->isNewRecord && $user->isActive() === false) {
75
            Yii::$app->session->setFlash('error', $user->getStatusDescription());
76
            return $this->redirect(['/']);
77
        }
78
79
        if (!$user->isNewRecord && $user->isActive()) {
80
            $model->login();
81
            return $this->redirect(['/']);
82
        }
83
84
        if ($model->validate() || ($model->load(Yii::$app->request->post()) && $model->validate())) {
85
            $model->signup();
86
            $model->sendEmail();
87
88
            Yii::$app->session->setFlash(
89
                'success',
90
                Yii::t('app.msg', 'Please activate your account') . '. ' .
91
                Yii::t('app.msg', 'A letter for activation was sent to {email}', ['email' => $model->email])
92
            );
93
            return $this->redirect(['/']);
94
        }
95
96
        return $this->render('signup', [
97
            'model' => $model
98
        ]);
99
    }
100
}
101