Skip to content

Commit

Permalink
Bust access token cache when updating Credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
jlevers committed Aug 19, 2023
1 parent 53e38cf commit 7b77178
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Models/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ public function getExpiresAtCacheKey(): string
return "spapi:access_token_expiration:{$this->id}";
}

/**
* Remove any cached access token info.
*
* @return static
*/
public function bustCache(): static
{
Cache::forget($this->getAccessTokenCacheKey());
Cache::forget($this->getExpiresAtCacheKey());
$this->access_token = null;

return $this;
}

/**
* Get the Seller that owns the Credentials.
*
Expand Down Expand Up @@ -216,4 +230,20 @@ protected function _getExpiresAt(): int

return $this->expires_at;
}

/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
// Bust the cache when the model is updated, in case the access token
// is no longer valid for the updated credentials.
static::updating(function (self $credentials) {
$credentials->bustCache();
$credentials->access_token = null;
$credentials->expires_at = null;
});
}
}

0 comments on commit 7b77178

Please sign in to comment.