Passed
Push — master ( 6dbcb1...96b972 )
by
unknown
01:44
created

SignupForm::getPropertyLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
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\User;
8
use App\User\UserRepository;
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\Equal;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Validator\Rule\Equal 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\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...
16
17
final class SignupForm extends FormModel implements RulesProviderInterface
18
{
19
    private string $login = '';
20
    private string $password = '';
21
    private string $passwordVerify = '';
0 ignored issues
show
introduced by
The private property $passwordVerify is not used, and could be removed.
Loading history...
22
23
    public function __construct(
24
        private readonly TranslatorInterface $translator,
25
        private readonly UserRepository $userRepository,
26
    ) {
27
    }
28
29
    public function getPropertyLabels(): array
30
    {
31
        return [
32
            'login' => $this->translator->translate('layout.login'),
33
            'password' => $this->translator->translate('layout.password'),
34
            'passwordVerify' => $this->translator->translate('layout.password-verify'),
35
        ];
36
    }
37
38
    public function getFormName(): string
39
    {
40
        return 'Signup';
41
    }
42
43
    public function getLogin(): string
44
    {
45
        return $this->login;
46
    }
47
48
    public function getPassword(): string
49
    {
50
        return $this->password;
51
    }
52
53
    public function signup(): User
54
    {
55
        $user = new User($this->getLogin(), $this->getPassword());
56
        $this->userRepository->save($user);
57
        return $user;
58
    }
59
60
    public function getRules(): array
61
    {
62
        return [
63
            'login' => [
64
                new Required(),
65
                new Length(min: 1, max: 48, skipOnError: true),
66
                function ($value): Result {
67
                    $result = new Result();
68
                    if ($this->userRepository->findByLogin($value) !== null) {
69
                        $result->addError('User with this login already exists.');
70
                    }
71
72
                    return $result;
73
                },
74
            ],
75
            'password' => [
76
                new Required(),
77
                new Length(min: 8),
78
            ],
79
            'passwordVerify' => [
80
                new Required(),
81
                new Equal(
82
                    targetValue: $this->password,
83
                    message: $this->translator->translate('validator.password.not.match')
84
                ),
85
            ],
86
        ];
87
    }
88
}
89