Completed
Push — master ( ccfeeb...7d9725 )
by Alexey
02:50 queued 46s
created

ResetPasswordForm   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 4 1
A resetPassword() 0 6 1
A rules() 0 5 1
A __construct() 0 10 4
1
<?php
2
3
namespace modules\users\models;
4
5
use yii\base\Model;
6
use yii\base\InvalidArgumentException;
7
use modules\users\Module;
8
9
/**
10
 * Class ResetPasswordForm
11
 * @package modules\users\models
12
 *
13
 * @property string $password Password
14
 */
15
class ResetPasswordForm extends Model
16
{
17
    public $password;
18
19
    /**
20
     * @var \modules\users\models\User
21
     */
22
    private $_user;
23
24
    /**
25
     * Creates a form model given a token.
26
     *
27
     * @param mixed $token
28
     * @param array $config name-value pairs that will be used to initialize the object properties
29
     * @throws \yii\base\InvalidArgumentException if token is empty or not valid
30
     */
31
    public function __construct($token = '', $config = [])
32
    {
33
        if (empty($token) || !is_string($token)) {
34
            throw new InvalidArgumentException(Module::t('module', 'Password reset token cannot be blank.'));
35
        }
36
        $this->_user = User::findByPasswordResetToken($token);
37
        if (!$this->_user) {
38
            throw new InvalidArgumentException(Module::t('module', 'Wrong password reset token.'));
39
        }
40
        parent::__construct($config);
41
    }
42
43
    /**
44
     * @inheritdoc
45
     * @return array
46
     */
47
    public function rules()
48
    {
49
        return [
50
            ['password', 'required'],
51
            ['password', 'string', 'min' => User::LENGTH_STRING_PASSWORD_MIN, 'max' => User::LENGTH_STRING_PASSWORD_MAX],
52
        ];
53
    }
54
55
    /**
56
     * @inheritdoc
57
     * @return array
58
     */
59
    public function attributeLabels()
60
    {
61
        return [
62
            'password' => Module::t('module', 'New Password'),
63
        ];
64
    }
65
66
    /**
67
     * Resets password.
68
     *
69
     * @return bool if password was reset.
70
     * @throws \yii\base\Exception
71
     */
72
    public function resetPassword()
73
    {
74
        $user = $this->_user;
75
        $user->setPassword($this->password);
76
        $user->removePasswordResetToken();
77
        return $user->save(false);
78
    }
79
}
80