client = app(AnikeenId::class); } /** * {@inheritDoc} */ public function getAccessToken(): string { $accessToken = Cache::get(self::ACCESS_TOKEN_CACHE_KEY); if ($accessToken) { return $accessToken; } return $this->requestFreshAccessToken('*'); } /** * Drop the cached token, e.g. after the ID service rejected it. */ public function forgetAccessToken(): void { Cache::forget(self::ACCESS_TOKEN_CACHE_KEY); } /** * @throws RequestFreshAccessTokenException */ private function requestFreshAccessToken(string $scope): mixed { $result = $this->getClient()->retrievingToken('client_credentials', [ 'scope' => $scope, ]); if (!$result->success()) { throw RequestFreshAccessTokenException::fromResponse($result->response()); } Cache::put(self::ACCESS_TOKEN_CACHE_KEY, $accessToken = $result->data()->access_token, now()->addWeek()); return $accessToken; } private function getClient(): AnikeenId { return $this->client; } }