Skip to content

Commit 86a13cb

Browse files
Automatically update PHP SDK
1 parent 4ed85fe commit 86a13cb

15 files changed

+1045
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "trophy/php",
3-
"version": "1.0.23",
3+
"version": "1.0.24",
44
"description": "Trophy PHP Library",
55
"keywords": [
66
"trophy",
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Trophy\Leaderboards\Requests;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
7+
class LeaderboardsGetRequest extends JsonSerializableType
8+
{
9+
/**
10+
* @var ?int $offset Number of rankings to skip for pagination.
11+
*/
12+
public ?int $offset;
13+
14+
/**
15+
* @var ?int $limit Maximum number of rankings to return.
16+
*/
17+
public ?int $limit;
18+
19+
/**
20+
* @var ?string $run Specific run date in YYYY-MM-DD format. If not provided, returns the current run.
21+
*/
22+
public ?string $run;
23+
24+
/**
25+
* @var ?string $userId When provided, offset is relative to this user's position on the leaderboard. If the user is not found in the leaderboard, returns empty rankings array.
26+
*/
27+
public ?string $userId;
28+
29+
/**
30+
* @param array{
31+
* offset?: ?int,
32+
* limit?: ?int,
33+
* run?: ?string,
34+
* userId?: ?string,
35+
* } $values
36+
*/
37+
public function __construct(
38+
array $values = [],
39+
) {
40+
$this->offset = $values['offset'] ?? null;
41+
$this->limit = $values['limit'] ?? null;
42+
$this->run = $values['run'] ?? null;
43+
$this->userId = $values['userId'] ?? null;
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Trophy\Leaderboards\Requests;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
7+
class UsersLeaderboardsRequest extends JsonSerializableType
8+
{
9+
/**
10+
* @var ?string $run Specific run date in YYYY-MM-DD format. If not provided, returns the current run.
11+
*/
12+
public ?string $run;
13+
14+
/**
15+
* @param array{
16+
* run?: ?string,
17+
* } $values
18+
*/
19+
public function __construct(
20+
array $values = [],
21+
) {
22+
$this->run = $values['run'] ?? null;
23+
}
24+
}

0 commit comments

Comments
 (0)