Passed
Pull Request — master (#408)
by Wilmer
05:31
created

SignupForm::getAttributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth\Form;
6
7
use App\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
    private AuthService $authService;
19
    private TranslatorInterface $translator;
20
21 5
    public function __construct(AuthService $authService, TranslatorInterface $translator)
22
    {
23 5
        parent::__construct();
24
25 5
        $this->authService = $authService;
26 5
        $this->translator = $translator;
27 5
    }
28
29 5
    public function getAttributeLabels(): array
30
    {
31
        return [
32 5
            'email' => $this->translator->translate('layout.login'),
33 5
            'password' => $this->translator->translate('layout.password'),
34 5
            'passwordVerify' => $this->translator->translate('layout.password-verify'),
35
        ];
36
    }
37
38 5
    public function getFormName(): string
39
    {
40 5
        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 5
    public function getRules(): array
54
    {
55
        return [
56 5
            'login' => [Required::rule()],
57 5
            'password' => [Required::rule()],
58 5
            'passwordVerify' => $this->passwordVerifyRules(),
59
        ];
60
    }
61
62 5
    private function passwordVerifyRules(): array
63
    {
64
        return [
65 5
            Required::rule(),
66
67 5
            function (): Result {
68 3
                $result = new Result();
69 3
                if ($this->password !== $this->passwordVerify) {
70 1
                    $result->addError($this->translator->translate('validator.password.not.match'));
71
                }
72
73 3
                if ($result->getErrors() === [] && !$this->authService->signup($this->login, $this->password)) {
74 1
                    $result->addError($this->translator->translate('validator.user.exist'));
75
                }
76
77 3
                return $result;
78 5
            },
79
        ];
80
    }
81
}
82