Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CadastroLivroService | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Services\Emprestimo; |
| 4 | |
| 5 | use Core\Models\{ |
| 6 | Livro, |
| 7 | Autor, |
| 8 | }; |
| 9 | |
| 10 | use Core\Repositories\{ |
| 11 | ILivrosRepository, |
| 12 | IAutoresRepository, |
| 13 | }; |
| 14 | |
| 15 | class CadastroLivroService |
| 16 | { |
| 17 | function __construct( |
| 18 | private ILivrosRepository $livrosRepo, |
| 19 | private IAutoresRepository $autoresRepo, |
| 20 | ){} |
| 21 | |
| 22 | public function execute( |
| 23 | string $titulo, |
| 24 | int $idAutor, |
| 25 | string $quantidade, |
| 26 | ?int $id = null, |
| 27 | ): Livro { |
| 28 | $autor = $this->autoresRepo->findById($idAutor); |
| 29 | |
| 30 | $l = null; |
| 31 | |
| 32 | if(!is_null($id)) { |
| 33 | $l = $this->livrosRepo->findById($id); |
| 34 | $l->titulo = $titulo; |
| 35 | $l->autor = $autor; |
| 36 | $l->quantidade = $quantidade; |
| 37 | } else { |
| 38 | $l = new Livro( |
| 39 | titulo: $titulo, |
| 40 | autor: $autor, |
| 41 | quantidade: $quantidade, |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | return $this->livrosRepo->save($l); |
| 46 | } |
| 47 | } |