Google - v1.0.0
TokenManager
ArtisanPackUI\Google\Tokens\TokenManager returns valid access tokens, refreshing transparently when the current one is close to expiring. Fully covered in Tokens; this page is the API reference.
Signature
namespace ArtisanPackUI\Google\Tokens;
class TokenManager
{
public function __construct(
protected ConfigurationRepository $config,
protected ConfigRepository $laravelConfig,
protected HttpFactory $http,
);
public function getValidAccessToken( GoogleConnection $connection ): string;
public function refresh( GoogleConnection $connection ): string;
}
Methods
getValidAccessToken( GoogleConnection $connection ): string
Return a valid access token. Refreshes if the current one is expired or missing.
Behavior:
- If
! $connection->isConnected()→ throwsTokenRefreshException. - If
! $connection->isExpired() && ! empty( $connection->access_token )→ returns the stored token. - Otherwise → delegates to
refresh()and returns the fresh token.
Throws TokenRefreshException on refresh failure.
refresh( GoogleConnection $connection ): string
Force a refresh regardless of expiry. POSTs to google.endpoints.token with the stored refresh token.
Behavior:
- Missing refresh token →
markDisconnected('Missing refresh token.')and throw. - Non-success response — parse
errorfrom the body:invalid_grant→markDisconnected('Refresh token revoked or expired.')and throw.- Anything else → throw without changing the connection status.
- Success → update
access_token,token_type,expires_at, and (if present)refresh_tokenandscopes. Save and return the new access token.
Refresh window
The isExpired() check on the connection treats tokens as expired 60 seconds before expires_at, so refresh happens proactively.
HTTP client
The manager resolves Illuminate\Http\Client\Factory from the container. Http::fake() in tests transparently intercepts calls — see Tokens → Testing.
Related
- Tokens — usage, failure modes, retry patterns.
TokenRefreshException.