|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Trophy\Leaderboards; |
| 4 | + |
| 5 | +use GuzzleHttp\ClientInterface; |
| 6 | +use Trophy\Core\Client\RawClient; |
| 7 | +use Trophy\Types\LeaderboardResponse; |
| 8 | +use Trophy\Exceptions\TrophyException; |
| 9 | +use Trophy\Exceptions\TrophyApiException; |
| 10 | +use Trophy\Core\Json\JsonApiRequest; |
| 11 | +use Trophy\Environments; |
| 12 | +use Trophy\Core\Client\HttpMethod; |
| 13 | +use Trophy\Core\Json\JsonDecoder; |
| 14 | +use JsonException; |
| 15 | +use GuzzleHttp\Exception\RequestException; |
| 16 | +use Psr\Http\Client\ClientExceptionInterface; |
| 17 | +use Trophy\Leaderboards\Requests\LeaderboardsGetRequest; |
| 18 | +use Trophy\Types\LeaderboardResponseWithRankings; |
| 19 | +use Trophy\Leaderboards\Requests\UsersLeaderboardsRequest; |
| 20 | +use Trophy\Types\UserLeaderboardResponse; |
| 21 | + |
| 22 | +class LeaderboardsClient |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var array{ |
| 26 | + * baseUrl?: string, |
| 27 | + * client?: ClientInterface, |
| 28 | + * headers?: array<string, string>, |
| 29 | + * maxRetries?: int, |
| 30 | + * } $options |
| 31 | + */ |
| 32 | + private array $options; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var RawClient $client |
| 36 | + */ |
| 37 | + private RawClient $client; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param RawClient $client |
| 41 | + * @param ?array{ |
| 42 | + * baseUrl?: string, |
| 43 | + * client?: ClientInterface, |
| 44 | + * headers?: array<string, string>, |
| 45 | + * maxRetries?: int, |
| 46 | + * } $options |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + RawClient $client, |
| 50 | + ?array $options = null, |
| 51 | + ) { |
| 52 | + $this->client = $client; |
| 53 | + $this->options = $options ?? []; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get all active leaderboards for your organization. |
| 58 | + * |
| 59 | + * @param ?array{ |
| 60 | + * baseUrl?: string, |
| 61 | + * maxRetries?: int, |
| 62 | + * } $options |
| 63 | + * @return array<LeaderboardResponse> |
| 64 | + * @throws TrophyException |
| 65 | + * @throws TrophyApiException |
| 66 | + */ |
| 67 | + public function all(?array $options = null): array |
| 68 | + { |
| 69 | + $options = array_merge($this->options, $options ?? []); |
| 70 | + try { |
| 71 | + $response = $this->client->sendRequest( |
| 72 | + new JsonApiRequest( |
| 73 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Default_->value, |
| 74 | + path: "leaderboards", |
| 75 | + method: HttpMethod::GET, |
| 76 | + ), |
| 77 | + $options, |
| 78 | + ); |
| 79 | + $statusCode = $response->getStatusCode(); |
| 80 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 81 | + $json = $response->getBody()->getContents(); |
| 82 | + return JsonDecoder::decodeArray($json, [LeaderboardResponse::class]); // @phpstan-ignore-line |
| 83 | + } |
| 84 | + } catch (JsonException $e) { |
| 85 | + throw new TrophyException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 86 | + } catch (RequestException $e) { |
| 87 | + $response = $e->getResponse(); |
| 88 | + if ($response === null) { |
| 89 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 90 | + } |
| 91 | + throw new TrophyApiException( |
| 92 | + message: "API request failed", |
| 93 | + statusCode: $response->getStatusCode(), |
| 94 | + body: $response->getBody()->getContents(), |
| 95 | + ); |
| 96 | + } catch (ClientExceptionInterface $e) { |
| 97 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 98 | + } |
| 99 | + throw new TrophyApiException( |
| 100 | + message: 'API request failed', |
| 101 | + statusCode: $statusCode, |
| 102 | + body: $response->getBody()->getContents(), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get a specific leaderboard by its key. |
| 108 | + * |
| 109 | + * @param string $key Unique key of the leaderboard as set when created. |
| 110 | + * @param LeaderboardsGetRequest $request |
| 111 | + * @param ?array{ |
| 112 | + * baseUrl?: string, |
| 113 | + * maxRetries?: int, |
| 114 | + * } $options |
| 115 | + * @return LeaderboardResponseWithRankings |
| 116 | + * @throws TrophyException |
| 117 | + * @throws TrophyApiException |
| 118 | + */ |
| 119 | + public function get(string $key, LeaderboardsGetRequest $request, ?array $options = null): LeaderboardResponseWithRankings |
| 120 | + { |
| 121 | + $options = array_merge($this->options, $options ?? []); |
| 122 | + $query = []; |
| 123 | + if ($request->offset != null) { |
| 124 | + $query['offset'] = $request->offset; |
| 125 | + } |
| 126 | + if ($request->limit != null) { |
| 127 | + $query['limit'] = $request->limit; |
| 128 | + } |
| 129 | + if ($request->run != null) { |
| 130 | + $query['run'] = $request->run; |
| 131 | + } |
| 132 | + if ($request->userId != null) { |
| 133 | + $query['userId'] = $request->userId; |
| 134 | + } |
| 135 | + try { |
| 136 | + $response = $this->client->sendRequest( |
| 137 | + new JsonApiRequest( |
| 138 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Default_->value, |
| 139 | + path: "leaderboards/$key", |
| 140 | + method: HttpMethod::GET, |
| 141 | + query: $query, |
| 142 | + ), |
| 143 | + $options, |
| 144 | + ); |
| 145 | + $statusCode = $response->getStatusCode(); |
| 146 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 147 | + $json = $response->getBody()->getContents(); |
| 148 | + return LeaderboardResponseWithRankings::fromJson($json); |
| 149 | + } |
| 150 | + } catch (JsonException $e) { |
| 151 | + throw new TrophyException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 152 | + } catch (RequestException $e) { |
| 153 | + $response = $e->getResponse(); |
| 154 | + if ($response === null) { |
| 155 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 156 | + } |
| 157 | + throw new TrophyApiException( |
| 158 | + message: "API request failed", |
| 159 | + statusCode: $response->getStatusCode(), |
| 160 | + body: $response->getBody()->getContents(), |
| 161 | + ); |
| 162 | + } catch (ClientExceptionInterface $e) { |
| 163 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 164 | + } |
| 165 | + throw new TrophyApiException( |
| 166 | + message: 'API request failed', |
| 167 | + statusCode: $statusCode, |
| 168 | + body: $response->getBody()->getContents(), |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get a user's rank, value, and history for a specific leaderboard. |
| 174 | + * |
| 175 | + * @param string $userId The user's ID in your database. |
| 176 | + * @param string $key Unique key of the leaderboard as set when created. |
| 177 | + * @param UsersLeaderboardsRequest $request |
| 178 | + * @param ?array{ |
| 179 | + * baseUrl?: string, |
| 180 | + * maxRetries?: int, |
| 181 | + * } $options |
| 182 | + * @return UserLeaderboardResponse |
| 183 | + * @throws TrophyException |
| 184 | + * @throws TrophyApiException |
| 185 | + */ |
| 186 | + public function usersLeaderboards(string $userId, string $key, UsersLeaderboardsRequest $request, ?array $options = null): UserLeaderboardResponse |
| 187 | + { |
| 188 | + $options = array_merge($this->options, $options ?? []); |
| 189 | + $query = []; |
| 190 | + if ($request->run != null) { |
| 191 | + $query['run'] = $request->run; |
| 192 | + } |
| 193 | + try { |
| 194 | + $response = $this->client->sendRequest( |
| 195 | + new JsonApiRequest( |
| 196 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Default_->value, |
| 197 | + path: "users/$userId/leaderboards/$key", |
| 198 | + method: HttpMethod::GET, |
| 199 | + query: $query, |
| 200 | + ), |
| 201 | + $options, |
| 202 | + ); |
| 203 | + $statusCode = $response->getStatusCode(); |
| 204 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 205 | + $json = $response->getBody()->getContents(); |
| 206 | + return UserLeaderboardResponse::fromJson($json); |
| 207 | + } |
| 208 | + } catch (JsonException $e) { |
| 209 | + throw new TrophyException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 210 | + } catch (RequestException $e) { |
| 211 | + $response = $e->getResponse(); |
| 212 | + if ($response === null) { |
| 213 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 214 | + } |
| 215 | + throw new TrophyApiException( |
| 216 | + message: "API request failed", |
| 217 | + statusCode: $response->getStatusCode(), |
| 218 | + body: $response->getBody()->getContents(), |
| 219 | + ); |
| 220 | + } catch (ClientExceptionInterface $e) { |
| 221 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 222 | + } |
| 223 | + throw new TrophyApiException( |
| 224 | + message: 'API request failed', |
| 225 | + statusCode: $statusCode, |
| 226 | + body: $response->getBody()->getContents(), |
| 227 | + ); |
| 228 | + } |
| 229 | +} |
0 commit comments