Completed
Push — master ( 89bc16...4b9257 )
by Igor
02:18
created

UserModel::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 12
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yii2mod\user\models\BaseUserDetailsModel;
8
use yii2mod\user\models\BaseUserModel;
9
10
/**
11
 * Class UserModel
12
 * @package app\models
13
 */
14
class UserModel extends BaseUserModel
15
{
16
    /**
17
     * @var string newPassword - for creation user and changing password
18
     */
19
    public $newPassword;
20
21
    /**
22
     * Returns the validation rules for attributes.
23
     *
24
     * Validation rules are used by [[validate()]] to check if attribute values are valid.
25
     * Child classes may override this method to declare different validation rules.
26
     * @return array validation rules
27
     * @see scenarios()
28
     */
29
    public function rules()
30
    {
31
        return ArrayHelper::merge([
32
            [['username', 'email'], 'required'],
33
            ['email', 'unique', 'message' => 'This email address has already been taken.'],
34
            ['username', 'unique', 'message' => 'This username has already been taken.'],
35
            ['username', 'string', 'min' => 2, 'max' => 30],
36
            ['email', 'email'],
37
            ['newPassword', 'string', 'min' => 6, 'max' => 24],
38
            ['newPassword', 'required', 'on' => 'createUser'],
39
        ], parent::rules());
40
    }
41
42
    /**
43
     * Returns the attribute labels.
44
     *
45
     * Attribute labels are mainly used for display purpose. For example, given an attribute
46
     * `firstName`, we can declare a label `First Name` which is more user-friendly and can
47
     * be displayed to end users.
48
     *
49
     * @return array attribute labels (name => label)
50
     */
51
    public function attributeLabels()
52
    {
53
        return ArrayHelper::merge([
54
            'newPassword' => $this->isNewRecord ? Yii::t('app', 'Password') : Yii::t('app', 'New Password'),
55
        ], parent::attributeLabels());
56
    }
57
58
    /**
59
     * Returns a list of scenarios and the corresponding active attributes.
60
     * @return array
61
     */
62
    public function scenarios()
63
    {
64
        $scenarios = parent::scenarios();
65
        return $scenarios;
66
    }
67
68
    /**
69
     * Create user
70
     * @return UserModel|null the saved model or null if saving fails
71
     */
72
    public function createUser()
73
    {
74
        if ($this->validate()) {
75
            $this->setPassword($this->newPassword);
76
            $this->generateAuthKey();
77
            if ($this->save()) {
78
                $userDetailsModels = new BaseUserDetailsModel();
79
                $userDetailsModels->userId = $this->primaryKey;
80
                $userDetailsModels->save();
81
            }
82
            return $this;
83
        }
84
85
        return false;
86
    }
87
88
}