Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement all Tournament v4 and Tournament Stub v4 endpoints #40

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ composer require simivar/riot-php symfony/http-client nyholm/psr7
| Tft Match v1 | [docs](https://developer.riotgames.com/apis#tft-match-v1) | - |
| Tft Summoner v1 | [docs](https://developer.riotgames.com/apis#tft-summoner-v1) | - |
| Third Party Code v4 | [docs](https://developer.riotgames.com/apis#third-party-code-v4) | 100% |
| Tournament Stub v4 | [docs](https://developer.riotgames.com/apis#tournament-stub-v4) | - |
| Tournament v4 | [docs](https://developer.riotgames.com/apis#tournament-v4) | - |
| Tournament Stub v4 | [docs](https://developer.riotgames.com/apis#tournament-stub-v4) | 100% |
| Tournament v4 | [docs](https://developer.riotgames.com/apis#tournament-v4) | 100% |

# Legal notice
Riot PHP isn't endorsed by Riot Games and doesn't reflect the views or opinions of Riot Games or anyone officially
Expand Down
18 changes: 18 additions & 0 deletions src/Riot/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,22 @@ public function getClasV1(): Version1\Clash

return $this->apis['clashV1'];
}

public function getTournamentStubV4(): Version4\TournamentStub
{
if (!isset($this->apis['tournamentStubV4'])) {
$this->apis['tournamentStubV4'] = new Version4\TournamentStub($this->riotConnection);
}

return $this->apis['tournamentStubV4'];
}

public function getTournamentV4(): Version4\Tournament
{
if (!isset($this->apis['tournamentV4'])) {
$this->apis['tournamentV4'] = new Version4\Tournament($this->riotConnection);
}

return $this->apis['tournamentV4'];
}
}
128 changes: 128 additions & 0 deletions src/Riot/API/Version4/Tournament.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace Riot\API\Version4;

use Riot\API\AbstractApi;
use Riot\DTO\LobbyEventDTOWrapperDTO;
use Riot\DTO\TournamentCodeDTO;
use Riot\Enum\GeoRegionEnum;
use Riot\Enum\MapTypeEnum;
use Riot\Enum\PickTypeEnum;
use Riot\Enum\SpectatorTypeEnum;
use Riot\Enum\TournamentRegionEnum;
use Webmozart\Assert\Assert;

final class Tournament extends AbstractApi
{
/**
* @param array<string> $allowedSummonerIds
*
* @return array<string>
*/
public function createCode(
int $tournamentId,
int $count,
array $allowedSummonerIds,
int $teamSize,
PickTypeEnum $pickType,
MapTypeEnum $mapType,
SpectatorTypeEnum $spectatorType,
?string $metadata
): array {
Assert::isList($allowedSummonerIds);
Assert::allString($allowedSummonerIds);
Assert::range($teamSize, 1, 5);

$response = $this->riotConnection->post(
GeoRegionEnum::AMERICAS()->getValue(),
sprintf('lol/tournament/v4/codes?count=%d&tournamentId=%d', $count, $tournamentId),
[
'allowedSummonerIds' => $allowedSummonerIds,
'metadata' => $metadata,
'teamSize' => $teamSize,
'pickType' => $pickType->getValue(),
'mapType' => $mapType->getValue(),
'spectatorType' => $spectatorType->getValue(),
],
);

return $response->getBodyContentsDecodedAsArray();
}

/**
* @param array<string> $allowedSummonerIds
*/
public function updateCode(
string $tournamentCode,
array $allowedSummonerIds,
PickTypeEnum $pickType,
MapTypeEnum $mapType,
SpectatorTypeEnum $spectatorType
): bool {
Assert::isList($allowedSummonerIds);
Assert::allString($allowedSummonerIds);

$this->riotConnection->put(
GeoRegionEnum::AMERICAS()->getValue(),
sprintf('lol/tournament/v4/codes/%s', $tournamentCode),
[
'allowedSummonerIds' => $allowedSummonerIds,
'pickType' => $pickType->getValue(),
'mapType' => $mapType->getValue(),
'spectatorType' => $spectatorType->getValue(),
],
);

return true;
}

public function getCodeByTournamentCode(string $tournamentCode): TournamentCodeDTO
{
$response = $this->riotConnection->get(
GeoRegionEnum::AMERICAS()->getValue(),
sprintf('lol/tournament/v4/codes/%s', $tournamentCode),
);

return TournamentCodeDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}

public function getLobbyEventsByTournamentCode(string $tournamentCode): LobbyEventDTOWrapperDTO
{
$response = $this->riotConnection->get(
GeoRegionEnum::AMERICAS()->getValue(),
sprintf('lol/tournament/v4/lobby-events/by-code/%s', $tournamentCode),
);

return LobbyEventDTOWrapperDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}

public function createProvider(TournamentRegionEnum $region, string $url): int
{
$response = $this->riotConnection->post(
GeoRegionEnum::AMERICAS()->getValue(),
'lol/tournament/v4/providers',
[
'region' => $region->getValue(),
'url' => $url,
],
);

return $response->getBodyContentsDecodedAsInt();
}

public function createTournament(int $providerId, string $name): int
{
$response = $this->riotConnection->post(
GeoRegionEnum::AMERICAS()->getValue(),
'lol/tournament/v4/tournaments',
[
'providerId' => $providerId,
'name' => $name,
],
);

return $response->getBodyContentsDecodedAsInt();
}
}
90 changes: 90 additions & 0 deletions src/Riot/API/Version4/TournamentStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Riot\API\Version4;

use Riot\API\AbstractApi;
use Riot\DTO\LobbyEventDTOWrapperDTO;
use Riot\Enum\GeoRegionEnum;
use Riot\Enum\MapTypeEnum;
use Riot\Enum\PickTypeEnum;
use Riot\Enum\SpectatorTypeEnum;
use Riot\Enum\TournamentRegionEnum;
use Webmozart\Assert\Assert;

final class TournamentStub extends AbstractApi
{
/**
* @param array<string> $allowedSummonerIds
*
* @return array<string>
*/
public function createCode(
int $tournamentId,
int $count,
array $allowedSummonerIds,
int $teamSize,
PickTypeEnum $pickType,
MapTypeEnum $mapType,
SpectatorTypeEnum $spectatorType,
?string $metadata
): array {
Assert::isList($allowedSummonerIds);
Assert::allString($allowedSummonerIds);
Assert::range($teamSize, 1, 5);

$response = $this->riotConnection->post(
GeoRegionEnum::AMERICAS()->getValue(),
sprintf('lol/tournament-stub/v4/codes?count=%d&tournamentId=%d', $count, $tournamentId),
[
'allowedSummonerIds' => $allowedSummonerIds,
'metadata' => $metadata,
'teamSize' => $teamSize,
'pickType' => $pickType->getValue(),
'mapType' => $mapType->getValue(),
'spectatorType' => $spectatorType->getValue(),
],
);

return $response->getBodyContentsDecodedAsArray();
}

public function getLobbyEventsByTournamentCode(string $tournamentCode): LobbyEventDTOWrapperDTO
{
$response = $this->riotConnection->get(
GeoRegionEnum::AMERICAS()->getValue(),
sprintf('lol/tournament-stub/v4/lobby-events/by-code/%s', $tournamentCode),
);

return LobbyEventDTOWrapperDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}

public function createProvider(TournamentRegionEnum $region, string $url): int
{
$response = $this->riotConnection->post(
GeoRegionEnum::AMERICAS()->getValue(),
'lol/tournament-stub/v4/providers',
[
'region' => $region->getValue(),
'url' => $url,
],
);

return $response->getBodyContentsDecodedAsInt();
}

public function createTournament(int $providerId, string $name): int
{
$response = $this->riotConnection->post(
GeoRegionEnum::AMERICAS()->getValue(),
'lol/tournament-stub/v4/tournaments',
[
'providerId' => $providerId,
'name' => $name,
],
);

return $response->getBodyContentsDecodedAsInt();
}
}
34 changes: 34 additions & 0 deletions src/Riot/Collection/LobbyEventDTOCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Riot\Collection;

use Ramsey\Collection\AbstractCollection;
use Riot\DTO\LobbyEventDTO;

final class LobbyEventDTOCollection extends AbstractCollection
{
/**
* @codeCoverageIgnore
*/
public function getType(): string
{
return LobbyEventDTO::class;
}

/**
* @param array<array<string, string>> $data
*
* @return self<LobbyEventDTO>
*/
public static function createFromArray(array $data): self
{
$collection = new self();
foreach ($data as $item) {
$collection->add(LobbyEventDTO::createFromArray($item));
}

return $collection;
}
}
58 changes: 56 additions & 2 deletions src/Riot/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Riot\API\ResponseDecoder;
use Riot\API\ResponseDecoderInterface;
use Riot\Exception\BadGatewayException;
Expand All @@ -31,13 +32,23 @@ final class Connection implements ConnectionInterface

private RequestFactoryInterface $requestFactory;

public function __construct(ClientInterface $riotClient, string $riotApiToken, RequestFactoryInterface $requestFactory)
{
private StreamFactoryInterface $streamFactory;

public function __construct(
ClientInterface $riotClient,
string $riotApiToken,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory
) {
$this->client = $riotClient;
$this->riotApiToken = $riotApiToken;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
}

/**
* {@inheritdoc}
*/
public function get(string $region, string $path): ResponseDecoderInterface
{
$request = $this->requestFactory->createRequest(
Expand All @@ -54,6 +65,49 @@ public function get(string $region, string $path): ResponseDecoderInterface
return new ResponseDecoder($response);
}

public function post(string $region, string $path, array $data): ResponseDecoderInterface
{
return $this->sendRequestWithData(
'POST',
$region,
$path,
$data,
);
}

public function put(string $region, string $path, array $data): ResponseDecoderInterface
{
return $this->sendRequestWithData(
'PUT',
$region,
$path,
$data,
);
}

/**
* @param array<mixed> $data
*/
private function sendRequestWithData(string $method, string $region, string $path, array $data): ResponseDecoderInterface
{
$request = $this->requestFactory->createRequest(
$method,
sprintf('https://%s.%s/%s', $region, self::API_URL, $path),
);
$request = $request->withAddedHeader('X-Riot-Token', $this->riotApiToken);
$request = $request->withBody($this->streamFactory->createStream(json_encode(
$data,
JSON_THROW_ON_ERROR,
)));

$response = $this->client->sendRequest($request);
if (self::STATUS_CODE_OK !== $response->getStatusCode()) {
$this->statusCodeToException($response);
}

return new ResponseDecoder($response);
}

/**
* @throws BadGatewayException
* @throws BadRequestException
Expand Down
Loading