ChangePassword   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 19
dl 0
loc 50
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A change() 0 13 3
A validatePassword() 0 6 3
A rules() 0 7 1
1
<?php
2
3
namespace toir427\admin\models\form;
4
5
use Yii;
6
use toir427\admin\models\User;
7
use yii\base\Model;
8
9
/**
10
 * Description of ChangePassword
11
 *
12
 * @author Misbahul D Munir <[email protected]>
13
 * @since 1.0
14
 */
15
class ChangePassword extends Model
16
{
17
    public $oldPassword;
18
    public $newPassword;
19
    public $retypePassword;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function rules()
25
    {
26
        return [
27
            [['oldPassword', 'newPassword', 'retypePassword'], 'required'],
28
            [['oldPassword'], 'validatePassword'],
29
            [['newPassword'], 'string', 'min' => 6],
30
            [['retypePassword'], 'compare', 'compareAttribute' => 'newPassword'],
31
        ];
32
    }
33
34
    /**
35
     * Validates the password.
36
     * This method serves as the inline validation for password.
37
     */
38
    public function validatePassword()
39
    {
40
        /* @var $user User */
41
        $user = Yii::$app->user->identity;
42
        if (!$user || !$user->validatePassword($this->oldPassword)) {
0 ignored issues
show
introduced by
$user is of type toir427\admin\models\User, thus it always evaluated to true.
Loading history...
43
            $this->addError('oldPassword', 'Incorrect old password.');
44
        }
45
    }
46
47
    /**
48
     * Change password.
49
     *
50
     * @return User|null the saved model or null if saving fails
51
     */
52
    public function change()
53
    {
54
        if ($this->validate()) {
55
            /* @var $user User */
56
            $user = Yii::$app->user->identity;
57
            $user->setPassword($this->newPassword);
58
            $user->generateAuthKey();
59
            if ($user->save()) {
60
                return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type null|toir427\admin\models\User.
Loading history...
61
            }
62
        }
63
64
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type null|toir427\admin\models\User.
Loading history...
65
    }
66
}
67