64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Game;
|
|
use App\Services\IgdbService;
|
|
use Illuminate\Console\Command;
|
|
|
|
class FetchIgdbGames extends Command
|
|
{
|
|
protected $signature = 'igdb:fetch {query?} {--limit=10}';
|
|
protected $description = 'Fetch games from IGDB and store them in the database';
|
|
|
|
public function handle(IgdbService $igdbService): int
|
|
{
|
|
$query = $this->argument('query') ?? $this->ask('Enter search query');
|
|
$limit = (int) $this->option('limit');
|
|
|
|
$this->info("Fetching games for query: {$query}");
|
|
|
|
$games = $igdbService->searchGames($query, $limit);
|
|
|
|
if (empty($games)) {
|
|
$this->error('No games found or an error occurred.');
|
|
return 1;
|
|
}
|
|
|
|
$this->info(sprintf('Found %d games. Processing...', count($games)));
|
|
|
|
$bar = $this->output->createProgressBar(count($games));
|
|
$bar->start();
|
|
|
|
foreach ($games as $gameData) {
|
|
$this->processGame($gameData);
|
|
$bar->advance();
|
|
}
|
|
|
|
$bar->finish();
|
|
$this->newLine();
|
|
$this->info('Done!');
|
|
|
|
return 0;
|
|
}
|
|
|
|
protected function processGame(array $gameData): void
|
|
{
|
|
$game = Game::updateOrCreate(
|
|
['igdb_id' => $gameData['id']],
|
|
[
|
|
'name' => $gameData['name'],
|
|
'summary' => $gameData['summary'] ?? null,
|
|
'cover_url' => $gameData['cover']['url'] ?? null,
|
|
'slug' => $gameData['slug'] ?? null,
|
|
'genres' => array_column($gameData['genres'] ?? [], 'name'),
|
|
'platforms' => array_column($gameData['platforms'] ?? [], 'name'),
|
|
'first_release_date' => isset($gameData['first_release_date'])
|
|
? date('Y-m-d', $gameData['first_release_date'])
|
|
: null,
|
|
'rating' => $gameData['rating'] ?? null,
|
|
'rating_count' => $gameData['rating_count'] ?? null,
|
|
]
|
|
);
|
|
}
|
|
}
|