Completed
Push — master ( 342ffa...2ca792 )
by Andrii
05:25
created

ResetPasswordForm   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 6
lcom 1
cbo 3

3 Methods

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