Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ExpireDocumentAction | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Documents\Application\Actions; |
| 6 | |
| 7 | use ImovelZapAi\Documents\Application\Services\DocumentHistoryWriter; |
| 8 | use ImovelZapAi\Documents\Domain\Enums\DocumentHistoryEvent; |
| 9 | use ImovelZapAi\Documents\Domain\Enums\DocumentStatus; |
| 10 | use ImovelZapAi\Documents\Domain\Events\DocumentExpired; |
| 11 | use ImovelZapAi\Documents\Domain\Exceptions\DocumentException; |
| 12 | use ImovelZapAi\Documents\Infrastructure\Persistence\Eloquent\BusinessDocumentModel; |
| 13 | |
| 14 | final class ExpireDocumentAction |
| 15 | { |
| 16 | public function __construct(private readonly DocumentHistoryWriter $history) {} |
| 17 | |
| 18 | public function execute(BusinessDocumentModel $document): BusinessDocumentModel |
| 19 | { |
| 20 | if (in_array($document->status, [DocumentStatus::Accepted, DocumentStatus::Rejected, DocumentStatus::Expired], true)) { |
| 21 | throw DocumentException::invalidState('documento já encerrado.'); |
| 22 | } |
| 23 | |
| 24 | $document->forceFill([ |
| 25 | 'status' => DocumentStatus::Expired, |
| 26 | 'decided_at' => now(), |
| 27 | ])->save(); |
| 28 | |
| 29 | $this->history->write( |
| 30 | $document->tenant_id, |
| 31 | $document->id, |
| 32 | DocumentHistoryEvent::Expired, |
| 33 | actorType: 'system', |
| 34 | message: 'Documento expirado.', |
| 35 | ); |
| 36 | |
| 37 | DocumentExpired::dispatch($document->tenant_id, $document->id); |
| 38 | |
| 39 | return $document->fresh(); |
| 40 | } |
| 41 | } |