diff --git a/apikey.php.dist b/apikey.php.dist index 6b3ab467..35f2201f 100644 --- a/apikey.php.dist +++ b/apikey.php.dist @@ -1,7 +1,10 @@ + * @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); diff --git a/examples/guest_session/model/rated_movies.php b/examples/guest_session/model/rated_movies.php new file mode 100644 index 00000000..5c028340 --- /dev/null +++ b/examples/guest_session/model/rated_movies.php @@ -0,0 +1,28 @@ + + * @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); diff --git a/examples/movies/model/rate.php b/examples/movies/model/rate.php index d60c257e..8909f86a 100644 --- a/examples/movies/model/rate.php +++ b/examples/movies/model/rate.php @@ -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); diff --git a/lib/Tmdb/Api/GuestSession.php b/lib/Tmdb/Api/GuestSession.php new file mode 100644 index 00000000..a92f2e00 --- /dev/null +++ b/lib/Tmdb/Api/GuestSession.php @@ -0,0 +1,42 @@ + + * @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); + } +} diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php index f0c99498..bffac94f 100644 --- a/lib/Tmdb/Client.php +++ b/lib/Tmdb/Client.php @@ -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 */ diff --git a/lib/Tmdb/Exception/MissingSessionTokenException.php b/lib/Tmdb/Exception/MissingSessionTokenException.php new file mode 100644 index 00000000..79c0a3db --- /dev/null +++ b/lib/Tmdb/Exception/MissingSessionTokenException.php @@ -0,0 +1,21 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Exception; + +/** + * Class MissingSessionTokenException + * @package Tmdb\Exception + */ +class MissingSessionTokenException extends \RuntimeException +{ +} diff --git a/lib/Tmdb/Factory/AuthenticationFactory.php b/lib/Tmdb/Factory/AuthenticationFactory.php index 3ec8ffd4..f14f7ba0 100644 --- a/lib/Tmdb/Factory/AuthenticationFactory.php +++ b/lib/Tmdb/Factory/AuthenticationFactory.php @@ -13,6 +13,7 @@ namespace Tmdb\Factory; use Tmdb\Exception\NotImplementedException; +use Tmdb\GuestSessionToken; use Tmdb\RequestToken; use Tmdb\SessionToken; @@ -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'])); diff --git a/lib/Tmdb/Factory/GuestSessionFactory.php b/lib/Tmdb/Factory/GuestSessionFactory.php new file mode 100644 index 00000000..fe6d25d2 --- /dev/null +++ b/lib/Tmdb/Factory/GuestSessionFactory.php @@ -0,0 +1,36 @@ + + * @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()) + { + } +} diff --git a/lib/Tmdb/GuestSessionToken.php b/lib/Tmdb/GuestSessionToken.php new file mode 100644 index 00000000..184ebbde --- /dev/null +++ b/lib/Tmdb/GuestSessionToken.php @@ -0,0 +1,21 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb; + +/** + * Class GuestSessionToken + * @package Tmdb + */ +class GuestSessionToken extends SessionToken +{ +} diff --git a/lib/Tmdb/HttpClient/Plugin/SessionTokenPlugin.php b/lib/Tmdb/HttpClient/Plugin/SessionTokenPlugin.php index d7536df0..6a84979d 100644 --- a/lib/Tmdb/HttpClient/Plugin/SessionTokenPlugin.php +++ b/lib/Tmdb/HttpClient/Plugin/SessionTokenPlugin.php @@ -14,6 +14,7 @@ use Guzzle\Common\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Tmdb\GuestSessionToken; use Tmdb\SessionToken; /** @@ -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); } diff --git a/lib/Tmdb/Repository/GuestSessionRepository.php b/lib/Tmdb/Repository/GuestSessionRepository.php new file mode 100644 index 00000000..e884e513 --- /dev/null +++ b/lib/Tmdb/Repository/GuestSessionRepository.php @@ -0,0 +1,70 @@ + + * @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(); + } +} diff --git a/test/Tmdb/Tests/Api/GuestSessionTest.php b/test/Tmdb/Tests/Api/GuestSessionTest.php new file mode 100644 index 00000000..0ffbf07e --- /dev/null +++ b/test/Tmdb/Tests/Api/GuestSessionTest.php @@ -0,0 +1,48 @@ + + * @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'; + } +} diff --git a/test/Tmdb/Tests/Api/TestCase.php b/test/Tmdb/Tests/Api/TestCase.php index dcd6072b..52bfc39d 100644 --- a/test/Tmdb/Tests/Api/TestCase.php +++ b/test/Tmdb/Tests/Api/TestCase.php @@ -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( diff --git a/test/Tmdb/Tests/GuestSessionTokenTest.php b/test/Tmdb/Tests/GuestSessionTokenTest.php new file mode 100644 index 00000000..4e0869b5 --- /dev/null +++ b/test/Tmdb/Tests/GuestSessionTokenTest.php @@ -0,0 +1,33 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests; + +class GuestSessionTokenTest extends \PHPUnit_Framework_TestCase +{ + const SESSION_TOKEN = '80b2bf99520cd795ff54e31af97917bc9e3a7c8d'; + + /** + * @test + */ + public function testSetGet() + { + $token = new \Tmdb\SessionToken(); + $token->setToken(self::SESSION_TOKEN); + $token->setExpiresAt('2014-12-04 22:51:19 UTC'); + $token->setSuccess(true); + + $this->assertEquals(self::SESSION_TOKEN, $token->getToken()); + $this->assertEquals('04-12-2014', $token->getExpiresAt()->format('d-m-Y')); + $this->assertEquals(true, $token->getSuccess()); + } +}