Skip to content

Commit c91467a

Browse files
authored
Merge pull request #40 from simivar/feature/all-tournament-endpoints
Implement all Tournament v4 and Tournament Stub v4 endpoints
2 parents d9dc03e + 745b289 commit c91467a

22 files changed

+1179
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ composer require simivar/riot-php symfony/http-client nyholm/psr7
3434
| Tft Match v1 | [docs](https://developer.riotgames.com/apis#tft-match-v1) | - |
3535
| Tft Summoner v1 | [docs](https://developer.riotgames.com/apis#tft-summoner-v1) | - |
3636
| Third Party Code v4 | [docs](https://developer.riotgames.com/apis#third-party-code-v4) | 100% |
37-
| Tournament Stub v4 | [docs](https://developer.riotgames.com/apis#tournament-stub-v4) | - |
38-
| Tournament v4 | [docs](https://developer.riotgames.com/apis#tournament-v4) | - |
37+
| Tournament Stub v4 | [docs](https://developer.riotgames.com/apis#tournament-stub-v4) | 100% |
38+
| Tournament v4 | [docs](https://developer.riotgames.com/apis#tournament-v4) | 100% |
3939

4040
# Legal notice
4141
Riot PHP isn't endorsed by Riot Games and doesn't reflect the views or opinions of Riot Games or anyone officially

src/Riot/API.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,22 @@ public function getClasV1(): Version1\Clash
127127

128128
return $this->apis['clashV1'];
129129
}
130+
131+
public function getTournamentStubV4(): Version4\TournamentStub
132+
{
133+
if (!isset($this->apis['tournamentStubV4'])) {
134+
$this->apis['tournamentStubV4'] = new Version4\TournamentStub($this->riotConnection);
135+
}
136+
137+
return $this->apis['tournamentStubV4'];
138+
}
139+
140+
public function getTournamentV4(): Version4\Tournament
141+
{
142+
if (!isset($this->apis['tournamentV4'])) {
143+
$this->apis['tournamentV4'] = new Version4\Tournament($this->riotConnection);
144+
}
145+
146+
return $this->apis['tournamentV4'];
147+
}
130148
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Riot\API\Version4;
6+
7+
use Riot\API\AbstractApi;
8+
use Riot\DTO\LobbyEventDTOWrapperDTO;
9+
use Riot\DTO\TournamentCodeDTO;
10+
use Riot\Enum\GeoRegionEnum;
11+
use Riot\Enum\MapTypeEnum;
12+
use Riot\Enum\PickTypeEnum;
13+
use Riot\Enum\SpectatorTypeEnum;
14+
use Riot\Enum\TournamentRegionEnum;
15+
use Webmozart\Assert\Assert;
16+
17+
final class Tournament extends AbstractApi
18+
{
19+
/**
20+
* @param array<string> $allowedSummonerIds
21+
*
22+
* @return array<string>
23+
*/
24+
public function createCode(
25+
int $tournamentId,
26+
int $count,
27+
array $allowedSummonerIds,
28+
int $teamSize,
29+
PickTypeEnum $pickType,
30+
MapTypeEnum $mapType,
31+
SpectatorTypeEnum $spectatorType,
32+
?string $metadata
33+
): array {
34+
Assert::isList($allowedSummonerIds);
35+
Assert::allString($allowedSummonerIds);
36+
Assert::range($teamSize, 1, 5);
37+
38+
$response = $this->riotConnection->post(
39+
GeoRegionEnum::AMERICAS()->getValue(),
40+
sprintf('lol/tournament/v4/codes?count=%d&tournamentId=%d', $count, $tournamentId),
41+
[
42+
'allowedSummonerIds' => $allowedSummonerIds,
43+
'metadata' => $metadata,
44+
'teamSize' => $teamSize,
45+
'pickType' => $pickType->getValue(),
46+
'mapType' => $mapType->getValue(),
47+
'spectatorType' => $spectatorType->getValue(),
48+
],
49+
);
50+
51+
return $response->getBodyContentsDecodedAsArray();
52+
}
53+
54+
/**
55+
* @param array<string> $allowedSummonerIds
56+
*/
57+
public function updateCode(
58+
string $tournamentCode,
59+
array $allowedSummonerIds,
60+
PickTypeEnum $pickType,
61+
MapTypeEnum $mapType,
62+
SpectatorTypeEnum $spectatorType
63+
): bool {
64+
Assert::isList($allowedSummonerIds);
65+
Assert::allString($allowedSummonerIds);
66+
67+
$this->riotConnection->put(
68+
GeoRegionEnum::AMERICAS()->getValue(),
69+
sprintf('lol/tournament/v4/codes/%s', $tournamentCode),
70+
[
71+
'allowedSummonerIds' => $allowedSummonerIds,
72+
'pickType' => $pickType->getValue(),
73+
'mapType' => $mapType->getValue(),
74+
'spectatorType' => $spectatorType->getValue(),
75+
],
76+
);
77+
78+
return true;
79+
}
80+
81+
public function getCodeByTournamentCode(string $tournamentCode): TournamentCodeDTO
82+
{
83+
$response = $this->riotConnection->get(
84+
GeoRegionEnum::AMERICAS()->getValue(),
85+
sprintf('lol/tournament/v4/codes/%s', $tournamentCode),
86+
);
87+
88+
return TournamentCodeDTO::createFromArray($response->getBodyContentsDecodedAsArray());
89+
}
90+
91+
public function getLobbyEventsByTournamentCode(string $tournamentCode): LobbyEventDTOWrapperDTO
92+
{
93+
$response = $this->riotConnection->get(
94+
GeoRegionEnum::AMERICAS()->getValue(),
95+
sprintf('lol/tournament/v4/lobby-events/by-code/%s', $tournamentCode),
96+
);
97+
98+
return LobbyEventDTOWrapperDTO::createFromArray($response->getBodyContentsDecodedAsArray());
99+
}
100+
101+
public function createProvider(TournamentRegionEnum $region, string $url): int
102+
{
103+
$response = $this->riotConnection->post(
104+
GeoRegionEnum::AMERICAS()->getValue(),
105+
'lol/tournament/v4/providers',
106+
[
107+
'region' => $region->getValue(),
108+
'url' => $url,
109+
],
110+
);
111+
112+
return $response->getBodyContentsDecodedAsInt();
113+
}
114+
115+
public function createTournament(int $providerId, string $name): int
116+
{
117+
$response = $this->riotConnection->post(
118+
GeoRegionEnum::AMERICAS()->getValue(),
119+
'lol/tournament/v4/tournaments',
120+
[
121+
'providerId' => $providerId,
122+
'name' => $name,
123+
],
124+
);
125+
126+
return $response->getBodyContentsDecodedAsInt();
127+
}
128+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Riot\API\Version4;
6+
7+
use Riot\API\AbstractApi;
8+
use Riot\DTO\LobbyEventDTOWrapperDTO;
9+
use Riot\Enum\GeoRegionEnum;
10+
use Riot\Enum\MapTypeEnum;
11+
use Riot\Enum\PickTypeEnum;
12+
use Riot\Enum\SpectatorTypeEnum;
13+
use Riot\Enum\TournamentRegionEnum;
14+
use Webmozart\Assert\Assert;
15+
16+
final class TournamentStub extends AbstractApi
17+
{
18+
/**
19+
* @param array<string> $allowedSummonerIds
20+
*
21+
* @return array<string>
22+
*/
23+
public function createCode(
24+
int $tournamentId,
25+
int $count,
26+
array $allowedSummonerIds,
27+
int $teamSize,
28+
PickTypeEnum $pickType,
29+
MapTypeEnum $mapType,
30+
SpectatorTypeEnum $spectatorType,
31+
?string $metadata
32+
): array {
33+
Assert::isList($allowedSummonerIds);
34+
Assert::allString($allowedSummonerIds);
35+
Assert::range($teamSize, 1, 5);
36+
37+
$response = $this->riotConnection->post(
38+
GeoRegionEnum::AMERICAS()->getValue(),
39+
sprintf('lol/tournament-stub/v4/codes?count=%d&tournamentId=%d', $count, $tournamentId),
40+
[
41+
'allowedSummonerIds' => $allowedSummonerIds,
42+
'metadata' => $metadata,
43+
'teamSize' => $teamSize,
44+
'pickType' => $pickType->getValue(),
45+
'mapType' => $mapType->getValue(),
46+
'spectatorType' => $spectatorType->getValue(),
47+
],
48+
);
49+
50+
return $response->getBodyContentsDecodedAsArray();
51+
}
52+
53+
public function getLobbyEventsByTournamentCode(string $tournamentCode): LobbyEventDTOWrapperDTO
54+
{
55+
$response = $this->riotConnection->get(
56+
GeoRegionEnum::AMERICAS()->getValue(),
57+
sprintf('lol/tournament-stub/v4/lobby-events/by-code/%s', $tournamentCode),
58+
);
59+
60+
return LobbyEventDTOWrapperDTO::createFromArray($response->getBodyContentsDecodedAsArray());
61+
}
62+
63+
public function createProvider(TournamentRegionEnum $region, string $url): int
64+
{
65+
$response = $this->riotConnection->post(
66+
GeoRegionEnum::AMERICAS()->getValue(),
67+
'lol/tournament-stub/v4/providers',
68+
[
69+
'region' => $region->getValue(),
70+
'url' => $url,
71+
],
72+
);
73+
74+
return $response->getBodyContentsDecodedAsInt();
75+
}
76+
77+
public function createTournament(int $providerId, string $name): int
78+
{
79+
$response = $this->riotConnection->post(
80+
GeoRegionEnum::AMERICAS()->getValue(),
81+
'lol/tournament-stub/v4/tournaments',
82+
[
83+
'providerId' => $providerId,
84+
'name' => $name,
85+
],
86+
);
87+
88+
return $response->getBodyContentsDecodedAsInt();
89+
}
90+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Riot\Collection;
6+
7+
use Ramsey\Collection\AbstractCollection;
8+
use Riot\DTO\LobbyEventDTO;
9+
10+
final class LobbyEventDTOCollection extends AbstractCollection
11+
{
12+
/**
13+
* @codeCoverageIgnore
14+
*/
15+
public function getType(): string
16+
{
17+
return LobbyEventDTO::class;
18+
}
19+
20+
/**
21+
* @param array<array<string, string>> $data
22+
*
23+
* @return self<LobbyEventDTO>
24+
*/
25+
public static function createFromArray(array $data): self
26+
{
27+
$collection = new self();
28+
foreach ($data as $item) {
29+
$collection->add(LobbyEventDTO::createFromArray($item));
30+
}
31+
32+
return $collection;
33+
}
34+
}

src/Riot/Connection.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Http\Client\ClientInterface;
88
use Psr\Http\Message\RequestFactoryInterface;
99
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\StreamFactoryInterface;
1011
use Riot\API\ResponseDecoder;
1112
use Riot\API\ResponseDecoderInterface;
1213
use Riot\Exception\BadGatewayException;
@@ -31,13 +32,23 @@ final class Connection implements ConnectionInterface
3132

3233
private RequestFactoryInterface $requestFactory;
3334

34-
public function __construct(ClientInterface $riotClient, string $riotApiToken, RequestFactoryInterface $requestFactory)
35-
{
35+
private StreamFactoryInterface $streamFactory;
36+
37+
public function __construct(
38+
ClientInterface $riotClient,
39+
string $riotApiToken,
40+
RequestFactoryInterface $requestFactory,
41+
StreamFactoryInterface $streamFactory
42+
) {
3643
$this->client = $riotClient;
3744
$this->riotApiToken = $riotApiToken;
3845
$this->requestFactory = $requestFactory;
46+
$this->streamFactory = $streamFactory;
3947
}
4048

49+
/**
50+
* {@inheritdoc}
51+
*/
4152
public function get(string $region, string $path): ResponseDecoderInterface
4253
{
4354
$request = $this->requestFactory->createRequest(
@@ -54,6 +65,49 @@ public function get(string $region, string $path): ResponseDecoderInterface
5465
return new ResponseDecoder($response);
5566
}
5667

68+
public function post(string $region, string $path, array $data): ResponseDecoderInterface
69+
{
70+
return $this->sendRequestWithData(
71+
'POST',
72+
$region,
73+
$path,
74+
$data,
75+
);
76+
}
77+
78+
public function put(string $region, string $path, array $data): ResponseDecoderInterface
79+
{
80+
return $this->sendRequestWithData(
81+
'PUT',
82+
$region,
83+
$path,
84+
$data,
85+
);
86+
}
87+
88+
/**
89+
* @param array<mixed> $data
90+
*/
91+
private function sendRequestWithData(string $method, string $region, string $path, array $data): ResponseDecoderInterface
92+
{
93+
$request = $this->requestFactory->createRequest(
94+
$method,
95+
sprintf('https://%s.%s/%s', $region, self::API_URL, $path),
96+
);
97+
$request = $request->withAddedHeader('X-Riot-Token', $this->riotApiToken);
98+
$request = $request->withBody($this->streamFactory->createStream(json_encode(
99+
$data,
100+
JSON_THROW_ON_ERROR,
101+
)));
102+
103+
$response = $this->client->sendRequest($request);
104+
if (self::STATUS_CODE_OK !== $response->getStatusCode()) {
105+
$this->statusCodeToException($response);
106+
}
107+
108+
return new ResponseDecoder($response);
109+
}
110+
57111
/**
58112
* @throws BadGatewayException
59113
* @throws BadRequestException

0 commit comments

Comments
 (0)