Skip to content
Open
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
62 changes: 61 additions & 1 deletion src/Riot/API/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,73 @@
namespace Riot\API;

use Riot\ConnectionInterface;
use Riot\Enum\GeoRegionEnum;
use Riot\Enum\RegionEnum;

abstract class AbstractApi
{
protected ConnectionInterface $riotConnection;
private ConnectionInterface $riotConnection;

public function __construct(ConnectionInterface $riotConnection)
{
$this->riotConnection = $riotConnection;
}

/**
* @param string|RegionEnum|GeoRegionEnum $unionRegion
*/
private function getData($unionRegion, string $path): ResponseDecoderInterface
{
/** @var string $region * */
$region = $unionRegion;
if ($unionRegion instanceof RegionEnum) {
$region = $unionRegion->getValue();
} elseif ($unionRegion instanceof GeoRegionEnum) {
$region = $unionRegion->__toString();
}

return $this->riotConnection->get($region, $path);
}

/**
* @param array<mixed> $data
*/
public function post(string $region, string $path, array $data): ResponseDecoderInterface
{
return $this->riotConnection->post($region, $path, $data);
}

/**
* @param array<mixed> $data
*/
public function put(string $region, string $path, array $data): ResponseDecoderInterface
{
return $this->riotConnection->put($region, $path, $data);
}

/**
* @param string|RegionEnum|GeoRegionEnum $region
*
* @return array<mixed>
*/
protected function get($region, string $path): array
{
return $this->getData($region, $path)->getBodyContentsDecodedAsArray();
}

/**
* @param string|RegionEnum|GeoRegionEnum $region
*/
protected function getInt($region, string $path): int
{
return $this->getData($region, $path)->getBodyContentsDecodedAsInt();
}

/**
* @param string|RegionEnum|GeoRegionEnum $region
*/
protected function getString($region, string $path): string
{
return $this->getData($region, $path)->getBodyContentsDecodedAsString();
}
}
21 changes: 6 additions & 15 deletions src/Riot/API/Version1/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ final class Account extends AbstractApi
*/
public function getByPuuid(string $puuid, GeoRegionEnum $geoRegion): AccountDTO
{
$response = $this->riotConnection->get(
$geoRegion->__toString(),
sprintf('riot/account/v1/accounts/by-puuid/%s', $puuid),
return AccountDTO::createFromArray(
$this->get($geoRegion->__toString(), sprintf('riot/account/v1/accounts/by-puuid/%s', $puuid))
);

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

/**
Expand All @@ -56,12 +53,9 @@ public function getByPuuid(string $puuid, GeoRegionEnum $geoRegion): AccountDTO
*/
public function getByGameNameAndTagLine(string $gameName, string $tagLine, GeoRegionEnum $geoRegion): AccountDTO
{
$response = $this->riotConnection->get(
$geoRegion->__toString(),
sprintf('riot/account/v1/accounts/by-riot-id/%s/%s', $gameName, $tagLine),
return AccountDTO::createFromArray(
$this->get($geoRegion, sprintf('riot/account/v1/accounts/by-riot-id/%s/%s', $gameName, $tagLine))
);

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

/**
Expand All @@ -81,11 +75,8 @@ public function getByGameNameAndTagLine(string $gameName, string $tagLine, GeoRe
*/
public function getByGameAndPuuid(string $game, string $puuid, GeoRegionEnum $geoRegion): ActiveShardDTO
{
$response = $this->riotConnection->get(
$geoRegion->__toString(),
sprintf('riot/account/v1/active-shards/by-game/%s/by-puuid/%s', $game, $puuid),
return ActiveShardDTO::createFromArray(
$this->get($geoRegion, sprintf('riot/account/v1/active-shards/by-game/%s/by-puuid/%s', $game, $puuid))
);

return ActiveShardDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}
}
35 changes: 10 additions & 25 deletions src/Riot/API/Version1/Clash.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,36 @@ final class Clash extends AbstractApi
{
public function getPlayersBySummonerId(string $encryptedSummonerId, RegionEnum $region): PlayerDTOCollection
{
$response = $this->riotConnection->get(
$region->getValue(),
sprintf('lol/clash/v1/players/by-summoner/%s', $encryptedSummonerId),
return PlayerDTOCollection::createFromArray(
$this->get($region, sprintf('lol/clash/v1/players/by-summoner/%s', $encryptedSummonerId))
);

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

public function getTeamById(string $teamid, RegionEnum $region): TeamDTO
{
$response = $this->riotConnection->get(
$region->getValue(),
sprintf('lol/clash/v1/teams/%s', $teamid),
return TeamDTO::createFromArray(
$this->get($region, sprintf('lol/clash/v1/teams/%s', $teamid))
);

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

public function getTournaments(RegionEnum $region): TournamentDTOCollection
{
$response = $this->riotConnection->get(
$region->getValue(),
'lol/clash/v1/tournaments',
return TournamentDTOCollection::createFromArray(
$this->get($region, 'lol/clash/v1/tournaments')
);

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

public function getTournamentByTeamId(string $teamId, RegionEnum $region): TournamentDTO
{
$response = $this->riotConnection->get(
$region->getValue(),
sprintf('lol/clash/v1/tournaments/by-team/%s', $teamId),
return TournamentDTO::createFromArray(
$this->get($region, sprintf('lol/clash/v1/tournaments/by-team/%s', $teamId))
);

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

public function getTournamentById(string $tournamentId, RegionEnum $region): TournamentDTO
{
$response = $this->riotConnection->get(
$region->getValue(),
sprintf('lol/clash/v1/tournaments/%s', $tournamentId),
return TournamentDTO::createFromArray(
$this->get($region, sprintf('lol/clash/v1/tournaments/%s', $tournamentId))
);

return TournamentDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}
}
14 changes: 3 additions & 11 deletions src/Riot/API/Version1/LorMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ final class LorMatch extends AbstractApi
*/
public function getIdsByPuuid(string $puuid, GeoRegionEnum $geoRegion): array
{
$response = $this->riotConnection->get(
$geoRegion->__toString(),
sprintf('lor/match/v1/matches/by-puuid/%s/ids', $puuid),
);

return $response->getBodyContentsDecodedAsArray();
return $this->get($geoRegion, sprintf('lor/match/v1/matches/by-puuid/%s/ids', $puuid));
}

/**
Expand All @@ -57,11 +52,8 @@ public function getIdsByPuuid(string $puuid, GeoRegionEnum $geoRegion): array
*/
public function getById(string $matchId, GeoRegionEnum $geoRegion): MatchDTO
{
$response = $this->riotConnection->get(
$geoRegion->__toString(),
sprintf('lor/match/v1/matches/%s', $matchId),
return MatchDTO::createFromArray(
$this->get($geoRegion, sprintf('lor/match/v1/matches/%s', $matchId))
);

return MatchDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}
}
7 changes: 2 additions & 5 deletions src/Riot/API/Version1/LorRanked.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ final class LorRanked extends AbstractApi
*/
public function getLeaderboards(GeoRegionEnum $geoRegion): LeaderboardDTO
{
$response = $this->riotConnection->get(
$geoRegion->__toString(),
'lor/ranked/v1/leaderboards',
return LeaderboardDTO::createFromArray(
$this->get($geoRegion, 'lor/ranked/v1/leaderboards')
);

return LeaderboardDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}
}
7 changes: 2 additions & 5 deletions src/Riot/API/Version3/Champion.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ final class Champion extends AbstractApi
*/
public function getChampionRotations(RegionEnum $region): ChampionInfoDTO
{
$response = $this->riotConnection->get(
$region->__toString(),
'lol/platform/v3/champion-rotations',
return ChampionInfoDTO::createFromArray(
$this->get($region, 'lol/platform/v3/champion-rotations')
);

return ChampionInfoDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}
}
7 changes: 2 additions & 5 deletions src/Riot/API/Version3/LolStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ final class LolStatus extends AbstractApi
*/
public function getShardData(RegionEnum $region): ShardStatusDTO
{
$response = $this->riotConnection->get(
$region->__toString(),
'lol/status/v3/shard-data',
return ShardStatusDTO::createFromArray(
$this->get($region, 'lol/status/v3/shard-data')
);

return ShardStatusDTO::createFromArray($response->getBodyContentsDecodedAsArray());
}
}
24 changes: 6 additions & 18 deletions src/Riot/API/Version4/ChampionMastery.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ final class ChampionMastery extends AbstractApi
*/
public function getBySummonerId(string $encryptedSummonerId, RegionEnum $region): ChampionMasteryDTOCollection
{
$response = $this->riotConnection->get(
$region->__toString(),
sprintf('lol/champion-mastery/v4/champion-masteries/by-summoner/%s', $encryptedSummonerId),
$championMasteries = $this->get(
$region, sprintf('lol/champion-mastery/v4/champion-masteries/by-summoner/%s', $encryptedSummonerId)
);

$championMasteries = $response->getBodyContentsDecodedAsArray();
$collection = new ChampionMasteryDTOCollection();
foreach ($championMasteries as $championMastery) {
$collection->add(ChampionMasteryDTO::createFromArray($championMastery));
Expand Down Expand Up @@ -67,16 +65,14 @@ public function getBySummonerIdAndChampionId(
int $championId,
RegionEnum $region
): ChampionMasteryDTO {
$response = $this->riotConnection->get(
$region->__toString(),
return ChampionMasteryDTO::createFromArray($this->get(
$region,
sprintf(
'lol/champion-mastery/v4/champion-masteries/by-summoner/%s/by-champion/%s',
$encryptedSummonerId,
$championId,
),
);

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

/**
Expand All @@ -96,14 +92,6 @@ public function getBySummonerIdAndChampionId(
*/
public function getScoreBySummonerId(string $encryptedSummonerId, RegionEnum $region): int
{
$response = $this->riotConnection->get(
$region->__toString(),
sprintf(
'lol/champion-mastery/v4/scores/by-summoner/%s',
$encryptedSummonerId,
),
);

return $response->getBodyContentsDecodedAsInt();
return $this->getInt($region, sprintf('lol/champion-mastery/v4/scores/by-summoner/%s', $encryptedSummonerId));
}
}
48 changes: 12 additions & 36 deletions src/Riot/API/Version4/League.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,16 @@ final class League extends AbstractApi
{
public function getChallengerLeaguesByQueue(QueueEnum $queue, RegionEnum $region): LeagueListDTO
{
$response = $this->riotConnection->get(
$region->__toString(),
sprintf('lol/league/v4/challengerleagues/by-queue/%s', $queue->__toString()),
return LeagueListDTO::createFromArray(
$this->get($region, sprintf('lol/league/v4/challengerleagues/by-queue/%s', $queue))
);

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

public function getBySummonerId(string $encryptedSummonerId, RegionEnum $region): LeagueEntryDTOCollection
{
$response = $this->riotConnection->get(
$region->__toString(),
sprintf('lol/league/v4/entries/by-summoner/%s', $encryptedSummonerId),
return LeagueEntryDTOCollection::createFromArray(
$this->get($region, sprintf('lol/league/v4/entries/by-summoner/%s', $encryptedSummonerId))
);

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

public function getByQueueAndTierAndDivision(
Expand All @@ -41,47 +35,29 @@ public function getByQueueAndTierAndDivision(
RegionEnum $region,
int $page = 1
): LeagueEntryDTOCollection {
$response = $this->riotConnection->get(
$region->__toString(),
sprintf(
'lol/league/v4/entries/%s/%s/%s?page=%d',
$queue->__toString(),
$tier->__toString(),
$division->__toString(),
$page
),
return LeagueEntryDTOCollection::createFromArray(
$this->get($region, sprintf('lol/league/v4/entries/%s/%s/%s?page=%d', $queue, $tier, $division, $page))
);

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

public function getGrandmasterLeaguesByQueue(QueueEnum $queue, RegionEnum $region): LeagueListDTO
{
$response = $this->riotConnection->get(
$region->__toString(),
sprintf('lol/league/v4/grandmasterleagues/by-queue/%s', $queue->__toString()),
return LeagueListDTO::createFromArray(
$this->get($region, sprintf('lol/league/v4/grandmasterleagues/by-queue/%s', $queue))
);

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

public function getById(string $leagueId, RegionEnum $region): LeagueListDTO
{
$response = $this->riotConnection->get(
$region->__toString(),
sprintf('lol/league/v4/leagues/%s', $leagueId),
return LeagueListDTO::createFromArray(
$this->get($region, sprintf('lol/league/v4/leagues/%s', $leagueId))
);

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

public function getMasterLeaguesByQueue(QueueEnum $queue, RegionEnum $region): LeagueListDTO
{
$response = $this->riotConnection->get(
$region->__toString(),
sprintf('lol/league/v4/masterleagues/by-queue/%s', $queue->__toString()),
return LeagueListDTO::createFromArray(
$this->get($region, sprintf('lol/league/v4/masterleagues/by-queue/%s', $queue))
);

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