Skip to content

Commit

Permalink
refactor sso driver with separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
iqbalatma committed Nov 1, 2023
1 parent c84e700 commit 9c4215b
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 253 deletions.
66 changes: 0 additions & 66 deletions src/Abstracts/BaseMumtazSSOService.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/AuthService.php

This file was deleted.

99 changes: 99 additions & 0 deletions src/HttpClientService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Classid\SsoDriver;


class HttpClientService implements \Classid\SsoDriver\Interfaces\HttpClient
{
public array $httpRequestHeaders;
public string $baseUrl;

public function __construct()
{
$this->setDefaultHttpRequestHeader()
->setBaseUrl();
}

/**
* @return HttpClientService
*/
public function setBaseUrl(): HttpClientService
{
$this->baseUrl = rtrim(config("mumtaz_sso_driver.host"), "/");
return $this;
}

/**
* @return string
*/
public function getBaseUrl(): string
{
return $this->baseUrl;
}


/**
* @return HttpClientService
*/
public function setDefaultHttpRequestHeader(): HttpClientService
{
$this->httpRequestHeaders = [
"Accept" => "application/json"
];

return $this;
}


/**
* @param array $httpRequestHeaders
* @return HttpClientService
*/
public function setHttpRequestHeaders(array $httpRequestHeaders): HttpClientService
{
$this->httpRequestHeaders = $httpRequestHeaders;
return $this;
}


/**
* @param string $key
* @param string|array $value
* @return HttpClientService
*/
public function addHttpRequestHeader(string $key, string|array $value): HttpClientService
{
$this->httpRequestHeaders[$key] = $value;
return $this;
}


/**
* @param array $httpRequestHeaders
* @return HttpClientService
*/
public function addHttpRequestHeaders(array $httpRequestHeaders): HttpClientService
{
$this->httpRequestHeaders = array_merge($this->httpRequestHeaders, $httpRequestHeaders);
return $this;
}


/**
* @return array
*/
public function getHttpRequestHeaders(): array
{
return $this->httpRequestHeaders;
}


/**
* @param string $key
* @return array
*/
public function getHttpRequestHeader(string $key): array
{
return $this->httpRequestHeaders[$key];
}
}
53 changes: 53 additions & 0 deletions src/Interfaces/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Classid\SsoDriver\Interfaces;



interface HttpClient
{
/**
* @return string
*/
public function getBaseUrl():string;

/**
* @return HttpClient
*/
public function setBaseUrl():HttpClient;

/**
* @return HttpClient
*/
public function setDefaultHttpRequestHeader(): HttpClient;

/**
* @param array $httpRequestHeaders
* @return HttpClient
*/
public function setHttpRequestHeaders(array $httpRequestHeaders): HttpClient;

/**
* @param string $key
* @param string|array $value
* @return $this
*/
public function addHttpRequestHeader(string $key, string|array $value): HttpClient;

/**
* @param array $httpRequestHeaders
* @return HttpClient
*/
public function addHttpRequestHeaders(array $httpRequestHeaders): HttpClient;

/**
* @return array
*/
public function getHttpRequestHeaders(): array;

/**
* @param string $key
* @return array
*/
public function getHttpRequestHeader(string $key):array;
}
15 changes: 0 additions & 15 deletions src/Interfaces/MumtazSSOServiceInterface.php

This file was deleted.

31 changes: 31 additions & 0 deletions src/Interfaces/OauthToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Classid\SsoDriver\Interfaces;


use Classid\SsoDriver\Exceptions\InvalidClientCredentials;
use Classid\SsoDriver\Exceptions\InvalidRetryGenerateException;
use Classid\SsoDriver\Exceptions\SSODriverException;
use Classid\SsoDriver\Exceptions\UnknownErrorHandlerException;

interface OauthToken
{
/**
* @param array|null $errorResponse
* @return bool
* @throws InvalidClientCredentials
* @throws SSODriverException
* @throws UnknownErrorHandlerException|InvalidRetryGenerateException
*/
public function isRetryOnInvalidAccessToken(?array $errorResponse): bool;


/**
* @param bool $isRegenerate
* @return string
* @throws InvalidClientCredentials
* @throws SSODriverException
* @throws UnknownErrorHandlerException
*/
public function getClientAccessToken(bool $isRegenerate = false): string;
}
29 changes: 29 additions & 0 deletions src/Interfaces/SSO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Classid\SsoDriver\Interfaces;


use Classid\SsoDriver\Exceptions\InvalidClientCredentials;
use Classid\SsoDriver\Exceptions\InvalidRetryGenerateException;
use Classid\SsoDriver\Exceptions\SSODriverException;
use Classid\SsoDriver\Exceptions\UnknownErrorHandlerException;

interface SSO
{
/**
* @return $this
* @throws InvalidClientCredentials
* @throws SSODriverException
* @throws UnknownErrorHandlerException
*/
public function setAuthorizationToken(): self;


/**
* @param callable $request
* @param callable|null $customErrorHandler
* @return object|null
* @throws InvalidRetryGenerateException|UnknownErrorHandlerException|InvalidClientCredentials|SSODriverException
*/
public function getResponse(callable $request, callable $customErrorHandler = null): ?object;
}
26 changes: 24 additions & 2 deletions src/MumtazSSODriverServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Classid\SsoDriver;

use Classid\SsoDriver\Interfaces\HttpClient;
use Classid\SsoDriver\Interfaces\OauthToken;
use Classid\SsoDriver\Interfaces\SSO;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class MumtazSSODriverServiceProvider extends ServiceProvider
Expand All @@ -12,9 +16,27 @@ class MumtazSSODriverServiceProvider extends ServiceProvider
public function register(): void
{
$this->publishes([
__DIR__."/Config/mumtaz_sso_driver.php" => config_path("mumtaz_sso_driver.php")
__DIR__ . "/Config/mumtaz_sso_driver.php" => config_path("mumtaz_sso_driver.php")
]);
$this->mergeConfigFrom(__DIR__."/Config/mumtaz_sso_driver.php", "mumtaz_sso_driver");
$this->mergeConfigFrom(__DIR__ . "/Config/mumtaz_sso_driver.php", "mumtaz_sso_driver");

$this->app->singleton(HttpClient::class, function (){
return new HttpClientService();
});


$this->app->singleton(OauthToken::class, function (Application $app){
$httpClient = $app->make(HttpClient::class);
return new OauthTokenService($httpClient);
});

$this->app->singleton(SSO::class, function (Application $app) {
$httpClient = $app->make(HttpClient::class);
$oauthTokenService = $app->make(OauthToken::class);
return new SSOService($httpClient,$oauthTokenService);
});


}

/**
Expand Down
Loading

0 comments on commit 9c4215b

Please sign in to comment.