Skip to content

Commit

Permalink
Merge pull request #55 from achmadhadikurnia/feature/add-new-method-t…
Browse files Browse the repository at this point in the history
…oken

feat(new): new getNewApimToken() and getNewSsoToken method in Token class
  • Loading branch information
achmadhadikurnia authored Jul 7, 2024
2 parents 9169a5e + bc74815 commit 3e54f68
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/Commands/GenerateApimTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/GenerateSsoTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
6 changes: 2 additions & 4 deletions src/Commands/GenerateTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
32 changes: 23 additions & 9 deletions src/Credentials/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit 3e54f68

Please sign in to comment.