37 lines
948 B
PHP
37 lines
948 B
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Features\Games\Models\Game;
|
|
use App\Features\Scraper\Services\ScraperService;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class ScrapeCoverJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public function __construct(
|
|
public Game $game,
|
|
) {}
|
|
|
|
public function handle(ScraperService $scraper): void
|
|
{
|
|
$result = $scraper->search($this->game->title, $this->game->console?->slug);
|
|
|
|
if ($result->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$bestMatch = $result->first();
|
|
|
|
if ($bestMatch->coverUrl) {
|
|
$path = $scraper->downloadCover($bestMatch->coverUrl, $this->game);
|
|
$this->game->update(['cover_path' => $path]);
|
|
}
|
|
}
|
|
}
|