Test Failed
Push — master ( 44c0f1...2dfe85 )
by vistart
03:48
created

ChangePasswordForm::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\forms;
14
15
use rhosocial\user\User;
16
use Yii;
17
use yii\base\Model;
18
19
/**
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
class ChangePasswordForm extends Model
24
{
25
    public $password;
26
    public $new_password;
27
    public $new_password_repeat;
28
    private $_user = false;
29
30
    public function attributeLabels()
31
    {
32
        return [
33
            'password' => Yii::t('user', 'Password'),
34
            'new_password' => Yii::t('user', 'New Password'),
35
            'new_password_repeat' => Yii::t('user', 'New Password Repeat'),
36
        ];
37
    }
38
39
    public function rules()
40
    {
41
        return [
42
            [['password', 'new_password', 'new_password_repeat'], 'required'],
43
            [['password', 'new_password', 'new_password_repeat'], 'string', 'min' => 6, 'max' => 32],
44
            ['password', 'validatePassword'],
45
            ['new_password', 'compare'],
46
        ];
47
    }
48
49
    public function clearAttributes()
50
    {
51
        $this->password = '';
52
        $this->new_password = '';
53
        $this->new_password_repeat = '';
54
    }
55
56
    /**
57
     * Validates the password.
58
     * This method serves as the inline validation for password.
59
     *
60
     * @param string $attribute the attribute currently being validated
61
     * @param array $params the additional name-value pairs given in the rule
62
     */
63
    public function validatePassword($attribute, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        if (!$this->hasErrors()) {
66
            $user = $this->getUser();
67
68
            if (!$user || !$user->validatePassword($this->password)) {
0 ignored issues
show
Bug introduced by
The method validatePassword cannot be called on $user (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
                $this->addError($attribute, 'Incorrect password.');
70
            }
71
        }
72
    }
73
74
    /**
75
     * Change password.
76
     * @return boolean Whether the password changed.
77
     */
78
    public function changePassword()
79
    {
80
        if ($this->validate()) {
81
            if (!($user = $this->getUser())) {
82
                return false;
83
            }
84
            if (!$user->applyForNewPassword()) {
0 ignored issues
show
Bug introduced by
The method applyForNewPassword cannot be called on $user (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
85
                return false;
86
            }
87
            return $user->resetPassword($this->new_password, $user->getPasswordResetToken());
0 ignored issues
show
Bug introduced by
The method getPasswordResetToken cannot be called on $user (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method resetPassword cannot be called on $user (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
88
        }
89
        return false;
90
    }
91
92
    /**
93
     * Set user.
94
     * @param User|string|integer $user
95
     */
96
    public function setUser($user)
97
    {
98
        if ($user instanceof User) {
99
            $this->_user = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user of type object<rhosocial\user\User> is incompatible with the declared type boolean of property $_user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
            return true;
101
        }
102
        $this->_user = null;
103
        return false;
104
    }
105
106
    /**
107
     * Finds user.
108
     *
109
     * @return User|null
110
     */
111
    public function getUser()
112
    {
113
        return $this->_user;
114
    }
115
}