ResetPasswordForm::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace yiicod\auth\models;
4
5
use Yii;
6
use yii\base\InvalidParamException;
7
use yii\base\Model;
8
9
/**
10
 * Password reset form
11
 */
12
class ResetPasswordForm extends Model
13
{
14
    public $password;
15
16
    /**
17
     * @var User2
18
     */
19
    private $_user;
20
21
    /**
22
     * Creates a form model given a token.
23
     *
24
     * @param  string                          $token
25
     * @param  array                           $config name-value pairs that will be used to initialize the object properties
26
     *
27
     * @throws InvalidParamException if token is empty or not valid
28
     */
29
    public function __construct($token, $config = [])
30
    {
31
        if (empty($token) || !is_string($token)) {
32
            throw new InvalidParamException('Password reset token cannot be blank.');
33
        }
34
        $userClass = Yii::$app->get('auth')->modelMap['user']['class'];
35
        $this->_user = $userClass::findByPasswordResetToken($token);
36
        if (!$this->_user) {
37
            throw new InvalidParamException('Wrong password reset token.');
38
        }
39
        parent::__construct($config);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function rules()
46
    {
47
        return [
48
            ['password', 'required'],
49
            ['password', 'string', 'min' => 6],
50
        ];
51
    }
52
53
    /**
54
     * Resets password.
55
     *
56
     * @return bool if password was reset
57
     */
58
    public function resetPassword()
59
    {
60
        $user = $this->_user;
61
        $user->generatePassword($this->password);
62
        $user->removePasswordResetToken();
63
64
        return $user->save();
65
    }
66
}
67