Skip to content

Commit

Permalink
Merge pull request #27 from simivar/feature/implement-all-lor-ranked
Browse files Browse the repository at this point in the history
 Implement all Lor Leaderboard v1 endpoints
  • Loading branch information
simivar authored Oct 23, 2020
2 parents 8387f2c + 4c3131d commit 29f3ea9
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ composer require simivar/riot-php symfony/http-client nyholm/psr7
| League v4 | [docs](https://developer.riotgames.com/apis#league-v4) | - |
| Lol Status v3 | [docs](https://developer.riotgames.com/apis#lol-status-v3) | 100% |
| Lor Match v1 | [docs](https://developer.riotgames.com/apis#lor-match-v1) | - |
| Lor Ranked v1 | [docs](https://developer.riotgames.com/apis#lor-ranked-v1) | - |
| Lor Ranked v1 | [docs](https://developer.riotgames.com/apis#lor-ranked-v1) | 100% |
| Match v4 | [docs](https://developer.riotgames.com/apis#match-v4) | - |
| Spectator v4 | [docs](https://developer.riotgames.com/apis#spectator-v4) | - |
| Summoner v4 | [docs](https://developer.riotgames.com/apis#summoner-v4) | 100% |
Expand Down
9 changes: 9 additions & 0 deletions src/Riot/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ public function getLolStatusV3Api(): Version3\LolStatus

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

public function getLorRankedV1Api(): Version1\LorRanked
{
if (!isset($this->apis['lorRankedV1'])) {
$this->apis['lorRankedV1'] = new Version1\LorRanked($this->riotConnection);
}

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

declare(strict_types=1);

namespace Riot\API\Version1;

use JsonException;
use Psr\Http\Client\ClientExceptionInterface;
use Riot\API\AbstractApi;
use Riot\DTO\LeaderboardDTO;
use Riot\Exception as RiotException;

final class LorRanked extends AbstractApi
{
/**
* @throws JsonException
* @throws RiotException\BadGatewayException
* @throws RiotException\BadRequestException
* @throws RiotException\DataNotFoundException
* @throws RiotException\ForbiddenException
* @throws RiotException\GatewayTimeoutException
* @throws RiotException\InternalServerErrorException
* @throws RiotException\MethodNotAllowedException
* @throws RiotException\RateLimitExceededException
* @throws RiotException\ServiceUnavailableException
* @throws RiotException\UnauthorizedException
* @throws RiotException\UnsupportedMediaTypeException
* @throws ClientExceptionInterface
*/
public function getLeaderboards(string $geoRegion): LeaderboardDTO
{
$response = $this->riotConnection->get(
$geoRegion,
'lor/ranked/v1/leaderboards',
);
$body = $response->getBody()->getContents();

return LeaderboardDTO::createFromArray(json_decode($body, true, 512, JSON_THROW_ON_ERROR));
}
}
2 changes: 2 additions & 0 deletions src/Riot/Collection/IncidentDTOCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getType(): string

/**
* @param array<array<string, mixed>> $data
*
* @return IncidentDTOCollection<IncidentDTO>
*/
public static function createFromArray(array $data): self
{
Expand Down
2 changes: 2 additions & 0 deletions src/Riot/Collection/MessageDTOCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getType(): string

/**
* @param array<array<string, array|string>> $data
*
* @return MessageDTOCollection<MessageDTO>
*/
public static function createFromArray(array $data): self
{
Expand Down
34 changes: 34 additions & 0 deletions src/Riot/Collection/PlayerDTOCollection.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\PlayerDTO;

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

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

return $collection;
}
}
2 changes: 2 additions & 0 deletions src/Riot/Collection/ServiceDTOCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getType(): string

/**
* @param array<array<string, array|string>> $data
*
* @return ServiceDTOCollection<ServiceDTO>
*/
public static function createFromArray(array $data): self
{
Expand Down
2 changes: 2 additions & 0 deletions src/Riot/Collection/TranslationDTOCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function getType(): string

/**
* @param array<array<string, string>> $data
*
* @return TranslationDTOCollection<TranslationDTO>
*/
public static function createFromArray(array $data): self
{
Expand Down
33 changes: 33 additions & 0 deletions src/Riot/DTO/LeaderboardDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Riot\DTO;

use Riot\Collection\PlayerDTOCollection;

final class LeaderboardDTO implements DTOInterface
{
/** @var PlayerDTOCollection<PlayerDTO> */
private PlayerDTOCollection $players;

/**
* @param PlayerDTOCollection<PlayerDTO> $players
*/
public function __construct(PlayerDTOCollection $players)
{
$this->players = $players;
}

public function getPlayers(): PlayerDTOCollection
{
return $this->players;
}

public static function createFromArray(array $data): self
{
return new self(
PlayerDTOCollection::createFromArray($data['players']),
);
}
}
45 changes: 45 additions & 0 deletions src/Riot/DTO/PlayerDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Riot\DTO;

final class PlayerDTO implements DTOInterface
{
private string $name;

private int $rank;

private int $lp;

public function __construct(string $name, int $rank, int $lp)
{
$this->name = $name;
$this->rank = $rank;
$this->lp = $lp;
}

public function getName(): string
{
return $this->name;
}

public function getRank(): int
{
return $this->rank;
}

public function getLp(): int
{
return $this->lp;
}

public static function createFromArray(array $data): self
{
return new self(
$data['name'],
$data['rank'],
(int) $data['lp'],
);
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Collection/PlayerDTOCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Riot\Tests\Unit\DTO;

use PHPUnit\Framework\TestCase;
use Riot\Collection\PlayerDTOCollection;
use Riot\DTO\PlayerDTO;

final class PlayerDTOCollectionTest extends TestCase
{
public function testCreateFromArrayCreatesProperObject(): void
{
$data = [
'players' => [
'name' => 'Player One',
'rank' => 0,
'lp' => 367,
],
];
$object = PlayerDTOCollection::createFromArray($data);
self::assertFalse($object->isEmpty());
self::assertInstanceOf(PlayerDTO::class, $object->offsetGet(0));
}

public function testCreateFromArrayReturnsEmptyCollectionWhenNoData(): void
{
$data = [];
$object = PlayerDTOCollection::createFromArray($data);
self::assertTrue($object->isEmpty());
}
}
21 changes: 21 additions & 0 deletions tests/Unit/DTO/LeaderboardDTOTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Riot\Tests\Unit\DTO;

use PHPUnit\Framework\TestCase;
use Riot\Collection\PlayerDTOCollection;
use Riot\DTO\LeaderboardDTO;

final class LeaderboardDTOTest extends TestCase
{
public function testCreateFromArrayCreatesProperObject(): void
{
$data = [
'players' => [],
];
$object = LeaderboardDTO::createFromArray($data);
self::assertInstanceOf(PlayerDTOCollection::class, $object->getPlayers());
}
}
24 changes: 24 additions & 0 deletions tests/Unit/DTO/PlayerDTOTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Riot\Tests\Unit\DTO;

use PHPUnit\Framework\TestCase;
use Riot\DTO\PlayerDTO;

final class PlayerDTOTest extends TestCase
{
public function testCreateFromArrayCreatesProperObject(): void
{
$data = [
'name' => 'Player One',
'rank' => 0,
'lp' => 367,
];
$object = PlayerDTO::createFromArray($data);
self::assertSame('Player One', $object->getName());
self::assertSame(0, $object->getRank());
self::assertSame(367, $object->getLp());
}
}

0 comments on commit 29f3ea9

Please sign in to comment.