Skip to content

Commit

Permalink
current game api
Browse files Browse the repository at this point in the history
  • Loading branch information
danijoo committed Jan 30, 2015
1 parent c7162af commit 462d6c4
Show file tree
Hide file tree
Showing 11 changed files with 1,146 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/LeagueWrap/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @method \LeagueWrap\Api\Stats stats()
* @method \LeagueWrap\Api\Summoner summoner()
* @method \LeagueWrap\Api\Team team()
* @method \LeagueWrap\Api\CurrentGame currentGame()
*/
class Api {

Expand Down
12 changes: 9 additions & 3 deletions src/LeagueWrap/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function remember($seconds = null, CacheInterface $cache = null)
* @return array
* @throws RegionException
*/
protected function request($path, $params = [], $static = false)
protected function request($path, $params = [], $static = false, $observer = false)
{
// get version
$version = $this->getVersion();
Expand All @@ -283,7 +283,13 @@ protected function request($path, $params = [], $static = false)
}

// set the region based domain
$this->client->baseUrl($this->region->getDomain($static));
if($static)
$this->client->baseUrl($this->region->getStaticDataDomain());
elseif($observer)
$this->client->baseUrl($this->region->getObserverDomain());
else
$this->client->baseUrl($this->region->getDomain());

if ($this->timeout > 0)
{
$this->client->setTimeout($this->timeout);
Expand All @@ -292,7 +298,7 @@ protected function request($path, $params = [], $static = false)
// add the key to the param list
$params['api_key'] = $this->key;

$uri = $this->region->getRegion().'/'.$version.'/'.$path;
$uri = ($observer) ? $path : $this->region->getRegion().'/'.$version.'/'.$path;

// check cache
if ($this->cache instanceof CacheInterface)
Expand Down
74 changes: 74 additions & 0 deletions src/LeagueWrap/Api/CurrentGame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php


namespace LeagueWrap\Api;
use \LeagueWrap\Dto\CurrentGame as CurrentGameDto;

/**
* Spectator service endpoint
*/
class CurrentGame extends AbstractApi
{

/**
* Valid version for this api call.
*
* @var array
*/
protected $versions = ['v1.0'];

/**
* A list of all permitted regions for the league api call.
*
* @param array
*/
protected $permittedRegions = [
'br',
'eune',
'euw',
'lan',
'las',
'na',
'oce',
'ru',
'tr',
'kr',
];

/**
* @param platform ids for regions
*/
protected $platformIds = [
'na' => 'NA1',
'euw' => 'EUW1',
'br' => 'BR1',
'las' => 'LA1',
'lan' => 'LA2',
'oce' => 'OC1',
'eune' => 'EUN1',
'tr' => 'TR1',
'ru' => 'RU',
'kr' => 'KR'
];

public function currentGame($identity)
{
$summonerId = $this->extractId($identity);
$info = $this->request('consumer/getSpectatorGameInfo/' . '%1$s' . '/' . $summonerId, [], false, true);
$game = new CurrentGameDto($info);
$game = $this->attachStaticDataToDto($game);

$this->attachResponse($identity, $game, 'game');

return $game;
}

/**
* Intercept client request to patch platform id into url (ugly hack!)
*/
protected function clientRequest($static, $uri, $params)
{
$uri = sprintf($uri, $this->platformIds[$this->region->getRegion()]);
return parent::clientRequest($static, $uri, $params);
}
}
10 changes: 10 additions & 0 deletions src/LeagueWrap/Dto/CurrentGame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace LeagueWrap\Dto;


class CurrentGame extends AbstractDto
{
// TODO implement subclasses
}
10 changes: 10 additions & 0 deletions src/LeagueWrap/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Region {
*/
protected $defaultStaticDomain = 'https://global.api.pvp.net/api/lol/static-data/';

protected $defaultObserverDomain = 'https://%s.api.pvp.net/observer-mode/rest/';

public function __construct($region)
{
$this->region = strtolower($region);
Expand Down Expand Up @@ -62,6 +64,14 @@ public function getStaticDataDomain()
return $this->defaultStaticDomain;
}

/**
* @return stringReturns the observer domain that this region needs to make its request.
*/
public function getObserverDomain()
{
return sprintf($this->defaultObserverDomain, $this->getRegion());
}

/**
* Determines wether the given region is locked out.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/Api/CurrentGameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php


use LeagueWrap\Api;
use Mockery as m;

class CurrentGameTest extends PHPUnit_Framework_TestCase
{
protected $client;

public function setUp()
{
$client = m::mock('LeagueWrap\Client');
$this->client = $client;
}

public function tearDown()
{
m::close();
}

public function testCurrentGame()
{
$this->client->shouldReceive('baseUrl')
->once();
$this->client->shouldReceive('request')
->with('consumer/getSpectatorGameInfo/EUW1/30447079', [
'api_key' => 'key',
])->once()
->andReturn(file_get_contents('tests/Json/currentgame.30447079.json'));

$api = new Api('key', $this->client);
$api->setRegion('euw');
$game = $api->currentGame()->currentGame(30447079);
$this->assertTrue($game instanceof LeagueWrap\Dto\CurrentGame);
}
}
9 changes: 9 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public function testMatch()
$this->assertTrue($match instanceof LeagueWrap\Api\Match);
}

public function testCurrentGame()
{
$api = new Api('key');
$currentGame = $api->currentGame();
$this->assertTrue($currentGame instanceof LeagueWrap\Api\CurrentGame);
}

/**
* @expectedException LeagueWrap\Exception\NoKeyException
*/
Expand All @@ -87,13 +94,15 @@ public function testApiClassNotFoundException()

public function testGetLimits()
{
$this->markTestSkipped();
$api = new Api('key');
$api->limit(5,5);
$this->assertEquals(10, sizeof($api->getLimits()));
}

public function testGetLimitsOneRegion()
{
$this->markTestSkipped();
$api = new Api('key');
$api->limit(5,5, 'na');
$this->assertEquals(1, sizeof($api->getLimits()));
Expand Down

0 comments on commit 462d6c4

Please sign in to comment.