Completed
Push — master ( 0f3d1f...6a4fbf )
by vistart
03:41
created

ChangePasswordForm   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 104
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 8 1
A rules() 0 11 1
A clearAttributes() 0 6 1
A validatePassword() 0 10 4
A changePassword() 0 13 4
A setUser() 0 9 2
A getUser() 0 4 1
A scenarios() 0 6 1
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
    const SCENARIO_ADMIN = 'admin';
31
32
    public function attributeLabels()
33
    {
34
        return [
35
            'password' => Yii::t('user', 'Password'),
36
            'new_password' => Yii::t('user', 'New Password'),
37
            'new_password_repeat' => Yii::t('user', 'New Password Repeat'),
38
        ];
39
    }
40
41
    public function rules()
42
    {
43
        return [
44
            ['password', 'required', 'on' =>self::SCENARIO_DEFAULT],
45
            [['new_password', 'new_password_repeat'], 'required'],
46
            ['password', 'string', 'min' => 6, 'max' => 32, 'on' => self::SCENARIO_DEFAULT],
47
            [['new_password', 'new_password_repeat'], 'string', 'min' => 6, 'max' => 32],
48
            ['password', 'validatePassword', 'on' => self::SCENARIO_DEFAULT],
49
            ['new_password', 'compare'],
50
        ];
51
    }
52
53
    public function clearAttributes()
54
    {
55
        $this->password = '';
56
        $this->new_password = '';
57
        $this->new_password_repeat = '';
58
    }
59
60
    /**
61
     * Validates the password.
62
     * This method serves as the inline validation for password.
63
     *
64
     * @param string $attribute the attribute currently being validated
65
     * @param array $params the additional name-value pairs given in the rule
66
     */
67
    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...
68
    {
69
        if (!$this->hasErrors()) {
70
            $user = $this->getUser();
71
72
            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...
73
                $this->addError($attribute, 'Incorrect password.');
74
            }
75
        }
76
    }
77
78
    /**
79
     * Change password.
80
     * @return boolean Whether the password changed.
81
     */
82
    public function changePassword()
83
    {
84
        if ($this->validate()) {
85
            if (!($user = $this->getUser())) {
86
                return false;
87
            }
88
            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...
89
                return false;
90
            }
91
            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...
92
        }
93
        return false;
94
    }
95
96
    /**
97
     * Set user.
98
     * @param User $user
99
     */
100
    public function setUser($user)
101
    {
102
        if ($user instanceof User) {
103
            $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...
104
            return true;
105
        }
106
        $this->_user = null;
107
        return false;
108
    }
109
110
    /**
111
     * Finds user.
112
     *
113
     * @return User|null
114
     */
115
    public function getUser()
116
    {
117
        return $this->_user;
118
    }
119
120
    public function scenarios()
121
    {
122
        return array_merge(parent::scenarios(), [
123
            self::SCENARIO_ADMIN => ['new_password', 'new_password_repeat'],
124
        ]);
125
    }
126
}