IgdbClient::getAllPlatformLogos()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\IgdbBundle\Client;
6
7
use GuzzleHttp\Client;
8
use KrisKuiper\IGDBV4\Authentication\Authentication;
9
use KrisKuiper\IGDBV4\IGDB;
10
use KrisKuiper\IGDBV4\Authentication\ValueObjects\AccessConfig;
11
use KrisKuiper\IGDBV4\Authentication\ValueObjects\AuthConfig;
12
use VideoGamesRecords\IgdbBundle\Endpoint\PlatformTypeEndpoint;
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\IgdbBu...nt\PlatformTypeEndpoint was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class IgdbClient
15
{
16
    private IGDB $api;
17
    private Client $client;
18
    private AccessConfig $accessConfig;
19
20
    public function __construct(
21
        private readonly string $clientId,
22
        private readonly string $clientSecret
23
    ) {
24
        $this->client = new Client();
25
        $authConfig = new AuthConfig($this->clientId, $this->clientSecret);
26
        $authentication = new Authentication($this->client, $authConfig);
27
        $token = $authentication->obtainToken();
28
29
        $this->accessConfig = new AccessConfig($this->clientId, $token->getAccessToken());
30
        $this->api = new IGDB($this->client, $this->accessConfig);
31
    }
32
33
    public function getAllGenres(): array
34
    {
35
        $genres = [];
36
        $collection = $this->api->genre()->list(0, 500, ['id', 'name', 'slug', 'url', 'created_at', 'updated_at']);
37
38
        foreach ($collection as $genre) {
39
            $genres[] = (array) $genre;
40
        }
41
42
        return $genres;
43
    }
44
45
    public function getAllPlatformTypes(): array
46
    {
47
        $platformTypes = [];
48
        $platformTypeEndpoint = new PlatformTypeEndpoint($this->client, $this->accessConfig);
49
        $collection = $platformTypeEndpoint->list(
50
            0,
51
            500,
52
            ['id', 'name', 'checksum', 'created_at', 'updated_at']
53
        );
54
55
        foreach ($collection as $platformType) {
56
            $platformTypes[] = (array) $platformType;
57
        }
58
59
        return $platformTypes;
60
    }
61
62
    public function getAllPlatformLogos(): array
63
    {
64
        $platformLogos = [];
65
        $collection = $this->api->platformLogo()->list(
66
            0,
67
            500,
68
            ['id', 'image_id', 'url', 'width', 'height', 'checksum', 'alpha_channel', 'animated']
69
        );
70
71
        foreach ($collection as $platformLogo) {
72
            $platformLogos[] = (array) $platformLogo;
73
        }
74
75
        return $platformLogos;
76
    }
77
78
    public function getAllPlatforms(): array
79
    {
80
        $platforms = [];
81
        $collection = $this->api->platform()->list(
82
            0,
83
            500,
84
            [
85
                'id', 'name', 'abbreviation', 'alternative_name', 'generation',
86
                'slug', 'summary', 'url', 'checksum', 'platform_logo', 'platform_type', 'created_at', 'updated_at'
87
            ]
88
        );
89
90
        foreach ($collection as $platform) {
91
            $platforms[] = (array) $platform;
92
        }
93
94
        return $platforms;
95
    }
96
97
    public function getAllGames(): array
98
    {
99
        $games = [];
100
        $collection = $this->api->game()->list(
101
            0,
102
            500,
103
            [
104
                'id', 'name', 'slug', 'storyline', 'summary', 'url', 'checksum',
105
                'first_release_date', 'version_parent', 'genres', 'platforms', 'created_at', 'updated_at'
106
            ]
107
        );
108
109
        foreach ($collection as $game) {
110
            $games[] = (array) $game;
111
        }
112
113
        return $games;
114
    }
115
116
    public function searchGamesByName(string $gameName, ?array $platformIds = null, int $limit = 50): array
117
    {
118
        $games = [];
119
120
        // Build query
121
        $fields = [
122
            'id', 'name', 'slug', 'storyline', 'summary', 'url', 'checksum',
123
            'first_release_date', 'version_parent', 'genres', 'platforms.*', 'created_at', 'updated_at'
124
        ];
125
126
        if (!empty($platformIds)) {
127
            // Use custom query with platform filtering
128
            $platformIdsStr = implode(',', $platformIds);
129
            $query = sprintf(
130
                'fields %s; where platforms = [%s]; search "%s"; limit %d;',
131
                implode(',', $fields),
132
                $platformIdsStr,
133
                addslashes($gameName),
134
                $limit
135
            );
136
            $collection = $this->api->game()->query($query);
137
        } else {
138
            // Simple search by name only
139
            $collection = $this->api->game()->search($gameName, $fields);
140
        }
141
142
        foreach ($collection as $game) {
143
            $games[] = (array) $game;
144
        }
145
146
        return $games;
147
    }
148
149
    public function findGameByNameAndPlatform(string $gameName, int $platformId): ?array
150
    {
151
        $results = $this->searchGamesByName($gameName, [$platformId], 1);
152
153
        // Return exact match if found
154
        foreach ($results as $game) {
155
            if (strcasecmp($game['name'], $gameName) === 0) {
156
                return $game;
157
            }
158
        }
159
160
        return null;
161
    }
162
}
163