mirror of
https://github.com/anikeen-com/id.git
synced 2026-11-03 06:09:31 +00:00
first commit
This commit is contained in:
47 files changed
+2723
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user