Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
21 / 22
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
RetryImportAction
95.45% covered (success)
95.45%
21 / 22
50.00% covered (danger)
50.00%
1 / 2
4
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Importing\Application\Actions;
6
7use ImovelZapAi\Importing\Application\Services\ImportLogWriter;
8use ImovelZapAi\Importing\Domain\Enums\ImportJobStatus;
9use ImovelZapAi\Importing\Domain\Enums\ImportLogEvent;
10use ImovelZapAi\Importing\Domain\Exceptions\ImportException;
11use ImovelZapAi\Importing\Infrastructure\Jobs\ExecuteImportJob;
12use ImovelZapAi\Importing\Infrastructure\Persistence\Eloquent\ImportJobModel;
13
14final class RetryImportAction
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 (! $status->canRetry()) {
27            throw ImportException::invalidState($status->value, 'retry');
28        }
29
30        $job->forceFill([
31            'status' => ImportJobStatus::Queued,
32            'queued_at' => now(),
33            'failure_message' => null,
34            'completed_at' => null,
35        ])->save();
36
37        $this->logWriter->write(
38            $job->tenant_id,
39            $job->id,
40            ImportLogEvent::RetryStarted,
41            message: 'Retry iniciado a partir do checkpoint.',
42            context: ['checkpoint_index' => $job->checkpoint_index],
43        );
44
45        ExecuteImportJob::dispatch($job->id, $job->tenant_id)
46            ->onQueue((string) config('importing.queue', 'imports'));
47
48        return $job->fresh();
49    }
50}