Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.00% |
24 / 25 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ConfirmImportAction | |
96.00% |
24 / 25 |
|
50.00% |
1 / 2 |
5 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Importing\Application\Actions; |
| 6 | |
| 7 | use ImovelZapAi\Importing\Application\Services\ImportLogWriter; |
| 8 | use ImovelZapAi\Importing\Domain\Enums\ImportJobStatus; |
| 9 | use ImovelZapAi\Importing\Domain\Enums\ImportLogEvent; |
| 10 | use ImovelZapAi\Importing\Domain\Exceptions\ImportException; |
| 11 | use ImovelZapAi\Importing\Infrastructure\Jobs\ExecuteImportJob; |
| 12 | use ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent\ImportJobModel; |
| 13 | |
| 14 | final class ConfirmImportAction |
| 15 | { |
| 16 | public function __construct( |
| 17 | private readonly ImportLogWriter $logWriter, |
| 18 | ) {} |
| 19 | |
| 20 | public function execute(ImportJobModel $job): ImportJobModel |
| 21 | { |
| 22 | $status = $job->status instanceof ImportJobStatus |
| 23 | ? $job->status |
| 24 | : ImportJobStatus::from((string) $job->status); |
| 25 | |
| 26 | if (! in_array($status, [ImportJobStatus::Ready, ImportJobStatus::PreviewReady], true)) { |
| 27 | throw ImportException::invalidState($status->value, 'confirm'); |
| 28 | } |
| 29 | |
| 30 | $ambiguities = $job->preview_summary['ambiguities'] ?? []; |
| 31 | if ($ambiguities !== []) { |
| 32 | throw ImportException::invalidState($status->value, 'confirm'); |
| 33 | } |
| 34 | |
| 35 | $job->forceFill([ |
| 36 | 'status' => ImportJobStatus::Queued, |
| 37 | 'queued_at' => now(), |
| 38 | 'progress_current' => 0, |
| 39 | 'progress_total' => (int) $job->total_listings, |
| 40 | 'checkpoint_index' => 0, |
| 41 | ])->save(); |
| 42 | |
| 43 | $this->logWriter->write( |
| 44 | $job->tenant_id, |
| 45 | $job->id, |
| 46 | ImportLogEvent::ImportQueued, |
| 47 | message: 'Importação enfileirada.', |
| 48 | ); |
| 49 | |
| 50 | ExecuteImportJob::dispatch($job->id, $job->tenant_id) |
| 51 | ->onQueue((string) config('importing.queue', 'imports')); |
| 52 | |
| 53 | return $job->fresh(); |
| 54 | } |
| 55 | } |