Completed
Push — master ( 7d233a...8ee6e7 )
by Dmitry
12:15
created

PasswordValidator::validateAttribute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
3
namespace hiam\mrdp\validators;
4
5
use hiam\validators\PasswordValidatorInterface;
6
use Yii;
7
use yii\base\Model;
8
use yii\validators\Validator;
9
use hiam\mrdp\storage\Client;
10
11
class PasswordValidator extends Validator implements PasswordValidatorInterface
12
{
13
    /**
14
     * @var string login attribute name
15
     */
16
    public $loginAttribute = 'login';
17
18
    /**
19
     * @var string password attribute name
20
     */
21
    public $passwordAttribute = 'current_password';
22
23 View Code Duplication
    public function init()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        parent::init();
26
        if ($this->message !== null) {
27
            return;
28
        }
29
        $this->message = Yii::t('hiam', 'The current password is incorrect');
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function validateAttribute($model, $attribute)
36
    {
37
        if (!empty($model->{$this->loginAttribute}) && !empty($model->{$this->passwordAttribute})) {
38
            $found = Client::find()->andWhere([
39
                'username' => $model->{$this->loginAttribute},
40
                'password' => $model->{$this->passwordAttribute},
41
            ])->one();
42
            if (!$found) {
43
                $this->addError($model, $attribute, $this->message);
44
            }
45
        }
46
    }
47
48
    public function inlineFor(Model $model): \Closure
49
    {
50
        return function (string $attribute) use ($model) {
51
            $this->validateAttribute($model, $attribute);
52
        };
53
    }
54
}
55