Skip to content

Commit

Permalink
Adding the rest of Account related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 26, 2014
1 parent 53f109c commit e62f084
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/authentication/model/authenticate_request_token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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);

$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client);
$requestToken = $authenticationRepository->getRequestToken();

// Will force a redirect
$authenticationRepository->authenticateRequestToken($requestToken);
22 changes: 22 additions & 0 deletions examples/authentication/model/guest_session_token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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);

$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client);
$sessionToken = $authenticationRepository->getGuestSessionToken($requestToken);

var_dump($sessionToken);
23 changes: 23 additions & 0 deletions examples/authentication/model/request_token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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);

$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client);
$requestToken = $authenticationRepository->getRequestToken();

var_dump($requestToken);

25 changes: 25 additions & 0 deletions examples/authentication/model/session_token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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 = new \Tmdb\RequestToken('2e57316025d7e4df5cfff81f0596209c2465a8bd');

$authenticationRepository = new \Tmdb\Repository\AuthenticationRepository($client);
$sessionToken = $authenticationRepository->getSessionToken($requestToken);

var_dump($sessionToken);

113 changes: 113 additions & 0 deletions lib/Tmdb/Factory/AuthenticationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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;

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;
}
}
94 changes: 94 additions & 0 deletions lib/Tmdb/Repository/AuthenticationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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\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();
}
}
23 changes: 23 additions & 0 deletions lib/Tmdb/RequestToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class RequestToken {
*/
private $expiresAt;

/**
* @var bool
*/
private $success;

/**
* Token bag
*
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit e62f084

Please sign in to comment.