75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Console\Commands\Concerns\UpsertsGames;
|
|
use App\Services\IgdbService;
|
|
use Illuminate\Console\Command;
|
|
|
|
class ImportGamesCommand extends Command
|
|
{
|
|
use UpsertsGames;
|
|
protected $signature = 'games:import {--limit=100} {--offset=0} {--popular} {--new} {--year=} {--platform=}';
|
|
protected $description = 'Import games from IGDB into the local database';
|
|
|
|
public function handle(IgdbService $igdb): int
|
|
{
|
|
$limit = (int) $this->option('limit');
|
|
$offset = (int) $this->option('offset');
|
|
$year = $this->option('year') ? (int) $this->option('year') : null;
|
|
$platform = $this->option('platform') ? (int) $this->option('platform') : null;
|
|
|
|
if ($year && $platform) {
|
|
$this->error('Use --year or --platform, not both.');
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$label = match (true) {
|
|
$year !== null => "year {$year}",
|
|
$platform !== null => "platform IGDB ID {$platform}",
|
|
$this->option('new') => 'new releases',
|
|
default => "popular (offset: {$offset})",
|
|
};
|
|
$this->info("Fetching {$limit} games from IGDB ({$label})…");
|
|
|
|
try {
|
|
$results = match (true) {
|
|
$year !== null => $igdb->getGamesByYear($year, $limit, $offset),
|
|
$platform !== null => $igdb->getGamesByPlatform($platform, $limit, $offset),
|
|
$this->option('new') => $igdb->getNewReleases($limit),
|
|
default => $igdb->getPopularGames($limit, $offset),
|
|
};
|
|
} catch (\Exception $e) {
|
|
$this->error('IGDB request failed: ' . $e->getMessage());
|
|
return self::FAILURE;
|
|
}
|
|
|
|
if (empty($results)) {
|
|
$this->warn('No games returned from IGDB.');
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$imported = 0;
|
|
$bar = $this->output->createProgressBar(count($results));
|
|
$bar->start();
|
|
|
|
foreach ($results as $data) {
|
|
try {
|
|
$this->upsertGame($data, $igdb);
|
|
$imported++;
|
|
} catch (\Exception $e) {
|
|
$this->newLine();
|
|
$this->warn("Skipped game (igdb_id={$data['id']}): " . $e->getMessage());
|
|
}
|
|
$bar->advance();
|
|
}
|
|
|
|
$bar->finish();
|
|
$this->newLine(2);
|
|
$this->info("Done. Imported/updated {$imported} games.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
}
|