Files
cv4/app/Jobs/ImageUpsert.php
2025-01-18 19:05:23 -05:00

59 lines
1.5 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Image;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;
use romanzipp\QueueMonitor\Traits\IsMonitored;
class ImageUpsert implements ShouldQueue
{
use IsMonitored, Queueable;
/**
* Create a new job instance.
*/
public function __construct(
public int $comicId,
public int $chapterId,
public array $chapter
){}
/**
* Execute the job.
*/
public function handle(): void
{
$this->queueProgress(0);
Log::info("JOB ImageUpsert START, comicId: {$this->comicId}, chapterId: {$this->chapterId}");
$this->queueData([
'comicId' => $this->comicId,
'chapterId' => $this->chapterId,
]);
$arrayForUpsert = [];
foreach ($this->chapter['sorted'] as $k => $image) {
$metadata = $this->chapter;
unset($metadata['sorted']);
unset($metadata['chapter']['contents'], $metadata['chapter']['words']);
$arrayForUpsert[] = [
'comic_id' => $this->comicId,
'chapter_id' => $this->chapterId,
'order' => $k,
'url' => $image['url'],
'metadata' => json_encode($metadata),
];
}
// Do an upsert
Image::upsert($arrayForUpsert, uniqueBy: 'url');
Log::info('JOB ImageUpsert END');
$this->queueProgress(100);
}
}