ResetPasswordForm   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 23
c 1
b 0
f 1
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 4 1
A resetPassword() 0 6 1
A rules() 0 9 1
A __construct() 0 13 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 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 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(
35
                'module',
36
                'Password reset token cannot be blank.'
37
            ));
38
        }
39
        $this->user = User::findByPasswordResetToken($token);
40
        if (!$this->user) {
41
            throw new InvalidArgumentException(Module::t('module', 'Wrong password reset token.'));
42
        }
43
        parent::__construct($config);
44
    }
45
46
    /**
47
     * @inheritdoc
48
     * @return array
49
     */
50
    public function rules()
51
    {
52
        return [
53
            ['password', 'required'],
54
            [
55
                'password',
56
                'string',
57
                'min' => User::LENGTH_STRING_PASSWORD_MIN,
58
                'max' => User::LENGTH_STRING_PASSWORD_MAX
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * @inheritdoc
65
     * @return array
66
     */
67
    public function attributeLabels()
68
    {
69
        return [
70
            'password' => Module::t('module', 'New Password'),
71
        ];
72
    }
73
74
    /**
75
     * Resets password.
76
     *
77
     * @return bool if password was reset.
78
     * @throws \yii\base\Exception
79
     */
80
    public function resetPassword()
81
    {
82
        $user = $this->user;
83
        $user->setPassword($this->password);
84
        $user->removePasswordResetToken();
85
        return $user->save(false);
86
    }
87
}
88