Initial GameLog setup
- Laravel 11 + Breeze (dark mode) + Tailwind + Alpine.js - Migrations: games, genres, platforms, companies, pivots, user_games, user_lists, reviews, follows, activities - Models: Game, Genre, Platform, Company, UserGame, UserList, Review, ReviewVote, Activity, User (with gamelog fields) - IgdbService: OAuth2 token management, search, game fetch, popular/new releases, genre filter, cover/screenshot URL helpers - IGDB credentials in config/services.php (from .env)
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
<?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,
|
||||
genres.name,genres.slug,platforms.abbreviation,
|
||||
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,
|
||||
genres.name,genres.slug,platforms.abbreviation,
|
||||
rating,rating_count;
|
||||
where first_release_date >= {$timestamp} & first_release_date <= {$now} & cover != null;
|
||||
sort first_release_date desc;
|
||||
limit {$limit};
|
||||
");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user