From e62f084df0422a494d276fe254e7e5eb68784091 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Wed, 26 Feb 2014 03:34:45 +0100 Subject: [PATCH] Adding the rest of Account related methods --- .../model/authenticate_request_token.php | 23 ++++ .../model/guest_session_token.php | 22 +++ .../authentication/model/request_token.php | 23 ++++ .../authentication/model/session_token.php | 25 ++++ lib/Tmdb/Factory/AuthenticationFactory.php | 113 ++++++++++++++++ .../Repository/AuthenticationRepository.php | 94 +++++++++++++ lib/Tmdb/RequestToken.php | 23 ++++ lib/Tmdb/SessionToken.php | 53 ++++++++ .../Repository/AccountRepositoryTest.php | 126 ++++++++++++++++++ .../AuthenticationRepositoryTest.php | 70 ++++++++++ test/Tmdb/Tests/RequestTokenTest.php | 2 + test/Tmdb/Tests/SessionTokenTest.php | 4 + 12 files changed, 578 insertions(+) create mode 100644 examples/authentication/model/authenticate_request_token.php create mode 100644 examples/authentication/model/guest_session_token.php create mode 100644 examples/authentication/model/request_token.php create mode 100644 examples/authentication/model/session_token.php create mode 100644 lib/Tmdb/Factory/AuthenticationFactory.php create mode 100644 lib/Tmdb/Repository/AuthenticationRepository.php create mode 100644 test/Tmdb/Tests/Repository/AccountRepositoryTest.php create mode 100644 test/Tmdb/Tests/Repository/AuthenticationRepositoryTest.php diff --git a/examples/authentication/model/authenticate_request_token.php b/examples/authentication/model/authenticate_request_token.php new file mode 100644 index 00000000..16b1e29d --- /dev/null +++ b/examples/authentication/model/authenticate_request_token.php @@ -0,0 +1,23 @@ + + * @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); + +$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client); +$requestToken = $authenticationRepository->getRequestToken(); + +// Will force a redirect +$authenticationRepository->authenticateRequestToken($requestToken); diff --git a/examples/authentication/model/guest_session_token.php b/examples/authentication/model/guest_session_token.php new file mode 100644 index 00000000..33e4ecf5 --- /dev/null +++ b/examples/authentication/model/guest_session_token.php @@ -0,0 +1,22 @@ + + * @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); + +$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client); +$sessionToken = $authenticationRepository->getGuestSessionToken($requestToken); + +var_dump($sessionToken); diff --git a/examples/authentication/model/request_token.php b/examples/authentication/model/request_token.php new file mode 100644 index 00000000..6685084f --- /dev/null +++ b/examples/authentication/model/request_token.php @@ -0,0 +1,23 @@ + + * @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); + +$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client); +$requestToken = $authenticationRepository->getRequestToken(); + +var_dump($requestToken); + diff --git a/examples/authentication/model/session_token.php b/examples/authentication/model/session_token.php new file mode 100644 index 00000000..00e28599 --- /dev/null +++ b/examples/authentication/model/session_token.php @@ -0,0 +1,25 @@ + + * @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); + +$requestToken = new \Tmdb\RequestToken('2e57316025d7e4df5cfff81f0596209c2465a8bd'); + +$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client); +$sessionToken = $authenticationRepository->getSessionToken($requestToken); + +var_dump($sessionToken); + diff --git a/lib/Tmdb/Factory/AuthenticationFactory.php b/lib/Tmdb/Factory/AuthenticationFactory.php new file mode 100644 index 00000000..3cf77797 --- /dev/null +++ b/lib/Tmdb/Factory/AuthenticationFactory.php @@ -0,0 +1,113 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Factory; + +use Tmdb\Exception\NotImplementedException; +use Tmdb\RequestToken; +use Tmdb\SessionToken; + +class AuthenticationFactory extends AbstractFactory +{ + /** + * @param array $data + * + * @throws NotImplementedException + * @return void + */ + public function create(array $data = array()) + { + throw new NotImplementedException(); + } + + /** + * @param array $data + * + * @throws NotImplementedException + * @return void + */ + public function createCollection(array $data = array()) + { + throw new NotImplementedException(); + } + + /** + * Create request token + * + * @param array $data + * @return RequestToken + */ + public function createRequestToken(array $data = array()) + { + $token = new RequestToken(); + + if (array_key_exists('expires_at', $data)) { + $token->setExpiresAt(new \DateTime($data['expires_at'])); + } + + if (array_key_exists('request_token', $data)) { + $token->setToken($data['request_token']); + } + + if (array_key_exists('success', $data)) { + $token->setSuccess($data['success']); + } + + return $token; + } + + /** + * Create session token for user + * + * @param array $data + * @return SessionToken + */ + public function createSessionToken(array $data = array()) + { + $token = new SessionToken(); + + if (array_key_exists('session_id', $data)) { + $token->setToken($data['session_id']); + } + + if (array_key_exists('success', $data)) { + $token->setSuccess($data['success']); + } + + return $token; + } + + /** + * Create session token for guest + * + * @param array $data + * @return SessionToken + */ + public function createGuestSessionToken(array $data = array()) + { + $token = new SessionToken(); + + if (array_key_exists('expires_at', $data)) { + $token->setExpiresAt(new \DateTime($data['expires_at'])); + } + + if (array_key_exists('guest_session_id', $data)) { + $token->setToken($data['guest_session_id']); + } + + if (array_key_exists('success', $data)) { + $token->setSuccess($data['success']); + } + + return $token; + } +} diff --git a/lib/Tmdb/Repository/AuthenticationRepository.php b/lib/Tmdb/Repository/AuthenticationRepository.php new file mode 100644 index 00000000..9c8d6fae --- /dev/null +++ b/lib/Tmdb/Repository/AuthenticationRepository.php @@ -0,0 +1,94 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Repository; + +use Tmdb\Factory\AuthenticationFactory; +use Tmdb\RequestToken; + +class AuthenticationRepository extends AbstractRepository { + + /** + * This method is used to generate a valid request token for user based authentication. + * A request token is required in order to request a session id. + * + * You can generate any number of request tokens but they will expire after 60 minutes. + * As soon as a valid session id has been created the token will be destroyed. + * + * @return RequestToken + */ + public function getRequestToken() + { + $data = $this->getApi()->getNewToken(); + return $this->getFactory()->createRequestToken($data); + } + + /** + * This method is used to generate a session id for user based authentication. + * A session id is required in order to use any of the write methods. + * + * @param RequestToken $requestToken + * @return RequestToken + */ + public function getSessionToken(RequestToken $requestToken) + { + $data = $this->getApi()->getNewSession($requestToken->getToken()); + return $this->getFactory()->createSessionToken($data); + } + + /** + * This method is used to generate a guest session id. + * + * A guest session can be used to rate movies without having a registered TMDb user account. + * You should only generate a single guest session per user (or device) as you will be able to + * attach the ratings to a TMDb user account in the future. + * + * There is also IP limits in place so you should always make sure it's the end user doing the guest session actions. + * If a guest session is not used for the first time within 24 hours, it will be automatically discarded. + * + * @return RequestToken + */ + public function getGuestSessionToken() + { + $data = $this->getApi()->getNewGuestSession(); + return $this->getFactory()->createGuestSessionToken($data); + } + + /** + * Authenticate request token, redirects the user + * + * @param RequestToken $requestToken + * @return void + */ + public function authenticateRequestToken(RequestToken $requestToken) + { + $this->getApi()->authenticateRequestToken($requestToken->getToken()); + } + + /** + * Return the Collection API Class + * + * @return \Tmdb\Api\Authentication + */ + public function getApi() + { + return $this->getClient()->getAuthenticationApi(); + } + + /** + * @return AuthenticationFactory + */ + public function getFactory() + { + return new AuthenticationFactory(); + } +} diff --git a/lib/Tmdb/RequestToken.php b/lib/Tmdb/RequestToken.php index 551ce954..5c1215a6 100644 --- a/lib/Tmdb/RequestToken.php +++ b/lib/Tmdb/RequestToken.php @@ -27,6 +27,11 @@ class RequestToken { */ private $expiresAt; + /** + * @var bool + */ + private $success; + /** * Token bag * @@ -76,4 +81,22 @@ public function getExpiresAt() { return $this->expiresAt; } + + /** + * @param boolean $success + * @return $this + */ + public function setSuccess($success) + { + $this->success = $success; + return $this; + } + + /** + * @return boolean + */ + public function getSuccess() + { + return $this->success; + } } diff --git a/lib/Tmdb/SessionToken.php b/lib/Tmdb/SessionToken.php index 19283f00..7ef9b638 100644 --- a/lib/Tmdb/SessionToken.php +++ b/lib/Tmdb/SessionToken.php @@ -13,8 +13,21 @@ namespace Tmdb; class SessionToken { + /** + * @var string + */ private $sessionToken = null; + /** + * @var \DateTime + */ + private $expiresAt; + + /** + * @var bool + */ + private $success; + /** * Token bag * @@ -42,4 +55,44 @@ public function getToken() { return $this->sessionToken; } + + /** + * @param \DateTime $expiresAt + * @return $this + */ + public function setExpiresAt($expiresAt) + { + if (!$expiresAt instanceof \DateTime) { + $expiresAt = new \DateTime($expiresAt); + } + + $this->expiresAt = $expiresAt; + return $this; + } + + /** + * @return \DateTime + */ + public function getExpiresAt() + { + return $this->expiresAt; + } + + /** + * @param mixed $success + * @return $this + */ + public function setSuccess($success) + { + $this->success = $success; + return $this; + } + + /** + * @return mixed + */ + public function getSuccess() + { + return $this->success; + } } diff --git a/test/Tmdb/Tests/Repository/AccountRepositoryTest.php b/test/Tmdb/Tests/Repository/AccountRepositoryTest.php new file mode 100644 index 00000000..33571324 --- /dev/null +++ b/test/Tmdb/Tests/Repository/AccountRepositoryTest.php @@ -0,0 +1,126 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +use Tmdb\Api\Account; +use Tmdb\Model\Movie; +use Tmdb\Repository\AccountRepository; + +class AccountRepositoryTest extends TestCase +{ + const ACCOUNT_ID = '12345'; + const LIST_ID = '509ec17b19c2950a0600050d'; + const MOVIE_ID = 150; + + /** + * @test + */ + public function shouldGetAccount() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getAccount(); + } + + /** + * @test + */ + public function shouldGetLists() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getLists(self::ACCOUNT_ID); + } + + /** + * @test + */ + public function shouldGetFavoriteMovies() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getFavoriteMovies(self::ACCOUNT_ID); + } + + /** + * @test + */ + public function shouldFavorite() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->favorite(self::ACCOUNT_ID, self::MOVIE_ID, true); + } + + /** + * @test + */ + public function shouldFavoriteMovieObject() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $movie = new Movie(); + $movie->setId(self::MOVIE_ID); + + $repository->favorite(self::ACCOUNT_ID, $movie, true); + } + + /** + * @test + */ + public function shouldGetRatedMovies() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getRatedMovies(self::ACCOUNT_ID); + } + + /** + * @test + */ + public function shouldWatchlist() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->watchlist(self::ACCOUNT_ID, self::MOVIE_ID, true); + } + + /** + * @test + */ + public function shouldWatchlistMovieObject() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $movie = new Movie(); + $movie->setId(self::MOVIE_ID); + + $repository->watchlist(self::ACCOUNT_ID, $movie, true); + } + + /** + * @return Account + */ + protected function getApiClass() + { + return 'Tmdb\Api\Account'; + } + + /** + * @return AccountRepository + */ + protected function getRepositoryClass() + { + return 'Tmdb\Repository\AccountRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/AuthenticationRepositoryTest.php b/test/Tmdb/Tests/Repository/AuthenticationRepositoryTest.php new file mode 100644 index 00000000..d2c6995e --- /dev/null +++ b/test/Tmdb/Tests/Repository/AuthenticationRepositoryTest.php @@ -0,0 +1,70 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +use Tmdb\Api\Authentication; +use Tmdb\Model\Movie; +use Tmdb\Repository\AuthenticationRepository; +use Tmdb\RequestToken; +use Tmdb\SessionToken; + +class AuthenticationRepositoryTest extends TestCase +{ + /** + * @test + */ + public function shouldGetRequestToken() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getRequestToken(); + } + + /** + * @test + */ + public function shouldGetSessionToken() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $token = new RequestToken('test'); + + $repository->getSessionToken($token); + } + + /** + * @test + */ + public function shouldGetGuestSession() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getGuestSessionToken(); + } + + /** + * @return Authentication + */ + protected function getApiClass() + { + return 'Tmdb\Api\Authentication'; + } + + /** + * @return AuthenticationRepository + */ + protected function getRepositoryClass() + { + return 'Tmdb\Repository\AuthenticationRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/RequestTokenTest.php b/test/Tmdb/Tests/RequestTokenTest.php index 28187081..90608004 100644 --- a/test/Tmdb/Tests/RequestTokenTest.php +++ b/test/Tmdb/Tests/RequestTokenTest.php @@ -22,8 +22,10 @@ public function testSetGet() $token = new \Tmdb\RequestToken(); $token->setToken(self::REQUEST_TOKEN); $token->setExpiresAt('2012-02-09 19:50:25 UTC'); + $token->setSuccess(true); $this->assertEquals(self::REQUEST_TOKEN, $token->getToken()); $this->assertInstanceOf('DateTime', $token->getExpiresAt()); + $this->assertEquals(true, $token->getSuccess()); } } diff --git a/test/Tmdb/Tests/SessionTokenTest.php b/test/Tmdb/Tests/SessionTokenTest.php index 1822902f..2a9d0836 100644 --- a/test/Tmdb/Tests/SessionTokenTest.php +++ b/test/Tmdb/Tests/SessionTokenTest.php @@ -21,7 +21,11 @@ public function testSetGet() { $token = new \Tmdb\SessionToken(); $token->setToken(self::SESSION_TOKEN); + $token->setExpiresAt('2012-12-04 22:51:19 UTC'); + $token->setSuccess(true); $this->assertEquals(self::SESSION_TOKEN, $token->getToken()); + $this->assertEquals('04-12-2012', $token->getExpiresAt()->format('d-m-Y')); + $this->assertEquals(true, $token->getSuccess()); } }