Skip to content

Commit

Permalink
Implemented first citizen for GuestSession's, fixes php-tmdb#20
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Apr 5, 2014
1 parent d458133 commit 78864fd
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 5 deletions.
3 changes: 3 additions & 0 deletions apikey.php.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
// Fill these out when you want to use the examples and rename or copy it to apikey.php

define('TMDB_API_KEY', 'API_KEY');
define('TMDB_REQUEST_TOKEN', 'REQUEST_TOKEN'); // for accounts
define('TMDB_SESSION_TOKEN', 'SESSION_TOKEN'); // for accounts
define('TMDB_GUEST_SESSION_TOKEN', 'GUEST_SESSION_TOKEN'); // for guest sessions
define('TMDB_ACCOUNT_ID', 12345);

define('TMDB_LIST_ID', 'LIST_ID');
Expand Down
24 changes: 24 additions & 0 deletions examples/guest_session/api/rated_movies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once '../../../vendor/autoload.php';
require_once '../../../apikey.php';

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$sessionToken = new \Tmdb\GuestSessionToken(TMDB_GUEST_SESSION_TOKEN);
$client->setSessionToken($sessionToken);

$rated_movies = $client->getGuestSessionApi()->getRatedMovies();

var_dump($rated_movies);
28 changes: 28 additions & 0 deletions examples/guest_session/model/rated_movies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
require_once '../../../vendor/autoload.php';
require_once '../../../apikey.php';

$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$guestSessionToken = new \Tmdb\GuestSessionToken(TMDB_GUEST_SESSION_TOKEN);
$client->setSessionToken($guestSessionToken);

/**
* @var \Tmdb\Repository\GuestSessionRepository $guestSessionRepository
*/
$guestSessionRepository = new \Tmdb\Repository\GuestSessionRepository($client);
$lists = $guestSessionRepository->getRatedMovies();

var_dump($lists);
9 changes: 8 additions & 1 deletion examples/movies/model/rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
$token = new \Tmdb\ApiToken(TMDB_API_KEY);
$client = new \Tmdb\Client($token);

$sessionToken = new \Tmdb\SessionToken(TMDB_SESSION_TOKEN);
/**
$sessionToken = new \Tmdb\SessionToken(TMDB_SESSION_TOKEN);
$client->setSessionToken($sessionToken);
*/

$client->setLogging(true, '/www/dev/php-tmdb-api/tmdb.log');

$guestSessionToken = new \Tmdb\GuestSessionToken(TMDB_GUEST_SESSION_TOKEN);
$client->setSessionToken($guestSessionToken);

$repository = new \Tmdb\Repository\MovieRepository($client);
$rate = $repository->rate(49047, 6.5);
Expand Down
42 changes: 42 additions & 0 deletions lib/Tmdb/Api/GuestSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Api;
use Tmdb\Exception\MissingSessionTokenException;
use Tmdb\SessionToken;

/**
* Class GuestSession
* @package Tmdb\Api
* @see http://docs.themoviedb.apiary.io/#guestsessions
*/
class GuestSession extends AbstractApi
{
/**
* Get a list of rated movies for a specific guest session id.
*
* @param array $parameters
* @param array $headers
* @throws MissingSessionTokenException when the guest session token was not set on the client.
* @return mixed
*/
public function getRatedMovies(array $parameters = array(), array $headers = array())
{
$sessionToken = $this->client->getSessionToken();

if (!$sessionToken instanceof SessionToken) {
throw new MissingSessionTokenException('The guest session token was not set on the client.');
}

return $this->get('guest_session/' . $sessionToken->getToken() . '/rated_movies', $parameters, $headers);
}
}
8 changes: 8 additions & 0 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ public function getTimezonesApi()
return new Api\Timezones($this);
}

/**
* @return Api\GuestSession
*/
public function getGuestSessionApi()
{
return new Api\GuestSession($this);
}

/**
* @return HttpClient|HttpClientInterface
*/
Expand Down
21 changes: 21 additions & 0 deletions lib/Tmdb/Exception/MissingSessionTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Exception;

/**
* Class MissingSessionTokenException
* @package Tmdb\Exception
*/
class MissingSessionTokenException extends \RuntimeException
{
}
3 changes: 2 additions & 1 deletion lib/Tmdb/Factory/AuthenticationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Tmdb\Factory;

use Tmdb\Exception\NotImplementedException;
use Tmdb\GuestSessionToken;
use Tmdb\RequestToken;
use Tmdb\SessionToken;

Expand Down Expand Up @@ -98,7 +99,7 @@ public function createSessionToken(array $data = array())
*/
public function createGuestSessionToken(array $data = array())
{
$token = new SessionToken();
$token = new GuestSessionToken();

if (array_key_exists('expires_at', $data)) {
$token->setExpiresAt(new \DateTime($data['expires_at']));
Expand Down
36 changes: 36 additions & 0 deletions lib/Tmdb/Factory/GuestSessionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Factory;

/**
* Currently a place-holder for future expansions
*
* Class GuestSessionFactory
* @package Tmdb\Factory
*/
class GuestSessionFactory extends AbstractFactory
{
/**
* {@inheritdoc}
*/
public function create(array $data = array())
{
}

/**
* {@inheritdoc}
*/
public function createCollection(array $data = array())
{
}
}
21 changes: 21 additions & 0 deletions lib/Tmdb/GuestSessionToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb;

/**
* Class GuestSessionToken
* @package Tmdb
*/
class GuestSessionToken extends SessionToken
{
}
7 changes: 6 additions & 1 deletion lib/Tmdb/HttpClient/Plugin/SessionTokenPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Guzzle\Common\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tmdb\GuestSessionToken;
use Tmdb\SessionToken;

/**
Expand Down Expand Up @@ -41,7 +42,11 @@ public function onBeforeSend(Event $event)
{
$url = $event['request']->getUrl(true);

$url->getQuery()->set('session_id', $this->token->getToken());
if ($this->token instanceof GuestSessionToken) {
$url->getQuery()->set('guest_session_id', $this->token->getToken());
} else {
$url->getQuery()->set('session_id', $this->token->getToken());
}

$event['request']->setUrl($url);
}
Expand Down
70 changes: 70 additions & 0 deletions lib/Tmdb/Repository/GuestSessionRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Repository;

use Tmdb\Factory\GuestSessionFactory;
use Tmdb\Factory\MovieFactory;
use Tmdb\Model\Collection\ResultCollection;
use Tmdb\Model\Movie;

/**
* Class GuestSessionRepository
* @package Tmdb\Repository
* @see http://docs.themoviedb.apiary.io/#guestsessions
*/
class GuestSessionRepository extends AbstractRepository
{
/**
* Get the list of top rated movies.
*
* By default, this list will only include movies that have 10 or more votes.
* This list refreshes every day.
*
* @param array $options
* @return ResultCollection|Movie[]
*/
public function getRatedMovies(array $options = array())
{
return $this->getMovieFactory()->createResultCollection(
$this->getApi()->getRatedMovies($options)
);
}

/**
* Return the Movies API Class
*
* @return \Tmdb\Api\GuestSession
*/
public function getApi()
{
return $this->getClient()->getGuestSessionApi();
}

/**
* Return the Guest Session Factory
*
* @return GuestSessionFactory
*/
public function getFactory()
{
return new GuestSessionFactory();
}

/**
* @return MovieFactory
*/
public function getMovieFactory()
{
return new MovieFactory();
}
}
48 changes: 48 additions & 0 deletions test/Tmdb/Tests/Api/GuestSessionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\Api;

use Tmdb\GuestSessionToken;

class GuestSessionTest extends TestCase
{
/**
* @test
* @expectedException Tmdb\Exception\MissingSessionTokenException
*/
public function shouldThrowExceptionGettingRatedMoviesWithNoSessionToken()
{
$api = $this->getApiMock();
$api->getRatedMovies();
}

/**
* @test
*/
public function shouldGetRatedMovies()
{
$sessionToken = new GuestSessionToken('xyz');
$api = $this->getApiMock(array(), array(), $sessionToken);

$api->expects($this->once())
->method('get')
->with('guest_session/xyz/rated_movies');

$api->getRatedMovies();
}

protected function getApiClass()
{
return 'Tmdb\Api\GuestSession';
}
}
8 changes: 6 additions & 2 deletions test/Tmdb/Tests/Api/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ abstract class TestCase extends Base

abstract protected function getApiClass();

protected function getApiMock(array $methods = array())
protected function getApiMock(array $methods = array(), array $clientMethods = array(), $sessionToken = null)
{
if ($this->_api) {
return $this->_api;
}

$client = $this->getClientWithMockedHttpClient();
$client = $this->getClientWithMockedHttpClient($clientMethods);

if ($sessionToken) {
$client->setSessionToken($sessionToken);
}

return $this->getMockBuilder($this->getApiClass())
->setMethods(
Expand Down
Loading

0 comments on commit 78864fd

Please sign in to comment.