ResetPasswordForm::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 10
cc 4
nc 3
nop 2
crap 20
1
<?php
2
namespace App\Http\Form;
3
4
use App\Model\User;
5
use yii\base\InvalidArgumentException;
6
use yii\base\Model;
7
8
/**
9
 * Password reset form
10
 */
11
class ResetPasswordForm extends Model
12
{
13
    public $password;
14
15
    /**
16
     * @var User
17
     */
18
    private $_user;
19
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
     * @throws InvalidArgumentException if token is empty or not valid
27
     */
28
    public function __construct($token, $config = [])
29
    {
30
        if (empty($token) || !is_string($token)) {
31
            throw new InvalidArgumentException('Password reset token cannot be blank.');
32
        }
33
        $this->_user = User::findByPasswordResetToken($token);
34
        if (!$this->_user) {
35
            throw new InvalidArgumentException('Wrong password reset token.');
36
        }
37
        parent::__construct($config);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function rules()
44
    {
45
        return [
46
            ['password', 'required'],
47
            ['password', 'string', 'min' => 6],
48
        ];
49
    }
50
51
    /**
52
     * Resets password.
53
     *
54
     * @return bool if password was reset.
55
     */
56
    public function resetPassword()
57
    {
58
        $user = $this->_user;
59
        $user->setPassword($this->password);
60
        $user->removePasswordResetToken();
61
62
        return $user->save(false);
63
    }
64
}
65