RegisterForm   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 125
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 13 1
A attributeHints() 0 6 1
A getNoInitUser() 0 5 1
A getNoInitUsername() 0 5 1
A init() 0 13 3
A rules() 0 21 2
A register() 0 20 4
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\forms;
14
15
use rhosocial\user\User;
16
use rhosocial\user\Profile;
17
use Yii;
18
use yii\base\Model;
19
20
/**
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
class RegisterForm extends Model
25
{
26
    public $nickname;
27
    public $username = false;
28
    public $password;
29
    public $password_repeat;
30
    public $first_name;
31
    public $last_name;
32
    public $gender = 1;
33
    public $userClass;
34
    public $model = null;
35
    public $continue = 0;
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function attributeLabels()
41
    {
42
        return [
43
            'username' => Yii::t('user', 'Username'),
44
            'nickname' => Yii::t('user', 'Nickname'),
45
            'password' => Yii::t('user', 'Password'),
46
            'password_repeat' => Yii::t('user', 'Password Repeat'),
47
            'first_name' => Yii::t('user', 'First Name'),
48
            'last_name' => Yii::t('user', 'Last Name'),
49
            'gender' => Yii::t('user', 'Gender'),
50
            'continue' => Yii::t('user', 'Continue to register'),
51
        ];
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function attributeHints()
58
    {
59
        return [
60
            'continue' => Yii::t('user', 'If you want to register new users consecutively, check this option.'),
61
        ];
62
    }
63
64
    /**
65
     * @return User
66
     */
67
    protected function getNoInitUser()
68
    {
69
        $userClass = $this->userClass;
70
        return $userClass::buildNoInitModel();
71
    }
72
73
    /**
74
     * @return Username
75
     */
76
    protected function getNoInitUsername()
77
    {
78
        $usernameClass = $this->getNoInitUser()->usernameClass;
79
        return $usernameClass::buildNoInitModel();
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    public function init()
86
    {
87
        if (empty($this->userClass)) {
88
            $this->userClass = Yii::$app->user->identityClass;
89
        }
90
        $noInit = $this->getNoInitUser();
91
        /* @var $noInit User */
92
        if (class_exists($noInit->usernameClass)) {
93
            $this->username = '';
0 ignored issues
show
Documentation Bug introduced by
The property $username was declared of type boolean, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
94
        } else {
95
            $this->username = false;
96
        }
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function rules()
103
    {
104
        $rules = [
105
            [['nickname', 'password', 'password_repeat', 'first_name', 'last_name', 'gender'], 'required'],
106
            [['nickname'], 'string', 'max' => 32],
107
            [['password', 'password_repeat'], 'string', 'min' => 6, 'max' => 32],
108
            ['password', 'compare'],
109
            [['first_name', 'last_name'], 'string', 'max' => 255],
110
            ['gender', 'in', 'range' => array_keys(Profile::$genders)],
111
            ['continue', 'integer'],
112
        ];
113
        if (is_string($this->username)) {
114
            $rules = array_merge($rules, [
115
                ['username', 'required'],
116
                ['username', 'string', 'min'=>2, 'max' => 32],
117
                ['username', 'match', 'not' => true, 'pattern' => '/^\d+$/', 'message' => Yii::t('user', 'The username can not be a pure number.')],
118
                ['username', 'unique', 'targetClass' => $this->getNoInitUser()->usernameClass, 'targetAttribute' => $this->getNoInitUsername()->contentAttribute, 'message' => Yii::t('user', 'The username has been used.')]
119
            ]);
120
        }
121
        return $rules;
122
    }
123
124
    /**
125
     * Register user with current model.
126
     * @return bool
127
     */
128
    public function register()
129
    {
130
        if ($this->validate()) {
131
            $class = $this->userClass;
132
            $user = new $class(['password' => $this->password]);
133
            /* @var $user User */
134
            $profile = $user->createProfile(['nickname' => $this->nickname, 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'gender' => $this->gender]);
135
            $models[] = $profile;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$models was never initialized. Although not strictly required by PHP, it is generally a good practice to add $models = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
136
            if (is_string($this->username)) {
137
                $username = $user->createUsername($this->username);
138
                $models[] = $username;
139
            }
140
            $result = $user->register($models);
141
            if ($result == true) {
142
                $this->model = $user;
143
            }
144
            return $result;
145
        }
146
        return false;
147
    }
148
}
149