|
4 | 4 |
|
5 | 5 | use GuzzleHttp\ClientInterface; |
6 | 6 | use Trophy\Core\Client\RawClient; |
7 | | -use Trophy\Achievements\Requests\AchievementsCompleteRequest; |
8 | | -use Trophy\Types\AchievementCompletionResponse; |
| 7 | +use Trophy\Types\AchievementWithStatsResponse; |
9 | 8 | use Trophy\Exceptions\TrophyException; |
10 | 9 | use Trophy\Exceptions\TrophyApiException; |
11 | 10 | use Trophy\Core\Json\JsonApiRequest; |
12 | 11 | use Trophy\Environments; |
13 | 12 | use Trophy\Core\Client\HttpMethod; |
| 13 | +use Trophy\Core\Json\JsonDecoder; |
14 | 14 | use JsonException; |
15 | 15 | use GuzzleHttp\Exception\RequestException; |
16 | 16 | use Psr\Http\Client\ClientExceptionInterface; |
| 17 | +use Trophy\Achievements\Requests\AchievementsCompleteRequest; |
| 18 | +use Trophy\Types\AchievementCompletionResponse; |
17 | 19 |
|
18 | 20 | class AchievementsClient |
19 | 21 | { |
@@ -49,6 +51,56 @@ public function __construct( |
49 | 51 | $this->options = $options ?? []; |
50 | 52 | } |
51 | 53 |
|
| 54 | + /** |
| 55 | + * Get all achievements and their completion stats. |
| 56 | + * |
| 57 | + * @param ?array{ |
| 58 | + * baseUrl?: string, |
| 59 | + * maxRetries?: int, |
| 60 | + * } $options |
| 61 | + * @return array<AchievementWithStatsResponse> |
| 62 | + * @throws TrophyException |
| 63 | + * @throws TrophyApiException |
| 64 | + */ |
| 65 | + public function all(?array $options = null): array |
| 66 | + { |
| 67 | + $options = array_merge($this->options, $options ?? []); |
| 68 | + try { |
| 69 | + $response = $this->client->sendRequest( |
| 70 | + new JsonApiRequest( |
| 71 | + baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Default_->value, |
| 72 | + path: "achievements", |
| 73 | + method: HttpMethod::GET, |
| 74 | + ), |
| 75 | + $options, |
| 76 | + ); |
| 77 | + $statusCode = $response->getStatusCode(); |
| 78 | + if ($statusCode >= 200 && $statusCode < 400) { |
| 79 | + $json = $response->getBody()->getContents(); |
| 80 | + return JsonDecoder::decodeArray($json, [AchievementWithStatsResponse::class]); // @phpstan-ignore-line |
| 81 | + } |
| 82 | + } catch (JsonException $e) { |
| 83 | + throw new TrophyException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 84 | + } catch (RequestException $e) { |
| 85 | + $response = $e->getResponse(); |
| 86 | + if ($response === null) { |
| 87 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 88 | + } |
| 89 | + throw new TrophyApiException( |
| 90 | + message: "API request failed", |
| 91 | + statusCode: $response->getStatusCode(), |
| 92 | + body: $response->getBody()->getContents(), |
| 93 | + ); |
| 94 | + } catch (ClientExceptionInterface $e) { |
| 95 | + throw new TrophyException(message: $e->getMessage(), previous: $e); |
| 96 | + } |
| 97 | + throw new TrophyApiException( |
| 98 | + message: 'API request failed', |
| 99 | + statusCode: $statusCode, |
| 100 | + body: $response->getBody()->getContents(), |
| 101 | + ); |
| 102 | + } |
| 103 | + |
52 | 104 | /** |
53 | 105 | * Mark an achievement as completed for a user. |
54 | 106 | * |
|
0 commit comments