Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DevolucaoService
100.00% covered (success)
100.00%
8 / 8
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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace Core\Services\Emprestimo;
4
5use Core\Models\Emprestimo;
6
7use Core\Repositories\{
8    IEmprestimosRepository,
9};
10
11use Core\Traits\Clock;
12
13use Core\Exceptions\ValidationException;
14
15class DevolucaoService
16{
17    use Clock;
18
19    function __construct(
20        private IEmprestimosRepository $emprestimosRepo,
21    ){}
22
23    public function execute(int $idEmprestimo): Emprestimo
24    {
25        $e = $this->emprestimosRepo->findById($idEmprestimo);
26
27        if(is_null($e)) {
28            throw new ValidationException("Empréstimo indisponível", $idEmprestimo);
29        }
30
31        if(!is_null($e->dataEntregaRealizada) || !$e->getAtivo()) {
32            throw new ValidationException("Empréstimo indisponível", $idEmprestimo);
33        }
34
35        $e->dataEntregaRealizada = new \DateTimeImmutable();
36
37        return $this->emprestimosRepo->save($e);
38    }
39}
40