Files

75 lines
2.5 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
2026-05-31 16:14:40 +03:00
use App\Console\Commands\Concerns\UpsertsGames;
use App\Services\IgdbService;
use Illuminate\Console\Command;
class ImportGamesCommand extends Command
{
2026-05-31 16:14:40 +03:00
use UpsertsGames;
2026-05-30 18:56:17 +03:00
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
{
2026-05-30 18:56:17 +03:00
$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;
2026-05-30 18:56:17 +03:00
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 {
2026-05-30 18:56:17 +03:00
$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;
}
}