Skip to content

Commit

Permalink
refactor(improvement): relocate throw exception
Browse files Browse the repository at this point in the history
  • Loading branch information
achmadhadikurnia committed Jul 7, 2024
1 parent 854b543 commit 07094dc
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Commands/GenerateTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function handle(): int
}

$this->info(json_encode([
'sso' => $ssoToken,
'apim' => $apimToken,
'sso' => $ssoToken,
], JSON_PRETTY_PRINT));

return self::SUCCESS;
Expand Down
4 changes: 1 addition & 3 deletions src/Contracts/Tokenize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Kanekescom\Siasn\Api\Contracts;

use Illuminate\Http\Client\Response;

interface Tokenize
{
public static function getToken(): Response;
public static function getToken(): object;
}
14 changes: 11 additions & 3 deletions src/Credentials/Apim.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace Kanekescom\Siasn\Api\Credentials;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Kanekescom\Siasn\Api\Contracts\Tokenize;
use Kanekescom\Siasn\Api\Exceptions\InvalidApimCredentialsException;
use Kanekescom\Siasn\Api\Exceptions\InvalidTokenException;
use Kanekescom\Siasn\Api\Helpers\Config;

class Apim implements Tokenize
{
/**
* @throws InvalidApimCredentialsException|ConnectionException
*/
public static function getToken(): Response
public static function getToken(): object
{
$credential = Config::getApimCredential();

Expand All @@ -26,7 +26,7 @@ public static function getToken(): Response
throw new InvalidApimCredentialsException('password must be set');
}

return Http::timeout(config('siasn-api.request_timeout'))
$response = Http::timeout(config('siasn-api.request_timeout'))
->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts'))
->withOptions([
'debug' => Config::getDebug(),
Expand All @@ -37,5 +37,13 @@ public static function getToken(): Response
)->post($credential->url, [
'grant_type' => $credential->grant_type,
]);

$token = $response->object();

if (blank(optional($token)->access_token)) {
throw new InvalidTokenException('Not receiving tokens correctly.');
}

return $token;
}
}
14 changes: 11 additions & 3 deletions src/Credentials/Sso.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace Kanekescom\Siasn\Api\Credentials;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Kanekescom\Siasn\Api\Contracts\Tokenize;
use Kanekescom\Siasn\Api\Exceptions\InvalidSsoCredentialsException;
use Kanekescom\Siasn\Api\Exceptions\InvalidTokenException;
use Kanekescom\Siasn\Api\Helpers\Config;

class Sso implements Tokenize
{
/**
* @throws InvalidSsoCredentialsException|ConnectionException
*/
public static function getToken(): Response
public static function getToken(): object
{
$credential = Config::getSsoCredential();

Expand All @@ -30,7 +30,7 @@ public static function getToken(): Response
throw new InvalidSsoCredentialsException('password must be set');
}

return Http::timeout(config('siasn-api.request_timeout'))
$response = Http::timeout(config('siasn-api.request_timeout'))
->asForm()
->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts'))
->withOptions([
Expand All @@ -43,5 +43,13 @@ public static function getToken(): Response
'username' => $credential->username,
'password' => $credential->password,
]);

$token = $response->object();

if (blank(optional($token)->token_type) || blank(optional($token)->access_token)) {
throw new InvalidTokenException('Not receiving tokens correctly.');
}

return $token;
}
}
18 changes: 2 additions & 16 deletions src/Credentials/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,21 @@

namespace Kanekescom\Siasn\Api\Credentials;

use Kanekescom\Siasn\Api\Exceptions\InvalidApimCredentialsException;
use Kanekescom\Siasn\Api\Exceptions\InvalidSsoCredentialsException;
use Kanekescom\Siasn\Api\Helpers\Config;

class Token
{
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;
return Apim::getToken();
});
}

public static function getSsoToken(): object
{
return cache()->remember('sso-token', Config::getSsoTokenAge(), function () {
$ssoToken = Sso::getToken()->object();

if (blank(optional($ssoToken)->token_type) || blank(optional($ssoToken)->access_token)) {
throw new InvalidSsoCredentialsException('Invalid SSO user credentials.');
}

return $ssoToken;
return Sso::getToken();
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/InvalidTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Kanekescom\Siasn\Api\Exceptions;

use Exception;

class InvalidTokenException extends Exception
{
//
}
20 changes: 16 additions & 4 deletions tests/GenerateTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use Kanekescom\Siasn\Api\Credentials\Token;

it('can generate apim token', function () {
$apimToken = Apim::getToken()->object();
$apimToken = Apim::getToken();

expect($apimToken)->toHaveProperty('access_token');
});

it('can generate sso token', function () {
$ssoToken = Sso::getToken()->object();
$ssoToken = Sso::getToken();

expect($ssoToken)->toHaveProperty('access_token');
});
Expand All @@ -22,12 +22,24 @@
expect($apimToken)->toHaveProperty('access_token');
});

it('can clear cache and generate apim token cache first', function () {
$apimToken = Token::getNewApimToken();

expect($apimToken)->toHaveProperty('access_token');
});

it('can generate sso token cache first', function () {
$ssoToken = Token::getSsoToken();

expect($ssoToken)->toHaveProperty('access_token');
});

it('can clear cache and generate sso token cache first', function () {
$ssoToken = Token::getNewSsoToken();

expect($ssoToken)->toHaveProperty('access_token');
});

it('can generate apim token same on cache', function () {
$apimToken = Token::getApimToken();
$cachedApimToken = cache('apim-token');
Expand All @@ -43,14 +55,14 @@
});

it('can generate apim token not same on cache', function () {
$apimTokenObject = Apim::getToken()->object();
$apimTokenObject = Apim::getToken();
$apimToken = Token::getApimToken();

expect($apimTokenObject)->not()->toBe($apimToken);
});

it('can generate sso token not same on cache', function () {
$ssoTokenObject = Sso::getToken()->object();
$ssoTokenObject = Sso::getToken();
$ssoToken = Token::getSsoToken();

expect($ssoTokenObject)->not()->toBe($ssoToken);
Expand Down

0 comments on commit 07094dc

Please sign in to comment.