Passed
Pull Request — master (#602)
by Ross
02:01
created

ChangePasswordForm::getAttributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth\Form;
6
7
use App\User\UserRepository;
8
use App\Auth\AuthService;
9
use Yiisoft\FormModel\FormModel;
0 ignored issues
show
Bug introduced by
The type Yiisoft\FormModel\FormModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Yiisoft\Translator\TranslatorInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Translator\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\Validator\Result;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Result was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Yiisoft\Validator\Rule\Callback;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Rule\Callback was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Validator\Rule\Length;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Rule\Length was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Validator\Rule\Required;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Rule\Required was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yiisoft\Validator\ValidatorInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\ValidatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Yiisoft\Validator\RulesProviderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\RulesProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
final class ChangePasswordForm extends FormModel implements RulesProviderInterface
19
{
20
    private string $login = '';
21
    private string $password = '';
22
    private string $newPassword = '';
23
    private string $newPasswordVerify = '';
24
    
25
    public function __construct(
26
        private AuthService $authService,
27
        private ValidatorInterface $validator,
28
        private TranslatorInterface $translator,
29
        private UserRepository $userRepository,
30
    ) {
31
    }
32
    
33
    public function change(): bool
34
    {
35
        if ($this->validator->validate($this)->isValid()) {
36
            $user = $this->userRepository->findByLogin($this->getLogin());
37
            if (null!==$user) {
38
              $user->setPassword($this->getNewPassword());
39
              // The cookie identity auth_key is regenerated on logout
40
              // Refer to ResetController reset function
41
              $this->userRepository->save($user);
42
              return true;
43
            }
44
        }
45
        return false;
46
    }
47
48
    /**
49
     * @return string[]
50
     *
51
     * @psalm-return array{login: string, password: string, newPassword: string, newPasswordVerify: string}
52
     */
53
    public function getAttributeLabels(): array
54
    {
55
        return [
56
            'login' => $this->translator->translate('layout.login'),
57
            'password' => $this->translator->translate('layout.password'),
58
            'newPassword' => $this->translator->translate('layout.password.new'),
59
            'newPasswordVerify' => $this->translator->translate('layout.password-verify.new'),
60
        ];
61
    }
62
63
    /**
64
     * @return string
65
     *
66
     * @psalm-return 'Change'
67
     */
68
    public function getFormName(): string
69
    {
70
        return 'ChangePassword';
71
    }
72
    
73
    public function getLogin(): string
74
    {
75
        return $this->login;
76
    }
77
    
78
    public function getPassword(): string
79
    {
80
        return $this->password;
81
    }
82
        
83
    public function getNewPassword(): string
84
    {  
85
        return $this->newPassword;
86
    }
87
    
88
    public function getNewPasswordVerify() : string
89
    {
90
        return $this->newPasswordVerify;
91
    }
92
    
93
    public function getRules(): array
94
    {
95
        return [
96
            'login' => [
97
                new Required(),
98
                new Length(min: 1, max: 48, skipOnError: true),
99
                function (mixed $value): Result {
100
                    $result = new Result();
101
                    if ($this->userRepository->findByLogin((string)$value) == null) {
102
                        $result->addError($this->translator->translate('validator.user.exist.not'));
103
                    }
104
                    return $result;
105
                },
106
            ],
107
            'password' => $this->PasswordRules(),
108
            'newPassword' => [
109
                new Required(),
110
                new Length(min: 8),
111
            ], 
112
            'newPasswordVerify' => $this->NewPasswordVerifyRules()
113
        ];
114
    }
115
    
116
    private function passwordRules(): array
117
    {
118
        return [
119
            new Required(),
120
            new Callback(
121
                callback: function (): Result {
122
                    $result = new Result();
123
                    if (!$this->authService->login($this->login, $this->password)) {
124
                      $result->addError($this->translator->translate('validator.invalid.login.password'));
125
                    }
126
                    
127
                    return $result;
128
                },
129
                skipOnEmpty: true,
130
            ),
131
        ];
132
    }
133
        
134
    private function newPasswordVerifyRules(): array
135
    {
136
        return [
137
            new Required(),
138
            new Callback(
139
                callback: function (): Result {
140
                    $result = new Result();
141
                    if (!($this->newPassword === $this->newPasswordVerify)) {
142
                      $result->addError($this->translator->translate('validator.password.not.match.new'));
143
                    }
144
                    return $result;
145
                },
146
                skipOnEmpty: true,
147
            ),
148
        ];
149
    }
150
}
151