UserForm   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 61.67%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 197
ccs 37
cts 60
cp 0.6167
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 28 2
A attributeLabels() 0 16 1
A attributeHints() 0 6 1
A setModel() 0 13 1
A model() 0 8 2
A save() 0 28 3
A statusesList() 0 4 1
A rolesList() 0 5 1
A assignUserToRole() 0 12 3
1
<?php
2
3
namespace app\modules\admin\models\forms;
4
5
use Yii;
6
use yii\base\Exception;
7
use yii\helpers\ArrayHelper;
8
use app\models\entity\{AuthItem, User};
9
10
class UserForm extends \yii\base\Model
11
{
12
    /**
13
     * @var int
14
     */
15
    public $id;
16
    /**
17
     * @var string
18
     */
19
    public $email;
20
    /**
21
     * @var string
22
     */
23
    public $date_create;
24
    /**
25
     * @var string
26
     */
27
    public $date_update;
28
    /**
29
     * @var string
30
     */
31
    public $date_login;
32
    /**
33
     * @var int
34
     */
35
    public $ip;
36
    /**
37
     * @var string
38
     */
39
    public $role_name;
40
    /**
41
     * @var int
42
     */
43
    public $status;
44
    /**
45
     * @var string
46
     */
47
    public $passwordNew;
48
    /**
49
     * @var \app\models\entity\User
50
     */
51
    private $model;
52
53
   /**
54
    * @return array The validation rules
55
    */
56
    public function rules()
57
    {
58
        return [
59
            ['email', 'email'],
60 2
            ['email', 'required'],
61
            ['email', 'string', 'max' => 255],
62
            ['email', 'unique', 'targetClass' => User::class, 'filter' => function ($query) {
63 2
                if (!$this->model()->isNewRecord) {
64
                    $query->andWhere(['not', ['id' => $this->model()->id]]);
65 2
                }
66
            }],
67
68
            ['role_name', 'string'],
69 2
            [
70
                'role_name',
71
                'exist',
72
                'targetClass' => AuthItem::class,
73
                'targetAttribute' => ['role_name' => 'name'],
74
                'filter' => ['type' => \yii\rbac\Item::TYPE_ROLE]
75 2
            ],
76
77
            ['status', 'integer'],
78
            ['status', 'default', 'value' => User::STATUS_ACTIVE],
79 2
            ['status', 'in', 'range' => array_keys(User::getStatuses())],
80
81
            ['passwordNew', 'string', 'min' => 6],
82
        ];
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function attributeLabels()
89
    {
90
        return [
91
            'id' => Yii::t('app', 'ID'),
92
            'email' => Yii::t('app', 'Email'),
93 2
            'password' => Yii::t('app', 'Password'),
94
            'date_create' => Yii::t('app', 'Date create'),
95
            'date_update' => Yii::t('app', 'Date update'),
96
            'date_login' => Yii::t('app', 'Last login'),
97
            'ip' => Yii::t('app', 'IP'),
98
            'role_name' => Yii::t('app', 'Role'),
99
            'status' => Yii::t('app', 'Status'),
100
101
            'passwordNew' => Yii::t('app', 'New password'),
102 2
        ];
103
    }
104
105 2
    /**
106 2
     * @inheritdoc
107 2
     */
108 2
    public function attributeHints()
109 2
    {
110 2
        return [
111 2
            'passwordNew' => Yii::t('app', 'Set a new password')
112 2
        ];
113 2
    }
114 2
115
    /**
116 2
     * Set model
117
     *
118
     * @param User $model
119
     */
120
    public function setModel(User $model): void
121
    {
122
        $this->model = $model;
123
124
        $this->id = $model->id;
125
        $this->email = $model->email;
126
        $this->date_create = $model->date_create;
127
        $this->date_update = $model->date_update;
128
        $this->date_login = $model->date_login;
129
        $this->ip = $model->id;
130
        $this->role_name = $model->role_name;
131
        $this->status = $model->status;
132
    }
133
134
    /**
135
     * Get model
136
     *
137 1
     * @return User
138
     */
139 1
    public function model(): User
140
    {
141 1
        if ($this->model === null) {
142 1
            $this->model = new User();
143 1
        }
144 1
145 1
        return $this->model;
146 1
    }
147 1
148 1
    /**
149 1
     * Save user
150 1
     *
151
     * @throws Exception
152
     * @return User
153
     */
154
    public function save(): User
155
    {
156
        $model = $this->model();
157 2
158
        $model->id = $this->id;
159 2
        $model->email = $this->email;
160 1
        $model->date_create = $this->date_create;
161
        $model->date_update = $this->date_update;
162
        $model->date_login = $this->date_login;
163 2
        $model->ip = $this->id;
164
        $model->role_name = $this->role_name;
165
        $model->status = $this->status;
166
        $model->passwordNew = $this->passwordNew;
167
168
        if ($model->isNewRecord) {
169
            $model->setConfirmed();
170
        }
171
172
        if (!$model->save()) {
173
            throw new Exception(Yii::t('app', 'An error occurred while saving user'));
174
        }
175
176
        $this->id = $model->id;
177
178
        $this->assignUserToRole($model->id, $model->role_name);
179
180
        return $model;
181
    }
182
183
    public function statusesList(): array
184
    {
185
        return $this->model()->getStatuses();
186
    }
187
188
    public function rolesList(): array
189
    {
190
        $list = Yii::$app->authManager->getRoles();
191
        return ArrayHelper::map($list, 'name', 'description');
192
    }
193
194
    private function assignUserToRole(int $userId, string $roleName = ''): void
195
    {
196
        $auth = Yii::$app->authManager;
197
        $auth->revokeAll($userId);
198
199
        if (!empty($roleName)) {
200
            $role = $auth->getRole($roleName);
201
            if ($role) {
202 2
                $auth->assign($role, $userId);
203
            }
204 2
        }
205
    }
206
}
207