Skip to content

Commit

Permalink
added a tokenUpdate callback
Browse files Browse the repository at this point in the history
  • Loading branch information
justijndepover committed Nov 1, 2021
1 parent 075af45 commit c27a837
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
16 changes: 14 additions & 2 deletions laravel-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class TeamleaderServiceProvider extends ServiceProvider implements DeferrablePro
config('services.teamleader.state'),
);

$teamleader->setTokenUpdateCallback(function ($teamleader) {
Storage::disk('local')->put('teamleader.json', json_encode([
'accessToken' => $teamleader->getAccessToken(),
'refreshToken' => $teamleader->getRefreshToken(),
'expiresAt' => $teamleader->getTokenExpiresAt(),
]));
});

if (Storage::exists('teamleader.json') && $json = Storage::get('teamleader.json')) {
try {
$json = json_decode($json);
Expand All @@ -70,13 +78,17 @@ class TeamleaderServiceProvider extends ServiceProvider implements DeferrablePro
}

if (! empty($teamleader->getRefreshToken()) && $teamleader->shouldRefreshToken()) {
$teamleader->connect();
try {
$teamleader->connect();
} catch (\Throwable $th) {
$teamleader->setRefreshToken('');

Storage::disk('local')->put('teamleader.json', json_encode([
Storage::disk('local')->put('teamleader.json', json_encode([
'accessToken' => $teamleader->getAccessToken(),
'refreshToken' => $teamleader->getRefreshToken(),
'expiresAt' => $teamleader->getTokenExpiresAt(),
]));
}
}

return $teamleader;
Expand Down
21 changes: 20 additions & 1 deletion src/Teamleader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class Teamleader
*/
private $clientSecret;

/**
* @var Client|null
*/
private $client;

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

/**
* @var string
*/
Expand Down Expand Up @@ -219,6 +229,11 @@ public function connect(): void
}
}

public function setTokenUpdateCallback(callable $callback): void
{
$this->tokenUpdateCallback = $callback;
}

public function get(string $endpoint, array $parameters = [])
{
try {
Expand Down Expand Up @@ -249,7 +264,7 @@ public function post(string $endpoint, array $body, array $parameters = [])
}
}

public function ensureRateLimitingIsNotExceeded() : void
public function ensureRateLimitingIsNotExceeded(): void
{
if ($this->rateLimitRemaining <= 1) {
$seconds = Carbon::createFromFormat('Y-m-d\TH:i:sT', $this->rateLimitReset, 'UTC')->diffInSeconds();
Expand Down Expand Up @@ -379,6 +394,10 @@ private function acquireAccessToken(): void
$this->accessToken = $body['access_token'];
$this->refreshToken = $body['refresh_token'];
$this->tokenExpiresAt = time() + $body['expires_in'];

if (is_callable($this->tokenUpdateCallback)) {
call_user_func($this->tokenUpdateCallback, $this);
}
} catch (ClientException $e) {
$response = json_decode($e->getResponse()->getBody()->getContents());
throw CouldNotAquireAccessTokenException::make($response->errors[0]->status, $response->errors[0]->title);
Expand Down

0 comments on commit c27a837

Please sign in to comment.