From bc74815be1e0fb9adeaa27fd8ed7a99a887e77d5 Mon Sep 17 00:00:00 2001 From: Achmad Hadi Kurnia Date: Sun, 7 Jul 2024 22:00:45 +0700 Subject: [PATCH] feat(new): new getNewApimToken() and getNewSsoToken method in Token class --- src/Commands/GenerateApimTokenCommand.php | 3 +-- src/Commands/GenerateSsoTokenCommand.php | 3 +-- src/Commands/GenerateTokenCommand.php | 6 ++--- src/Credentials/Token.php | 32 ++++++++++++++++------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Commands/GenerateApimTokenCommand.php b/src/Commands/GenerateApimTokenCommand.php index 6d5e143..73cf1cf 100644 --- a/src/Commands/GenerateApimTokenCommand.php +++ b/src/Commands/GenerateApimTokenCommand.php @@ -3,7 +3,6 @@ namespace Kanekescom\Siasn\Api\Commands; use Illuminate\Console\Command; -use Kanekescom\Siasn\Api\Credentials\Apim; use Kanekescom\Siasn\Api\Credentials\Token; class GenerateApimTokenCommand extends Command @@ -16,7 +15,7 @@ class GenerateApimTokenCommand extends Command public function handle(): int { if ($this->option('fresh')) { - $token = Apim::getToken()->object(); + $token = Token::getNewApimToken(); } else { $token = Token::getApimToken(); } diff --git a/src/Commands/GenerateSsoTokenCommand.php b/src/Commands/GenerateSsoTokenCommand.php index 1a30302..275eac4 100644 --- a/src/Commands/GenerateSsoTokenCommand.php +++ b/src/Commands/GenerateSsoTokenCommand.php @@ -3,7 +3,6 @@ namespace Kanekescom\Siasn\Api\Commands; use Illuminate\Console\Command; -use Kanekescom\Siasn\Api\Credentials\Sso; use Kanekescom\Siasn\Api\Credentials\Token; class GenerateSsoTokenCommand extends Command @@ -16,7 +15,7 @@ class GenerateSsoTokenCommand extends Command public function handle(): int { if ($this->option('fresh')) { - $token = Sso::getToken()->object(); + $token = Token::getNewSsoToken(); } else { $token = Token::getSsoToken(); } diff --git a/src/Commands/GenerateTokenCommand.php b/src/Commands/GenerateTokenCommand.php index 6787d6f..c098122 100644 --- a/src/Commands/GenerateTokenCommand.php +++ b/src/Commands/GenerateTokenCommand.php @@ -3,8 +3,6 @@ namespace Kanekescom\Siasn\Api\Commands; use Illuminate\Console\Command; -use Kanekescom\Siasn\Api\Credentials\Apim; -use Kanekescom\Siasn\Api\Credentials\Sso; use Kanekescom\Siasn\Api\Credentials\Token; class GenerateTokenCommand extends Command @@ -17,8 +15,8 @@ class GenerateTokenCommand extends Command public function handle(): int { if ($this->option('fresh')) { - $apimToken = Apim::getToken()->object(); - $ssoToken = Sso::getToken()->object(); + $apimToken = Token::getNewApimToken(); + $ssoToken = Token::getNewSsoToken(); } else { $apimToken = Token::getApimToken(); $ssoToken = Token::getSsoToken(); diff --git a/src/Credentials/Token.php b/src/Credentials/Token.php index cf7e6d3..bf3e038 100644 --- a/src/Credentials/Token.php +++ b/src/Credentials/Token.php @@ -8,7 +8,20 @@ class Token { - public static function getSsoToken() + public static function getApimToken(): object + { + return cache()->remember('apim-token', Config::getApimTokenAge(), function () { + $apimToken = Apim::getToken()->object(); + + if (blank(optional($apimToken)->access_token)) { + throw new InvalidApimCredentialsException('Invalid Apim user credentials.'); + } + + return $apimToken; + }); + } + + public static function getSsoToken(): object { return cache()->remember('sso-token', Config::getSsoTokenAge(), function () { $ssoToken = Sso::getToken()->object(); @@ -21,17 +34,18 @@ public static function getSsoToken() }); } - public static function getApimToken() + public static function getNewApimToken(): object { - return cache()->remember('apim-token', Config::getApimTokenAge(), function () { - $apimToken = Apim::getToken()->object(); + cache()->forget('apim-token'); - if (blank(optional($apimToken)->access_token)) { - throw new InvalidApimCredentialsException('Invalid Apim user credentials.'); - } + return self::getApimToken(); + } - return $apimToken; - }); + public static function getNewSsoToken(): object + { + cache()->forget('sso-token'); + + return self::getSsoToken(); } public static function forget(): void