Completed
Push — master ( c709fd...e60c36 )
by vistart
04:03
created

UsernameForm::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
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 Yii;
17
use yii\base\Model;
18
19
/**
20
 * Class UsernameForm
21
 * @package rhosocial\user\forms
22
 * @version 1.0
23
 * @author vistart <[email protected]>
24
 */
25
class UsernameForm extends Model
26
{
27
    /**
28
     * @var User
29
     */
30
    public $user;
31
    public $username;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function init()
37
    {
38
        if (!$this->user) {
39
            $this->user = Yii::$app->user->identity;
40
        }
41
        $username = $this->user->getUsername()->one();
42
        if (!$username) {
43
            $this->username = '';
44
        } else {
45
            $this->username = (string)$username;
46
        }
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    protected function getNoInitUsername()
53
    {
54
        $class = $this->user->usernameClass;
55
        return $class::buildNoInitModel();
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function rules()
62
    {
63
        return [
64
            ['username', 'required'],
65
            ['username', 'string', 'max' => 32, 'min' => '2'],
66
            ['username', 'match', 'not' => true, 'pattern' => '/^\d+$/', 'message' => Yii::t('user', 'The username can not be a pure number.')],
67
            ['username', 'match', 'pattern' => '/^\w{2,32}$/'],
68
            ['username', 'unique', 'targetClass' => $this->user->usernameClass, 'targetAttribute' => $this->getNoInitUsername()->contentAttribute, 'when' => function ($model, $attribute) {
69
                /* @var $model static */
70
                $username = $model->user->getUsername()->one();
0 ignored issues
show
Bug introduced by
The property user does not seem to exist in rhosocial\user\models\LoginMethod\Username.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
71
                if (!$username) {
72
                    return true;
73
                }
74
                return $model->$attribute != (string)$username;
75
            }],
76
        ];
77
    }
78
79
    /**
80
     * Change username.
81
     * @return bool
82
     */
83
    public function changeUsername()
84
    {
85
        if ($this->validate()) {
86
            $username = $this->user->createUsername($this->username);
87
            if (!$username) {
88
                return false;
89
            }
90
            return $username->save();
91
        }
92
        return false;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function attributeLabels()
99
    {
100
        return [
101
            'username' => Yii::t('user', 'Username'),
102
        ];
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function attributeHints()
109
    {
110
        return [
111
            'username' => Yii::t('user', 'Specify a username to provide convenience for login.'),
112
        ];
113
    }
114
}
115