From a8c2cc4678c16e2cf4e6c56aa7a28ca4a9a117b4 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Mon, 24 Feb 2014 23:38:51 +0100 Subject: [PATCH] Added RequestToken in the API and preparing modelling. --- examples/authentication/api/get.php | 21 ++++++ lib/Tmdb/Api/Authentication.php | 18 ++++- lib/Tmdb/Client.php | 2 +- lib/Tmdb/RequestToken.php | 79 ++++++++++++++++++++++ test/Tmdb/Tests/Api/AuthenticationTest.php | 1 - test/Tmdb/Tests/RequestTokenTest.php | 29 ++++++++ 6 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 examples/authentication/api/get.php create mode 100644 lib/Tmdb/RequestToken.php create mode 100644 test/Tmdb/Tests/RequestTokenTest.php diff --git a/examples/authentication/api/get.php b/examples/authentication/api/get.php new file mode 100644 index 00000000..5f9ec2fe --- /dev/null +++ b/examples/authentication/api/get.php @@ -0,0 +1,21 @@ + + * @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 = $client->getAuthenticationApi()->getNewToken(); + +var_dump($requestToken); \ No newline at end of file diff --git a/lib/Tmdb/Api/Authentication.php b/lib/Tmdb/Api/Authentication.php index 3552404e..34a3625f 100644 --- a/lib/Tmdb/Api/Authentication.php +++ b/lib/Tmdb/Api/Authentication.php @@ -12,11 +12,14 @@ */ namespace Tmdb\Api; +use Tmdb\Client; use Tmdb\Exception\NotImplementedException; class Authentication extends AbstractApi { + const REQUEST_TOKEN_URI = 'https://www.themoviedb.org/authenticate/%s'; + /** * 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. @@ -24,12 +27,21 @@ class Authentication * 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. * - * @throws NotImplementedException + * @param array $parameters + * @param array $headers * @return mixed */ - public function getNewToken() + public function getNewToken($parameters = array(), $headers = array()) { - throw new NotImplementedException(__METHOD__ . ' has not been implemented yet.'); + return $this->get('authentication/token/new', $parameters, $headers); + } + + public function authenticateRequestToken($token) + { + header(sprintf( + 'Location: %s/%s', + sprintf(self::REQUEST_TOKEN_URI, $token) + )); } /** diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php index 5f5498ae..f4bbc0f8 100644 --- a/lib/Tmdb/Client.php +++ b/lib/Tmdb/Client.php @@ -303,7 +303,7 @@ public function setHttpClient(HttpClientInterface $httpClient) * * @return string */ - private function getBaseUrl() + public function getBaseUrl() { return sprintf( '%s:%s', diff --git a/lib/Tmdb/RequestToken.php b/lib/Tmdb/RequestToken.php new file mode 100644 index 00000000..551ce954 --- /dev/null +++ b/lib/Tmdb/RequestToken.php @@ -0,0 +1,79 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb; + +class RequestToken { + /** + * The token for obtaining a session + * + * @var string + */ + private $token = null; + + /** + * Expiry date UTC + * + * @var + */ + private $expiresAt; + + /** + * Token bag + * + * @param $request_token + */ + public function __construct($request_token = null) + { + $this->token = $request_token; + } + + /** + * @param null $token + * @return $this + */ + public function setToken($token) + { + $this->token = $token; + return $this; + } + + /** + * @return null + */ + public function getToken() + { + return $this->token; + } + + /** + * @param mixed $expiresAt + * @return $this + */ + public function setExpiresAt($expiresAt) + { + if (!$expiresAt instanceof \DateTime) { + $expiresAt = new \DateTime($expiresAt); + } + + $this->expiresAt = $expiresAt; + return $this; + } + + /** + * @return mixed + */ + public function getExpiresAt() + { + return $this->expiresAt; + } +} diff --git a/test/Tmdb/Tests/Api/AuthenticationTest.php b/test/Tmdb/Tests/Api/AuthenticationTest.php index 49bb1ae2..62a87b9b 100644 --- a/test/Tmdb/Tests/Api/AuthenticationTest.php +++ b/test/Tmdb/Tests/Api/AuthenticationTest.php @@ -16,7 +16,6 @@ class AuthenticationTest extends TestCase { /** * @test - * @expectedException Tmdb\Exception\NotImplementedException */ public function shouldGetNewToken() { diff --git a/test/Tmdb/Tests/RequestTokenTest.php b/test/Tmdb/Tests/RequestTokenTest.php new file mode 100644 index 00000000..28187081 --- /dev/null +++ b/test/Tmdb/Tests/RequestTokenTest.php @@ -0,0 +1,29 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +class RequestTokenTest extends \PHPUnit_Framework_TestCase +{ + const REQUEST_TOKEN = '641bf16c663db167c6cffcdff41126039d4445bf'; + + /** + * @test + */ + public function testSetGet() + { + $token = new \Tmdb\RequestToken(); + $token->setToken(self::REQUEST_TOKEN); + $token->setExpiresAt('2012-02-09 19:50:25 UTC'); + + $this->assertEquals(self::REQUEST_TOKEN, $token->getToken()); + $this->assertInstanceOf('DateTime', $token->getExpiresAt()); + } +}