From 488447c70a8a1a97f66385e90267727b727065de Mon Sep 17 00:00:00 2001 From: Dan Garner Date: Sun, 18 Jul 2021 11:10:05 +0100 Subject: [PATCH] Method to set the access token on entity provider. --- src/Provider/XiboEntityProvider.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Provider/XiboEntityProvider.php b/src/Provider/XiboEntityProvider.php index f4beddd..f8762bf 100644 --- a/src/Provider/XiboEntityProvider.php +++ b/src/Provider/XiboEntityProvider.php @@ -67,6 +67,17 @@ public function getLogger() { return $this->logger; } + + /** + * Set the access token + * for example, if the access token has been stored in a data store, we should not have to get a new one + * @param $accessToken + * @return $this + */ + public function setAccessToken($accessToken) { + $this->token = $accessToken; + return $this; + } /** * Get Access Token @@ -75,17 +86,18 @@ public function getLogger() */ private function getAccessToken() { - if ($this->provider === null) + if ($this->provider === null) { throw new EmptyProviderException(); + } + $this->getLogger()->debug('Checking Access token and requesting a new one if necessary'); + if ($this->token == null || $this->token->hasExpired() || $this->token->getExpires() <= time() + 10) { // Get and store a new token - $this->getLogger()->info('Getting a new Access Token'); + $this->getLogger()->info('Getting a new Access Token'); $this->token = $this->provider->getAccessToken('client_credentials'); } - else { - $this->token = $this->token; - } + return $this->token; }