Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CadastroUsuarioService
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace Core\Services\Usuario;
4
5use Core\Models\{
6    Usuario,
7    CPF,
8    Email,
9    Papel,
10};
11
12use Core\Repositories\{
13    IUsuariosRepository,
14};
15
16use Core\Providers\{
17    ICriptografiaProvider,
18};
19
20use Core\Exceptions\ValidationException;
21
22class CadastroUsuarioService
23{
24    function __construct(
25        private IUsuariosRepository $repo,
26        private ICriptografiaProvider $crypt,
27    ){}
28
29    public function execute(
30        string $nome,
31        string $cpf,
32        string $senha,
33        string $email,
34        string $papel = '',
35    ): Usuario {
36        if(empty($papel)) {
37            $papel = Papel::$MEMBRO;
38        }
39
40        $senhaCriptografada = $this->crypt->encrypt($senha);
41
42        $usuarioExistente = $this->repo->findByEmail($email) ;
43
44        if(!is_null($usuarioExistente)) {
45            throw new ValidationException("Email já cadastrado", $email);
46        }
47
48        $usuarioExistente = $this->repo->findByCpf($cpf) ;
49
50        if(!is_null($usuarioExistente)) {
51            throw new ValidationException("CPF já cadastrado", $email);
52        }
53
54        $u = new Usuario(
55            nome:   $nome,
56            cpf:    new CPF($cpf),
57            senha:  $senhaCriptografada,
58            email:  new Email($email),
59            papel:  new Papel($papel),
60        );
61
62        return $this->repo->save($u);
63    }
64}