Skip to content

Commit

Permalink
Merge pull request #449 from dataswitcher/master
Browse files Browse the repository at this point in the history
Add a callback to be able to catch refreshes from other threads
  • Loading branch information
stephangroen authored Jan 4, 2021
2 parents 7dbca25 + ba3d36a commit 4669343
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Picqer/Financials/Exact/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class Connection
*/
private $acquireAccessTokenUnlockCallback;

/**
* @var callable(Connection)
*/
private $refreshAccessTokenCallback;

/**
* @var callable[]
*/
Expand Down Expand Up @@ -484,6 +489,14 @@ private function acquireAccessToken()
call_user_func($this->acquireAccessTokenLockCallback, $this);
}

if (is_callable($this->refreshAccessTokenCallback)) {
call_user_func($this->refreshAccessTokenCallback, $this);
if (! $this->tokenHasExpired()) {
// the refreshed token has not expired, so we are fine to keep using it
return;
}
}

// If refresh token not yet acquired, do token request
if (empty($this->refreshToken)) {
$body = [
Expand Down Expand Up @@ -632,6 +645,14 @@ public function setTokenUpdateCallback($callback)
$this->tokenUpdateCallback = $callback;
}

/**
* @param callable $callback
*/
public function setRefreshAccessTokenCallback($callback)
{
$this->refreshAccessTokenCallback = $callback;
}

/**
* Parse the reponse in the Exception to return the Exact error messages.
*
Expand Down

0 comments on commit 4669343

Please sign in to comment.