Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CadastroUsuarioService | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Services\Usuario; |
| 4 | |
| 5 | use Core\Models\{ |
| 6 | Usuario, |
| 7 | CPF, |
| 8 | Email, |
| 9 | Papel, |
| 10 | }; |
| 11 | |
| 12 | use Core\Repositories\{ |
| 13 | IUsuariosRepository, |
| 14 | }; |
| 15 | |
| 16 | use Core\Providers\{ |
| 17 | ICriptografiaProvider, |
| 18 | }; |
| 19 | |
| 20 | use Core\Exceptions\ValidationException; |
| 21 | |
| 22 | class 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 | } |