first commit

This commit is contained in:
envoyr committed 2025-04-27 04:02:46 +02:00
commit 05e8cca347
47 files changed
+2723

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
<?php
namespace Anikeen\Id\Repository;
use Anikeen\Id\AnikeenId;
use Anikeen\Id\Contracts\AppTokenRepository as Repository;
use Anikeen\Id\Exceptions\RequestFreshAccessTokenException;
use Illuminate\Support\Facades\Cache;
class AppTokenRepository implements Repository
{
public const ACCESS_TOKEN_CACHE_KEY = 'anikeen-id:access_token';
private AnikeenId $client;
public function __construct()
{
$this->client = app(AnikeenId::class);
}
/**
* {@inheritDoc}
*/
public function getAccessToken(): string
{
$accessToken = Cache::get(self::ACCESS_TOKEN_CACHE_KEY);
if ($accessToken) {
return $accessToken;
}
return $this->requestFreshAccessToken('*');
}
/**
* @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;
}
}