Skip to content

Commit 9f01a22

Browse files
Automatically update PHP SDK
1 parent 7cca3d0 commit 9f01a22

14 files changed

Lines changed: 430 additions & 29 deletions

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.9",
3+
"version": "1.0.10",
44
"description": "Trophy PHP Library",
55
"keywords": [
66
"trophy",

src/Achievements/AchievementsClient.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44

55
use GuzzleHttp\ClientInterface;
66
use Trophy\Core\Client\RawClient;
7-
use Trophy\Achievements\Requests\AchievementsCompleteRequest;
8-
use Trophy\Types\AchievementCompletionResponse;
7+
use Trophy\Types\AchievementWithStatsResponse;
98
use Trophy\Exceptions\TrophyException;
109
use Trophy\Exceptions\TrophyApiException;
1110
use Trophy\Core\Json\JsonApiRequest;
1211
use Trophy\Environments;
1312
use Trophy\Core\Client\HttpMethod;
13+
use Trophy\Core\Json\JsonDecoder;
1414
use JsonException;
1515
use GuzzleHttp\Exception\RequestException;
1616
use Psr\Http\Client\ClientExceptionInterface;
17+
use Trophy\Achievements\Requests\AchievementsCompleteRequest;
18+
use Trophy\Types\AchievementCompletionResponse;
1719

1820
class AchievementsClient
1921
{
@@ -49,6 +51,56 @@ public function __construct(
4951
$this->options = $options ?? [];
5052
}
5153

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+
52104
/**
53105
* Mark an achievement as completed for a user.
54106
*

src/Achievements/Requests/AchievementsCompleteRequest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace Trophy\Achievements\Requests;
44

55
use Trophy\Core\Json\JsonSerializableType;
6-
use Trophy\Types\UpsertedUser;
6+
use Trophy\Types\UpdatedUser;
77
use Trophy\Core\Json\JsonProperty;
88

99
class AchievementsCompleteRequest extends JsonSerializableType
1010
{
1111
/**
12-
* @var UpsertedUser $user The user that completed the achievement.
12+
* @var UpdatedUser $user The user that completed the achievement.
1313
*/
1414
#[JsonProperty('user')]
15-
public UpsertedUser $user;
15+
public UpdatedUser $user;
1616

1717
/**
1818
* @param array{
19-
* user: UpsertedUser,
19+
* user: UpdatedUser,
2020
* } $values
2121
*/
2222
public function __construct(

src/Traits/AchievementResponse.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Trophy\Traits;
4+
5+
use Trophy\Core\Json\JsonProperty;
6+
7+
trait AchievementResponse
8+
{
9+
/**
10+
* @var string $id The unique ID of the achievement.
11+
*/
12+
#[JsonProperty('id')]
13+
public string $id;
14+
15+
/**
16+
* @var string $name The name of this achievement.
17+
*/
18+
#[JsonProperty('name')]
19+
public string $name;
20+
21+
/**
22+
* @var string $trigger The trigger of the achievement, either 'metric', 'streak', or 'api'.
23+
*/
24+
#[JsonProperty('trigger')]
25+
public string $trigger;
26+
27+
/**
28+
* @var ?string $description The description of this achievement.
29+
*/
30+
#[JsonProperty('description')]
31+
public ?string $description;
32+
33+
/**
34+
* @var ?string $badgeUrl The URL of the badge image for the achievement, if one has been uploaded.
35+
*/
36+
#[JsonProperty('badgeUrl')]
37+
public ?string $badgeUrl;
38+
39+
/**
40+
* @var ?string $key The key used to reference this achievement in the API (only applicable if trigger = 'api')
41+
*/
42+
#[JsonProperty('key')]
43+
public ?string $key;
44+
45+
/**
46+
* @var ?int $streakLength The length of the streak required to complete the achievement (only applicable if trigger = 'streak')
47+
*/
48+
#[JsonProperty('streakLength')]
49+
public ?int $streakLength;
50+
51+
/**
52+
* @var ?string $metricId The ID of the metric associated with this achievement (only applicable if trigger = 'metric')
53+
*/
54+
#[JsonProperty('metricId')]
55+
public ?string $metricId;
56+
57+
/**
58+
* @var ?float $metricValue The value of the metric required to complete the achievement (only applicable if trigger = 'metric')
59+
*/
60+
#[JsonProperty('metricValue')]
61+
public ?float $metricValue;
62+
63+
/**
64+
* @var ?string $metricName The name of the metric associated with this achievement (only applicable if trigger = 'metric')
65+
*/
66+
#[JsonProperty('metricName')]
67+
public ?string $metricName;
68+
}

src/Types/AchievementCompletionResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class AchievementCompletionResponse extends JsonSerializableType
1414
public string $completionId;
1515

1616
/**
17-
* @var AchievementResponse $achievement
17+
* @var CompletedAchievementResponse $achievement
1818
*/
1919
#[JsonProperty('achievement')]
20-
public AchievementResponse $achievement;
20+
public CompletedAchievementResponse $achievement;
2121

2222
/**
2323
* @param array{
2424
* completionId: string,
25-
* achievement: AchievementResponse,
25+
* achievement: CompletedAchievementResponse,
2626
* } $values
2727
*/
2828
public function __construct(

src/Types/AchievementResponse.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Trophy\Core\Json\JsonSerializableType;
66
use Trophy\Core\Json\JsonProperty;
7-
use DateTime;
8-
use Trophy\Core\Types\Date;
97

108
class AchievementResponse extends JsonSerializableType
119
{
@@ -28,16 +26,16 @@ class AchievementResponse extends JsonSerializableType
2826
public string $trigger;
2927

3028
/**
31-
* @var ?string $badgeUrl The URL of the badge image for the achievement, if one has been uploaded.
29+
* @var ?string $description The description of this achievement.
3230
*/
33-
#[JsonProperty('badgeUrl')]
34-
public ?string $badgeUrl;
31+
#[JsonProperty('description')]
32+
public ?string $description;
3533

3634
/**
37-
* @var ?DateTime $achievedAt The date and time the achievement was completed, in ISO 8601 format.
35+
* @var ?string $badgeUrl The URL of the badge image for the achievement, if one has been uploaded.
3836
*/
39-
#[JsonProperty('achievedAt'), Date(Date::TYPE_DATETIME)]
40-
public ?DateTime $achievedAt;
37+
#[JsonProperty('badgeUrl')]
38+
public ?string $badgeUrl;
4139

4240
/**
4341
* @var ?string $key The key used to reference this achievement in the API (only applicable if trigger = 'api')
@@ -74,8 +72,8 @@ class AchievementResponse extends JsonSerializableType
7472
* id: string,
7573
* name: string,
7674
* trigger: string,
75+
* description?: ?string,
7776
* badgeUrl?: ?string,
78-
* achievedAt?: ?DateTime,
7977
* key?: ?string,
8078
* streakLength?: ?int,
8179
* metricId?: ?string,
@@ -89,8 +87,8 @@ public function __construct(
8987
$this->id = $values['id'];
9088
$this->name = $values['name'];
9189
$this->trigger = $values['trigger'];
90+
$this->description = $values['description'] ?? null;
9291
$this->badgeUrl = $values['badgeUrl'] ?? null;
93-
$this->achievedAt = $values['achievedAt'] ?? null;
9492
$this->key = $values['key'] ?? null;
9593
$this->streakLength = $values['streakLength'] ?? null;
9694
$this->metricId = $values['metricId'] ?? null;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Trophy\Types;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
use Trophy\Traits\AchievementResponse;
7+
use Trophy\Core\Json\JsonProperty;
8+
9+
class AchievementWithStatsResponse extends JsonSerializableType
10+
{
11+
use AchievementResponse;
12+
13+
/**
14+
* @var ?int $completions The number of users who have completed this achievement.
15+
*/
16+
#[JsonProperty('completions')]
17+
public ?int $completions;
18+
19+
/**
20+
* @var ?float $completedPercentage The percentage of all users who have completed this achievement.
21+
*/
22+
#[JsonProperty('completedPercentage')]
23+
public ?float $completedPercentage;
24+
25+
/**
26+
* @param array{
27+
* completions?: ?int,
28+
* completedPercentage?: ?float,
29+
* id: string,
30+
* name: string,
31+
* trigger: string,
32+
* description?: ?string,
33+
* badgeUrl?: ?string,
34+
* key?: ?string,
35+
* streakLength?: ?int,
36+
* metricId?: ?string,
37+
* metricValue?: ?float,
38+
* metricName?: ?string,
39+
* } $values
40+
*/
41+
public function __construct(
42+
array $values,
43+
) {
44+
$this->completions = $values['completions'] ?? null;
45+
$this->completedPercentage = $values['completedPercentage'] ?? null;
46+
$this->id = $values['id'];
47+
$this->name = $values['name'];
48+
$this->trigger = $values['trigger'];
49+
$this->description = $values['description'] ?? null;
50+
$this->badgeUrl = $values['badgeUrl'] ?? null;
51+
$this->key = $values['key'] ?? null;
52+
$this->streakLength = $values['streakLength'] ?? null;
53+
$this->metricId = $values['metricId'] ?? null;
54+
$this->metricValue = $values['metricValue'] ?? null;
55+
$this->metricName = $values['metricName'] ?? null;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function __toString(): string
62+
{
63+
return $this->toJson();
64+
}
65+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Trophy\Types;
4+
5+
use Trophy\Core\Json\JsonSerializableType;
6+
use Trophy\Traits\AchievementResponse;
7+
use DateTime;
8+
use Trophy\Core\Json\JsonProperty;
9+
use Trophy\Core\Types\Date;
10+
11+
class CompletedAchievementResponse extends JsonSerializableType
12+
{
13+
use AchievementResponse;
14+
15+
/**
16+
* @var ?DateTime $achievedAt The date and time the achievement was completed, in ISO 8601 format.
17+
*/
18+
#[JsonProperty('achievedAt'), Date(Date::TYPE_DATETIME)]
19+
public ?DateTime $achievedAt;
20+
21+
/**
22+
* @param array{
23+
* achievedAt?: ?DateTime,
24+
* id: string,
25+
* name: string,
26+
* trigger: string,
27+
* description?: ?string,
28+
* badgeUrl?: ?string,
29+
* key?: ?string,
30+
* streakLength?: ?int,
31+
* metricId?: ?string,
32+
* metricValue?: ?float,
33+
* metricName?: ?string,
34+
* } $values
35+
*/
36+
public function __construct(
37+
array $values,
38+
) {
39+
$this->achievedAt = $values['achievedAt'] ?? null;
40+
$this->id = $values['id'];
41+
$this->name = $values['name'];
42+
$this->trigger = $values['trigger'];
43+
$this->description = $values['description'] ?? null;
44+
$this->badgeUrl = $values['badgeUrl'] ?? null;
45+
$this->key = $values['key'] ?? null;
46+
$this->streakLength = $values['streakLength'] ?? null;
47+
$this->metricId = $values['metricId'] ?? null;
48+
$this->metricValue = $values['metricValue'] ?? null;
49+
$this->metricName = $values['metricName'] ?? null;
50+
}
51+
52+
/**
53+
* @return string
54+
*/
55+
public function __toString(): string
56+
{
57+
return $this->toJson();
58+
}
59+
}

src/Types/EventResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class EventResponse extends JsonSerializableType
2727
public float $total;
2828

2929
/**
30-
* @var ?array<AchievementResponse> $achievements Achievements completed as a result of this event.
30+
* @var ?array<CompletedAchievementResponse> $achievements Achievements completed as a result of this event.
3131
*/
32-
#[JsonProperty('achievements'), ArrayType([AchievementResponse::class])]
32+
#[JsonProperty('achievements'), ArrayType([CompletedAchievementResponse::class])]
3333
public ?array $achievements;
3434

3535
/**
@@ -43,7 +43,7 @@ class EventResponse extends JsonSerializableType
4343
* eventId: string,
4444
* metricId: string,
4545
* total: float,
46-
* achievements?: ?array<AchievementResponse>,
46+
* achievements?: ?array<CompletedAchievementResponse>,
4747
* currentStreak?: ?IncrementMetricStreakResponse,
4848
* } $values
4949
*/

0 commit comments

Comments
 (0)