mirror of
https://github.com/bitinflow/accounts.git
synced 2026-03-14 22:15:52 +00:00
Add app token repository
This commit is contained in:
61
src/Accounts/Repository/AppTokenRepository.php
Normal file
61
src/Accounts/Repository/AppTokenRepository.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Repository;
|
||||
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Contracts\AppTokenRepository as Repository;
|
||||
use Bitinflow\Accounts\Exceptions\RequestFreshAccessTokenException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class AppTokenRepository implements Repository
|
||||
{
|
||||
public const ACCESS_TOKEN_CACHE_KEY = 'bitinflow-accounts:access_token';
|
||||
|
||||
private BitinflowAccounts $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = app(BitinflowAccounts::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
$accessToken = Cache::get(self::ACCESS_TOKEN_CACHE_KEY);
|
||||
|
||||
if ($accessToken) {
|
||||
return $accessToken;
|
||||
}
|
||||
|
||||
return $this->requestFreshAccessToken('*');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $scope
|
||||
*
|
||||
* @throws RequestFreshAccessTokenException
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function requestFreshAccessToken(string $scope)
|
||||
{
|
||||
$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(): BitinflowAccounts
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user