Skip to content

Commit

Permalink
Added RequestToken in the API and preparing modelling.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 24, 2014
1 parent 41524a5 commit a8c2cc4
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
21 changes: 21 additions & 0 deletions examples/authentication/api/get.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
*/
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);
18 changes: 15 additions & 3 deletions lib/Tmdb/Api/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,36 @@
*/
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.
*
* 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)
));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function setHttpClient(HttpClientInterface $httpClient)
*
* @return string
*/
private function getBaseUrl()
public function getBaseUrl()
{
return sprintf(
'%s:%s',
Expand Down
79 changes: 79 additions & 0 deletions lib/Tmdb/RequestToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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 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;
}
}
1 change: 0 additions & 1 deletion test/Tmdb/Tests/Api/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class AuthenticationTest extends TestCase
{
/**
* @test
* @expectedException Tmdb\Exception\NotImplementedException
*/
public function shouldGetNewToken()
{
Expand Down
29 changes: 29 additions & 0 deletions test/Tmdb/Tests/RequestTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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
*/
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());
}
}

0 comments on commit a8c2cc4

Please sign in to comment.