Test Failed
Pull Request — master (#408)
by Wilmer
03:55
created

SignupForm::passwordVerifyRules()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.9666
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
    public function __construct(AuthService $authService, TranslatorInterface $translator)
22
    {
23
        parent::__construct();
24
25
        $this->authService = $authService;
26
        $this->translator = $translator;
27
    }
28
29
    public function getAttributeLabels(): array
30
    {
31
        return [
32
            'email' => $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 getRules(): array
54
    {
55
        return [
56
            'login' => [Required::rule()],
57
            'password' => [Required::rule()],
58
            'passwordVerify' => $this->passwordVerifyRules(),
59
        ];
60
    }
61
62
    private function passwordVerifyRules(): array
63
    {
64
        return [
65
            Required::rule(),
66
67
            function (): Result {
68
                $result = new Result();
69
                if ($this->password !== $this->passwordVerify) {
70
                    $result->addError($this->translator->translate('validator.password.not.match'));
71
                }
72
73
                if ($result->getErrors() === [] && !$this->authService->signup($this->login, $this->password)) {
74
                    $result->addError($this->translator->translate('validator.user.exist'));
75
                }
76
77
                return $result;
78
            },
79
        ];
80
    }
81
}
82