These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace hiam\mrdp\validators; |
||
4 | |||
5 | use hiam\validators\LoginValidatorInterface; |
||
6 | use Yii; |
||
7 | use yii\base\Model; |
||
8 | use yii\validators\Validator; |
||
9 | use hiam\mrdp\storage\Client; |
||
10 | |||
11 | class LoginValidator extends Validator implements LoginValidatorInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string login attribute name |
||
15 | */ |
||
16 | public $loginAttribute = 'username'; |
||
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
|
|||
24 | { |
||
25 | parent::init(); |
||
26 | if ($this->message !== null) { |
||
27 | return; |
||
28 | } |
||
29 | $this->message = Yii::t('hiam', 'Incorrect login'); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function validateAttribute($model, $attribute) |
||
36 | { |
||
37 | if (!empty($model->{$this->loginAttribute})) { |
||
38 | $found = Client::find()->andWhere([ |
||
39 | 'username' => $model->{$this->loginAttribute}, |
||
40 | ])->one(); |
||
41 | if (!$found) { |
||
42 | $this->addError($model, $attribute, $this->message); |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | |||
47 | public function inlineFor(Model $model): \Closure |
||
48 | { |
||
49 | return function (string $attribute) use ($model) { |
||
50 | $this->validateAttribute($model, $attribute); |
||
51 | }; |
||
52 | } |
||
53 | } |
||
54 |
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.