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

ChangePasswordForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 $passwordVerify = '';
23
    private string $newPassword = '';
24
    private string $newPasswordVerify = '';
25
    
26
    public function __construct(
27
        private AuthService $authService,
28
        private ValidatorInterface $validator,
29
        private TranslatorInterface $translator,
30
        private UserRepository $userRepository,
31
    ) {
32
    }
33
    
34
    public function change(): bool
35
    {
36
        if ($this->validator->validate($this)->isValid()) {
37
            $user = $this->userRepository->findByLogin($this->getLogin());
38
            if (null!==$user) {
39
              $user->setPassword($this->getNewPassword());
40
              // The cookie identity auth_key is regenerated on logout
41
              // Refer to ResetController reset function
42
              $this->userRepository->save($user);
43
              return true;
44
            }
45
        }
46
        return false;
47
    }
48
49
    /**
50
     * @return string[]
51
     *
52
     * @psalm-return array{login: string, password: string, passwordVerify: string, newPassword: string, newPasswordVerify: string}
53
     */
54
    public function getAttributeLabels(): array
55
    {
56
        return [
57
            'login' => $this->translator->translate('layout.login'),
58
            'password' => $this->translator->translate('layout.password'),
59
            'passwordVerify' => $this->translator->translate('layout.password-verify'),
60
            'newPassword' => $this->translator->translate('layout.password.new'),
61
            'newPasswordVerify' => $this->translator->translate('layout.password-verify.new'),
62
        ];
63
    }
64
65
    /**
66
     * @return string
67
     *
68
     * @psalm-return 'Change'
69
     */
70
    public function getFormName(): string
71
    {
72
        return 'ChangePassword';
73
    }
74
    
75
    public function getLogin(): string
76
    {
77
        return $this->login;
78
    }
79
    
80
    public function getPassword(): string
81
    {
82
        return $this->password;
83
    }
84
    
85
    public function getPasswordVerify(): string
86
    {  
87
        return $this->passwordVerify;
88
    }
89
    
90
    public function getNewPassword(): string
91
    {  
92
        return $this->newPassword;
93
    }
94
    
95
    public function getNewPasswordVerify() : string
96
    {
97
        return $this->newPasswordVerify;
98
    }
99
    
100
    public function getRules(): array
101
    {
102
        return [
103
            'login' => [
104
                new Required(),
105
                new Length(min: 1, max: 48, skipOnError: true),
106
                function (mixed $value): Result {
107
                    $result = new Result();
108
                    if ($this->userRepository->findByLogin((string)$value) == null) {
109
                        $result->addError($this->translator->translate('validator.user.exist.not'));
110
                    }
111
                    return $result;
112
                },
113
            ],
114
            'password' => $this->PasswordRules(),
115
            'passwordVerify' => $this->PasswordVerifyRules(),
116
            'newPassword' => [
117
                new Required(),
118
                new Length(min: 8),
119
            ], 
120
            'newPasswordVerify' => $this->NewPasswordVerifyRules()
121
        ];
122
    }
123
    
124
    private function passwordRules(): array
125
    {
126
        return [
127
            new Required(),
128
            new Callback(
129
                callback: function (): Result {
130
                    $result = new Result();
131
                    if (!$this->authService->login($this->login, $this->password)) {
132
                      $result->addError($this->translator->translate('validator.invalid.login.password'));
133
                    }
134
                    
135
                    return $result;
136
                },
137
                skipOnEmpty: true,
138
            ),
139
        ];
140
    }
141
        
142
    private function passwordVerifyRules(): array
143
    {
144
        return [
145
            new Required(),
146
            new Callback(
147
                callback: function (): Result {
148
                    $result = new Result();
149
                    if (!($this->password === $this->passwordVerify)) {
150
                        $result->addError($this->translator->translate('validator.password.not.match'));
151
                    }
152
                    return $result;
153
                },
154
                skipOnEmpty: true,
155
            ),
156
        ];
157
    }
158
    
159
    private function newPasswordVerifyRules(): array
160
    {
161
        return [
162
            new Required(),
163
            new Callback(
164
                callback: function (): Result {
165
                    $result = new Result();
166
                    if (!($this->newPassword === $this->newPasswordVerify)) {
167
                      $result->addError($this->translator->translate('validator.password.not.match.new'));
168
                    }
169
                    return $result;
170
                },
171
                skipOnEmpty: true,
172
            ),
173
        ];
174
    }
175
}
176