RegisterController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 104
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 24 1
A actions() 0 12 2
B actionIndex() 0 31 8
A actionSuccess() 0 8 2
A actionFailed() 0 8 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\web\user\controllers;
14
15
use rhosocial\user\forms\RegisterForm;
16
use rhosocial\user\web\user\Module;
17
use Yii;
18
use yii\bootstrap\ActiveForm;
19
use yii\filters\AccessControl;
20
use yii\web\Controller;
21
use yii\web\ForbiddenHttpException;
22
use yii\web\Response;
23
24
/**
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
class RegisterController extends Controller
29
{
30
    public $layout = 'main';
31
32
    const SESSION_KEY_REGISTER_USER_ID = 'session_key_register_user_id';
33
    const SESSION_KEY_REGISTER_FAILED_MESSAGE = 'session_key_register_failed_message';
34
35
    public function behaviors()
36
    {
37
        return [
38
            'access' => [
39
                'class' => AccessControl::class,
40
                'only' => ['index'],
41
                'rules' => [
42
                    [
43
                        'actions' => ['index'],
44
                        'allow' => true,
45
                        'roles' => ['?'],
46
                    ],
47
                    [
48
                        'actions' => ['index'],
49
                        'allow' => false,
50
                        'roles' => ['@'],
51
                        'denyCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
                            throw new ForbiddenHttpException(Yii::t('user', 'You must log out current user before registering new one.'));
53
                        },
54
                    ],
55
                ],
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function actions()
64
    {
65
        return [
66
            'error' => [
67
                'class' => 'yii\web\ErrorAction',
68
            ],
69
            'captcha' => [
70
                'class' => 'yii\captcha\CaptchaAction',
71
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
72
            ],
73
        ];
74
    }
75
76
    /**
77
     * @return string|\yii\web\Response
78
     */
79
    public function actionIndex()
80
    {        
81
        $model = new RegisterForm();
82
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
83
            Yii::$app->response->format = Response::FORMAT_JSON;
84
            return ActiveForm::validate($model);
85
        }
86
        if ($model->load(Yii::$app->request->post())) {
87
            try {
88
                if (($result = $model->register()) === true) {
89
                    if ($model->continue) {
90
                        Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
91
                        Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $model->model->getID() . ') ' . Yii::t('user', 'User Registered.'));
92
                        return $this->redirect(['index']);
93
                    }
94
                    Yii::$app->session->setFlash(self::SESSION_KEY_REGISTER_USER_ID, $model->model->getID());
95
                    return $this->redirect(['success']);
96
                }
97
                if ($result instanceof \Exception) {
98
                    throw $result;
99
                }
100
            } catch (\Exception $ex) {
101
                Yii::error($ex->getMessage(), __METHOD__);
102
                Yii::$app->session->setFlash(self::SESSION_KEY_REGISTER_FAILED_MESSAGE, $ex->getMessage());
103
            }
104
            return $this->redirect(['failed']);
105
        }
106
        return $this->render('index', [
107
            'model' => $model,
108
        ]);
109
    }
110
111
    /**
112
     * @return string|\yii\web\Response
113
     */
114
    public function actionSuccess()
115
    {
116
        $id = Yii::$app->session->getFlash(self::SESSION_KEY_REGISTER_USER_ID);
117
        if ($id === null) {
118
            return $this->redirect(['index']);
119
        }
120
        return $this->render('success', ['id' => $id]);
121
    }
122
123
    public function actionFailed()
124
    {
125
        $message = Yii::$app->session->getFlash(self::SESSION_KEY_REGISTER_FAILED_MESSAGE);
126
        if ($message === null) {
127
            return $this->redirect(['index']);
128
        }
129
        return $this->render('failed', ['message' => $message]);
130
    }
131
}
132