Completed
Push — master ( 53489f...89bc16 )
by Igor
02:21
created

ResetPasswordForm::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace app\models\forms;
4
5
use app\models\UserModel;
6
use Yii;
7
use yii\base\InvalidConfigException;
8
use yii\base\Model;
9
10
/**
11
 * Class ResetPasswordForm
12
 * @package app\models\forms
13
 */
14
class ResetPasswordForm extends Model
15
{
16
    /**
17
     * @var string Password
18
     */
19
    public $password;
20
21
    /**
22
     * @var string confirmPassword
23
     */
24
    public $confirmPassword;
25
26
    /**
27
     * @var UserModel
28
     */
29
    private $_user;
30
31
    /**
32
     * Construct
33
     *
34
     * @param UserModel $user
35
     * @param  array $config name-value pairs that will be used to initialize the object properties
36
     *
37
     * @throws InvalidConfigException
38
     */
39
    public function __construct($user, $config = [])
40
    {
41
        $this->_user = $user;
42
        if (!$this->_user) {
43
            throw new InvalidConfigException('UserModel must be set.');
44
        }
45
        parent::__construct($config);
46
    }
47
48
    /**
49
     * Returns the validation rules for attributes.
50
     *
51
     * Validation rules are used by [[validate()]] to check if attribute values are valid.
52
     * Child classes may override this method to declare different validation rules.
53
     * @return array validation rules
54
     * @see scenarios()
55
     */
56
    public function rules()
57
    {
58
        return [
59
            [['password', 'confirmPassword'], 'filter', 'filter' => 'trim'],
60
            ['password', 'required'],
61
            ['confirmPassword', 'required'],
62
            [['password', 'confirmPassword'], 'string', 'min' => '3'],
63
            [['password', 'confirmPassword'], 'match', 'pattern' => '/^[a-z0-9]+$/i'],
64
            ['confirmPassword', 'compare', 'compareAttribute' => 'password'],
65
        ];
66
    }
67
68
    /**
69
     * Returns the attribute labels.
70
     *
71
     * Attribute labels are mainly used for display purpose. For example, given an attribute
72
     * `firstName`, we can declare a label `First Name` which is more user-friendly and can
73
     * be displayed to end users.
74
     *
75
     * @return array attribute labels (name => label)
76
     */
77
    public function attributeLabels()
78
    {
79
        return [
80
            'password' => Yii::t('app', 'New Password'),
81
            'confirmPassword' => Yii::t('app', 'Confirm New Password'),
82
        ];
83
    }
84
85
    /**
86
     * Resets password.
87
     *
88
     * @return bool if password was reset.
89
     */
90
    public function resetPassword()
91
    {
92
        $user = $this->_user;
93
        if ($this->validate()) {
94
            $user->setPassword($this->password);
95
            return $user->save(true, ['passwordHash']);
96
        } else {
97
            return false;
98
        }
99
    }
100
}
101