Skip to content

Instantly share code, notes, and snippets.

@kevinruscoe
Created February 6, 2017 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinruscoe/f31f15905f551cb7ebdda40bc45373b2 to your computer and use it in GitHub Desktop.
Save kevinruscoe/f31f15905f551cb7ebdda40bc45373b2 to your computer and use it in GitHub Desktop.
<?php
class API {
private $token;
private $tokenExpiry;
public function __construct()
{
$this->deserializeToken();
if ($this->tokenNeedsUpdating()) {
$this->requestToken();
}
}
/**
* Pulls the token from the session into the object.
*
* @return null
**/
public function deserializeToken()
{
$this->token = session('token', false);
$this->tokenExpirey = session('tokenExpiry', false);
}
/**
* Returns whether or not the token needs updating.
*
* @return bool
**/
public function tokenNeedsUpdating()
{
if (!$this->token || !$this->tokenExpiry){
return true;
}
if (time() > $this->tokenExpiry){
return true;
}
return false;
}
/**
* Requests a new token with the give username/password, then deserializes it.
*
* @return null
**/
public function requestToken()
{
// do API call
// put in session
session('tokenExpire', $this->guessTokenExpiry());
$this->deserializeToken();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment