2026-05-29 00:33:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class IgdbService
|
|
|
|
|
{
|
|
|
|
|
protected Client $client;
|
|
|
|
|
protected string $clientId;
|
|
|
|
|
protected string $clientSecret;
|
|
|
|
|
protected string $baseUrl;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->client = new Client(['timeout' => 10]);
|
|
|
|
|
$this->clientId = config('services.igdb.client_id');
|
|
|
|
|
$this->clientSecret = config('services.igdb.client_secret');
|
|
|
|
|
$this->baseUrl = config('services.igdb.base_url', 'https://api.igdb.com/v4');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get or refresh the OAuth access token (cached for 55 days).
|
|
|
|
|
*/
|
|
|
|
|
protected function getAccessToken(): string
|
|
|
|
|
{
|
|
|
|
|
return Cache::remember('igdb_access_token', 60 * 60 * 24 * 55, function () {
|
|
|
|
|
$response = $this->client->post('https://id.twitch.tv/oauth2/token', [
|
|
|
|
|
'form_params' => [
|
|
|
|
|
'client_id' => $this->clientId,
|
|
|
|
|
'client_secret' => $this->clientSecret,
|
|
|
|
|
'grant_type' => 'client_credentials',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
$data = json_decode($response->getBody(), true);
|
|
|
|
|
return $data['access_token'];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make an authenticated POST request to the IGDB API.
|
|
|
|
|
*/
|
|
|
|
|
protected function query(string $endpoint, string $body): array
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$response = $this->client->post("{$this->baseUrl}/{$endpoint}", [
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Client-ID' => $this->clientId,
|
|
|
|
|
'Authorization' => 'Bearer ' . $this->getAccessToken(),
|
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'body' => $body,
|
|
|
|
|
]);
|
|
|
|
|
return json_decode($response->getBody(), true) ?? [];
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error("IGDB API error [{$endpoint}]: " . $e->getMessage());
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search games by title.
|
|
|
|
|
*/
|
|
|
|
|
public function searchGames(string $query, int $limit = 10): array
|
|
|
|
|
{
|
|
|
|
|
return $this->query('games', "
|
|
|
|
|
search \"{$query}\";
|
|
|
|
|
fields id,name,slug,summary,cover.image_id,first_release_date,
|
|
|
|
|
genres.name,genres.slug,platforms.name,platforms.abbreviation,
|
|
|
|
|
involved_companies.company.name,involved_companies.developer,
|
|
|
|
|
rating,rating_count;
|
|
|
|
|
limit {$limit};
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a single game by IGDB ID with full details.
|
|
|
|
|
*/
|
|
|
|
|
public function getGame(int $igdbId): ?array
|
|
|
|
|
{
|
|
|
|
|
$results = $this->query('games', "
|
|
|
|
|
fields id,name,slug,summary,cover.image_id,first_release_date,
|
|
|
|
|
genres.id,genres.name,genres.slug,
|
|
|
|
|
platforms.id,platforms.name,platforms.slug,platforms.abbreviation,
|
|
|
|
|
involved_companies.company.id,involved_companies.company.name,
|
|
|
|
|
involved_companies.company.slug,involved_companies.developer,
|
|
|
|
|
involved_companies.publisher,
|
|
|
|
|
screenshots.image_id,
|
|
|
|
|
rating,rating_count;
|
|
|
|
|
where id = {$igdbId};
|
|
|
|
|
limit 1;
|
|
|
|
|
");
|
|
|
|
|
return $results[0] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get popular games (by rating and rating count).
|
|
|
|
|
*/
|
|
|
|
|
public function getPopularGames(int $limit = 20, int $offset = 0): array
|
|
|
|
|
{
|
|
|
|
|
return $this->query('games', "
|
|
|
|
|
fields id,name,slug,summary,cover.image_id,first_release_date,
|
2026-05-29 09:37:07 +03:00
|
|
|
genres.id,genres.name,genres.slug,
|
|
|
|
|
platforms.id,platforms.name,platforms.slug,platforms.abbreviation,
|
|
|
|
|
involved_companies.company.id,involved_companies.company.name,
|
|
|
|
|
involved_companies.company.slug,involved_companies.developer,
|
|
|
|
|
involved_companies.publisher,
|
|
|
|
|
screenshots.image_id,
|
2026-05-29 00:33:00 +03:00
|
|
|
rating,rating_count;
|
|
|
|
|
where rating != null & rating_count > 100 & cover != null;
|
|
|
|
|
sort rating_count desc;
|
|
|
|
|
limit {$limit};
|
|
|
|
|
offset {$offset};
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get recently released games.
|
|
|
|
|
*/
|
|
|
|
|
public function getNewReleases(int $limit = 10): array
|
|
|
|
|
{
|
|
|
|
|
$timestamp = now()->subMonths(3)->timestamp;
|
|
|
|
|
$now = now()->timestamp;
|
|
|
|
|
|
|
|
|
|
return $this->query('games', "
|
|
|
|
|
fields id,name,slug,summary,cover.image_id,first_release_date,
|
2026-05-29 09:37:07 +03:00
|
|
|
genres.id,genres.name,genres.slug,
|
|
|
|
|
platforms.id,platforms.name,platforms.slug,platforms.abbreviation,
|
|
|
|
|
involved_companies.company.id,involved_companies.company.name,
|
|
|
|
|
involved_companies.company.slug,involved_companies.developer,
|
|
|
|
|
involved_companies.publisher,
|
|
|
|
|
screenshots.image_id,
|
2026-05-29 00:33:00 +03:00
|
|
|
rating,rating_count;
|
|
|
|
|
where first_release_date >= {$timestamp} & first_release_date <= {$now} & cover != null;
|
|
|
|
|
sort first_release_date desc;
|
|
|
|
|
limit {$limit};
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 18:56:17 +03:00
|
|
|
/**
|
|
|
|
|
* Get games released in a given year.
|
|
|
|
|
*/
|
|
|
|
|
public function getGamesByYear(int $year, int $limit = 20, int $offset = 0): array
|
|
|
|
|
{
|
|
|
|
|
$from = mktime(0, 0, 0, 1, 1, $year);
|
|
|
|
|
$to = mktime(23, 59, 59, 12, 31, $year);
|
|
|
|
|
|
|
|
|
|
return $this->query('games', "
|
|
|
|
|
fields id,name,slug,summary,cover.image_id,first_release_date,
|
|
|
|
|
genres.id,genres.name,genres.slug,
|
|
|
|
|
platforms.id,platforms.name,platforms.slug,platforms.abbreviation,
|
|
|
|
|
involved_companies.company.id,involved_companies.company.name,
|
|
|
|
|
involved_companies.company.slug,involved_companies.developer,
|
|
|
|
|
involved_companies.publisher,
|
|
|
|
|
screenshots.image_id,
|
|
|
|
|
rating,rating_count;
|
|
|
|
|
where first_release_date >= {$from} & first_release_date <= {$to} & cover != null;
|
|
|
|
|
sort rating_count desc;
|
|
|
|
|
limit {$limit};
|
|
|
|
|
offset {$offset};
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get games for a given platform IGDB ID.
|
|
|
|
|
*/
|
|
|
|
|
public function getGamesByPlatform(int $platformIgdbId, int $limit = 20, int $offset = 0): array
|
|
|
|
|
{
|
|
|
|
|
return $this->query('games', "
|
|
|
|
|
fields id,name,slug,summary,cover.image_id,first_release_date,
|
|
|
|
|
genres.id,genres.name,genres.slug,
|
|
|
|
|
platforms.id,platforms.name,platforms.slug,platforms.abbreviation,
|
|
|
|
|
involved_companies.company.id,involved_companies.company.name,
|
|
|
|
|
involved_companies.company.slug,involved_companies.developer,
|
|
|
|
|
involved_companies.publisher,
|
|
|
|
|
screenshots.image_id,
|
|
|
|
|
rating,rating_count;
|
|
|
|
|
where platforms = [{$platformIgdbId}] & cover != null & rating != null;
|
|
|
|
|
sort rating_count desc;
|
|
|
|
|
limit {$limit};
|
|
|
|
|
offset {$offset};
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 00:33:00 +03:00
|
|
|
/**
|
|
|
|
|
* Get games by genre.
|
|
|
|
|
*/
|
|
|
|
|
public function getGamesByGenre(int $genreIgdbId, int $limit = 20, int $offset = 0): array
|
|
|
|
|
{
|
|
|
|
|
return $this->query('games', "
|
|
|
|
|
fields id,name,slug,summary,cover.image_id,first_release_date,
|
|
|
|
|
genres.name,genres.slug,platforms.abbreviation,
|
|
|
|
|
rating,rating_count;
|
|
|
|
|
where genres = [{$genreIgdbId}] & cover != null & rating != null;
|
|
|
|
|
sort rating desc;
|
|
|
|
|
limit {$limit};
|
|
|
|
|
offset {$offset};
|
|
|
|
|
");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build a full cover image URL from an IGDB image_id.
|
|
|
|
|
*/
|
|
|
|
|
public static function coverUrl(string $imageId, string $size = 'cover_big'): string
|
|
|
|
|
{
|
|
|
|
|
return "https://images.igdb.com/igdb/image/upload/t_{$size}/{$imageId}.jpg";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build a screenshot URL from an IGDB image_id.
|
|
|
|
|
*/
|
|
|
|
|
public static function screenshotUrl(string $imageId, string $size = 'screenshot_med'): string
|
|
|
|
|
{
|
|
|
|
|
return "https://images.igdb.com/igdb/image/upload/t_{$size}/{$imageId}.jpg";
|
|
|
|
|
}
|
|
|
|
|
}
|