UpdatePasswordForm   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 33
c 1
b 0
f 0
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A validateCurrentPassword() 0 6 4
A resetPassword() 0 5 1
A attributeLabels() 0 6 1
A processValidatePassword() 0 8 3
A rules() 0 12 1
1
<?php
2
3
namespace modules\users\models;
4
5
use yii\base\Exception;
6
use yii\base\Model;
7
use yii\base\InvalidArgumentException;
8
use modules\users\Module;
9
10
/**
11
 * Class UpdatePasswordForm
12
 * @package modules\users\models
13
 */
14
class UpdatePasswordForm extends Model
15
{
16
    public $newPassword;
17
    public $newPasswordRepeat;
18
    public $currentPassword;
19
20
    /**
21
     * @var User
22
     */
23
    private $user;
24
25
    /**
26
     * UpdatePasswordForm constructor.
27
     * @param User $user
28
     * @param array $config
29
     */
30
    public function __construct(User $user, $config = [])
31
    {
32
        $this->user = $user;
33
        if (!$this->user) {
34
            throw new InvalidArgumentException(Module::translate('module', 'User not found.'));
35
        }
36
        parent::__construct($config);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     * @return array
42
     */
43
    public function rules()
44
    {
45
        return [
46
            [['newPassword', 'newPasswordRepeat', 'currentPassword'], 'required'],
47
            [
48
                'newPassword',
49
                'string',
50
                'min' => User::LENGTH_STRING_PASSWORD_MIN,
51
                'max' => User::LENGTH_STRING_PASSWORD_MAX
52
            ],
53
            ['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'],
54
            ['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false],
55
        ];
56
    }
57
58
    /**
59
     * @param string $attribute
60
     */
61
    public function validateCurrentPassword($attribute)
62
    {
63
        if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) {
64
            $this->processValidatePassword($attribute);
65
        } else {
66
            $this->addError($attribute, Module::translate('module', 'Not all fields are filled in correctly.'));
67
        }
68
    }
69
70
    /**
71
     * @param string $attribute
72
     */
73
    protected function processValidatePassword($attribute)
74
    {
75
        if ($attribute) {
76
            if (!$this->user->validatePassword($this->$attribute)) {
77
                $this->addError($attribute, Module::translate('module', 'Incorrect current password.'));
78
            }
79
        } else {
80
            $this->addError($attribute, Module::translate('module', 'Enter your current password.'));
81
        }
82
    }
83
84
    /**
85
     * @inheritdoc
86
     * @return array
87
     */
88
    public function attributeLabels()
89
    {
90
        return [
91
            'newPassword' => Module::translate('module', 'New Password'),
92
            'newPasswordRepeat' => Module::translate('module', 'Repeat Password'),
93
            'currentPassword' => Module::translate('module', 'Current Password'),
94
        ];
95
    }
96
97
    /**
98
     * Resets password.
99
     *
100
     * @return bool if password was reset.
101
     * @throws Exception
102
     */
103
    public function resetPassword()
104
    {
105
        $user = $this->user;
106
        $user->setPassword($this->newPassword);
107
        return $user->save();
108
    }
109
}
110