Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| EmprestimoService | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Services\Emprestimo; |
| 4 | |
| 5 | use Core\Models\Emprestimo; |
| 6 | |
| 7 | use Core\Repositories\{ |
| 8 | IEmprestimosRepository, |
| 9 | IUsuariosRepository, |
| 10 | ILivrosRepository, |
| 11 | }; |
| 12 | |
| 13 | use Core\Exceptions\ValidationException; |
| 14 | |
| 15 | use Core\Traits\Clock; |
| 16 | |
| 17 | class EmprestimoService |
| 18 | { |
| 19 | use Clock; |
| 20 | |
| 21 | function __construct( |
| 22 | private IEmprestimosRepository $emprestimosRepo, |
| 23 | private ILivrosRepository $livrosRepo, |
| 24 | private IUsuariosRepository $usuariosRepo, |
| 25 | ){} |
| 26 | |
| 27 | public function execute( |
| 28 | int $idLivro, |
| 29 | int $idMembro, |
| 30 | int $idColaborador, |
| 31 | ): Emprestimo { |
| 32 | $livro = $this->livrosRepo->findById($idLivro); |
| 33 | $membro = $this->usuariosRepo->findById($idMembro); |
| 34 | $colaborador = $this->usuariosRepo->findById($idColaborador); |
| 35 | |
| 36 | $livrosEmprestados = count( |
| 37 | $this->emprestimosRepo->findNaoDevolvidosByIdLivro($idLivro) |
| 38 | ); |
| 39 | |
| 40 | $livrosEmprestados = $this |
| 41 | ->emprestimosRepo |
| 42 | ->findNaoDevolvidosByIdLivro($idLivro); |
| 43 | $totalDeLivrosEmprestados = count($livrosEmprestados); |
| 44 | |
| 45 | if($totalDeLivrosEmprestados >= $livro->quantidade) { |
| 46 | throw new ValidationException("Livro indisponível", $idLivro); |
| 47 | } |
| 48 | |
| 49 | $e = new Emprestimo( |
| 50 | livro: $livro, |
| 51 | usuarioMembro: $membro, |
| 52 | usuarioColaborador: $colaborador, |
| 53 | dataEmprestimo: $this->now(), |
| 54 | ); |
| 55 | |
| 56 | return $this->emprestimosRepo->save($e); |
| 57 | } |
| 58 | } |