Test Failed
Pull Request — master (#476)
by
unknown
03:08
created

SignupForm::fillPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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