Completed
Push — master ( 51f787...8bc09a )
by Alexey
03:06
created

UserDeleteForm::processValidatePassword()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace modules\users\models;
4
5
use yii\base\Model;
6
use yii\base\InvalidArgumentException;
7
use modules\users\Module;
8
9
/**
10
 * Class UpdatePasswordForm
11
 * @package modules\users\models
12
 * @property User $user
13
 */
14
class UserDeleteForm extends Model
15
{
16
    public $currentPassword;
17
18
    /**
19
     * @var \modules\users\models\User
20
     */
21
    private $_user;
22
23
    /**
24
     * UpdatePasswordForm constructor.
25
     * @param User $user
26
     * @param array $config
27
     */
28
    public function __construct(User $user, $config = [])
29
    {
30
        $this->_user = $user;
31
        if (!$this->_user) {
32
            throw new InvalidArgumentException(Module::t('module', 'User not found.'));
33
        }
34
        parent::__construct($config);
35
    }
36
37
    /**
38
     * @return User
39
     */
40
    public function getUser()
41
    {
42
        return $this->_user;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     * @return array
48
     */
49
    public function rules()
50
    {
51
        return [
52
            [['currentPassword'], 'required'],
53
            ['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false],
54
        ];
55
    }
56
57
    /**
58
     * @param string $attribute
59
     */
60
    public function validateCurrentPassword($attribute)
61
    {
62
        if (!empty($this->currentPassword) && !$this->hasErrors()) {
63
            $this->processValidatePassword($attribute);
64
        } else {
65
            $this->addError($attribute, Module::t('module', 'Not all fields are filled in correctly.'));
66
        }
67
    }
68
69
    /**
70
     * @param string $attribute
71
     */
72
    protected function processValidatePassword($attribute)
73
    {
74
        if ($attribute) {
75
            if (!$this->_user->validatePassword($this->$attribute))
76
                $this->addError($attribute, Module::t('module', 'Incorrect current password.'));
77
        } else {
78
            $this->addError($attribute, Module::t('module', 'Enter your current password.'));
79
        }
80
    }
81
82
    /**
83
     * @inheritdoc
84
     * @return array
85
     */
86
    public function attributeLabels()
87
    {
88
        return [
89
            'currentPassword' => Module::t('module', 'Current Password'),
90
        ];
91
    }
92
93
    /**
94
     * @return false|int
95
     * @throws \Throwable
96
     * @throws \yii\db\StaleObjectException
97
     */
98
    public function userDelete()
99
    {
100
        $user = $this->_user;
101
        /** @var $user \yii2tech\ar\softdelete\SoftDeleteBehavior */
102
        return $user->softDelete();
103
    }
104
}
105