ResetPasswordForm::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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