Created
February 6, 2017 19:03
-
-
Save kevinruscoe/f31f15905f551cb7ebdda40bc45373b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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