Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| EdicaoUsuarioService | |
94.74% |
18 / 19 |
|
50.00% |
1 / 2 |
8.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
7.01 | |||
| 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 | |
| 17 | use Core\Exceptions\ValidationException; |
| 18 | |
| 19 | class EdicaoUsuarioService |
| 20 | { |
| 21 | function __construct( |
| 22 | private IUsuariosRepository $repo, |
| 23 | ){} |
| 24 | |
| 25 | public function execute( |
| 26 | int $id, |
| 27 | string $nome, |
| 28 | string $cpf, |
| 29 | string $email, |
| 30 | string $papel = '', |
| 31 | ): Usuario { |
| 32 | if(empty($papel)) { |
| 33 | $papel = Papel::$MEMBRO; |
| 34 | } |
| 35 | |
| 36 | $u = $this->repo->findById($id) ; |
| 37 | |
| 38 | if(is_null($u)) { |
| 39 | throw new ValidationException("Id inexistente", $id); |
| 40 | } |
| 41 | |
| 42 | if($email !== $u->email->getEmail()) { |
| 43 | $emailEncontrado = $this->repo->findByEmail($email); |
| 44 | |
| 45 | if(!is_null($emailEncontrado)) { |
| 46 | throw new ValidationException("Email já cadastrado", $email); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if($cpf !== $u->cpf->getNumero()) { |
| 51 | $cpfEncontrado = $this->repo->findByCpf($cpf) ; |
| 52 | if(!is_null($cpfEncontrado)) { |
| 53 | throw new ValidationException("Cpf já cadastrado", $cpf); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | $u->nome = $nome; |
| 58 | $u->cpf = new CPF($cpf); |
| 59 | $u->email = new Email($email); |
| 60 | $u->papel = new Papel($papel); |
| 61 | |
| 62 | return $this->repo->save($u); |
| 63 | } |
| 64 | } |