ChangePasswordForm   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A changePassword() 0 4 1
A __construct() 0 3 1
A validatePassword() 0 5 3
A rules() 0 7 1
A attributeLabels() 0 4 1
1
<?php
2
namespace site\models;
3
4
use common\models\User;
5
use yii\base\Model;
6
use Yii;
7
8
/**
9
 * change password form
10
 */
11
class ChangePasswordForm extends Model
12
{
13
  public $old_password;
14
  public $new_password;
15
  private $user;
16
17
  /**
18
   * Creates a form model
19
   *
20
   * @param  object                          $user
21
   * @param  array                           $config name-value pairs that will be used to initialize the object properties
22
   */
23
  public function __construct(\common\interfaces\UserInterface $user, $config = []) {
24
    $this->user = $user;
25
    parent::__construct($config);
26
  }
27
28
  /**
29
   * @inheritdoc
30
   */
31
  public function rules()
32
  {
33
    return [
34
      ['old_password', 'string', 'min' => 6],
35
      ['new_password', 'string', 'min' => 8],
36
      [['old_password', 'new_password'], 'required'],
37
      ['old_password', 'validatePassword'],
38
    ];
39
  }
40
41
  public function attributeLabels() {
42
    return [
43
      'old_password' => 'Current Password',
44
      'new_password' => 'New Password',
45
      ];
46
  }
47
48
  /**
49
   * Validates the password.
50
   * This method serves as the inline validation for password.
51
   */
52
  public function validatePassword()
53
  {
54
    if (!$this->hasErrors()) {
55
      if (!$this->user->validatePassword($this->old_password)) {
0 ignored issues
show
Bug introduced by
The method validatePassword() does not exist on common\interfaces\UserInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\interfaces\UserInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
      if (!$this->user->/** @scrutinizer ignore-call */ validatePassword($this->old_password)) {
Loading history...
56
        $this->addError('old_password', 'Incorrect email or password.');
57
      }
58
    }
59
  }
60
61
  /**
62
   * changes user's password.
63
   *
64
   * @return Boolean whether or not the save was successful
65
   */
66
  public function changePassword()
67
  {
68
    $this->user->setPassword($this->new_password);
0 ignored issues
show
Bug introduced by
The method setPassword() does not exist on common\interfaces\UserInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\interfaces\UserInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
    $this->user->/** @scrutinizer ignore-call */ 
69
                 setPassword($this->new_password);
Loading history...
69
    return $this->user->save();
70
  }
71
}
72