Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

SignupForm::passwordVerifyRules()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 1
nop 0
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Modules\Auth\Form;
6
7
use App\Modules\Auth\AuthService;
8
use Yiisoft\Form\FormModel;
9
use Yiisoft\Translator\TranslatorInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule\Required;
12
13
final class SignupForm extends FormModel
14
{
15
    private string $login = '';
16
    private string $password = '';
17
    private string $passwordVerify = '';
18
19
    public function __construct(private AuthService $authService, private TranslatorInterface $translator)
20
    {
21
        parent::__construct();
22
    }
23
24
    public function getAttributeLabels(): array
25
    {
26
        return [
27
            'email' => $this->translator->translate('layout.login'),
28
            'password' => $this->translator->translate('layout.password'),
29
            'passwordVerify' => $this->translator->translate('layout.password-verify'),
30
        ];
31
    }
32
33
    public function getFormName(): string
34
    {
35
        return 'Signup';
36
    }
37
38
    public function getLogin(): string
39
    {
40
        return $this->login;
41
    }
42
43
    public function getPassword(): string
44
    {
45
        return $this->password;
46
    }
47
48
    public function getRules(): array
49
    {
50
        return [
51
            'login' => [new Required()],
52
            'password' => [new Required()],
53
            'passwordVerify' => $this->passwordVerifyRules(),
54
        ];
55
    }
56
57
    private function passwordVerifyRules(): array
58
    {
59
        return [
60
            new Required(),
61
62
            function (): Result {
63
                $result = new Result();
64
                if ($this->password !== $this->passwordVerify) {
65
                    $this
66
                        ->getFormErrors()
67
                        ->addError('password', '');
68
                    $result->addError($this->translator->translate('validator.password.not.match'));
69
                }
70
71
                if ($result->getErrors() === [] && !$this->authService->signup($this->login, $this->password)) {
72
                    $result->addError($this->translator->translate('validator.user.exist'));
73
                }
74
75
                return $result;
76
            },
77
        ];
78
    }
79
}
80