-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from softonic/feature/add-client-builder
Feature/add client builder
- Loading branch information
Showing
4 changed files
with
131 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Softonic\OAuth2\Guzzle\Middleware; | ||
|
||
use GuzzleHttp\HandlerStack as Stack; | ||
use League\OAuth2\Client\Provider\AbstractProvider as OAuth2Provider; | ||
use Psr\Cache\CacheItemPoolInterface as Cache; | ||
|
||
class ClientBuilder | ||
{ | ||
public static function build( | ||
OAuth2Provider $oauthProvider, | ||
array $tokenOptions, | ||
Cache $cache, | ||
array $guzzleOptions = null | ||
): \GuzzleHttp\Client { | ||
$cacheHandler = new AccessTokenCacheHandler($cache); | ||
|
||
$stack = static::getStack(); | ||
|
||
$stack = static::addHeaderMiddlewareToStack( | ||
$stack, | ||
$oauthProvider, | ||
$tokenOptions, | ||
$cacheHandler | ||
); | ||
$stack = static::addRetryMiddlewareToStack( | ||
$stack, | ||
$oauthProvider, | ||
$tokenOptions, | ||
$cacheHandler | ||
); | ||
|
||
$defaultOptions = [ | ||
'handler' => $stack, | ||
]; | ||
$guzzleOptions = static::mergeOptions($defaultOptions, $guzzleOptions); | ||
|
||
return new \GuzzleHttp\Client($guzzleOptions); | ||
} | ||
|
||
private static function getStack(): Stack | ||
{ | ||
$stack = new Stack(); | ||
$stack->setHandler(new \GuzzleHttp\Handler\CurlHandler()); | ||
return $stack; | ||
} | ||
|
||
private static function addHeaderMiddlewareToStack( | ||
Stack $stack, | ||
OAuth2Provider $oauthProvider, | ||
array $tokenOptions, | ||
AccessTokenCacheHandler $cacheHandler | ||
): Stack { | ||
$addAuthorizationHeader = new AddAuthorizationHeader( | ||
$oauthProvider, | ||
$tokenOptions, | ||
$cacheHandler | ||
); | ||
|
||
$stack->push(\GuzzleHttp\Middleware::mapRequest($addAuthorizationHeader)); | ||
return $stack; | ||
} | ||
|
||
private static function addRetryMiddlewareToStack( | ||
Stack $stack, | ||
OAuth2Provider $oauthProvider, | ||
array $tokenOptions, | ||
AccessTokenCacheHandler $cacheHandler | ||
): Stack { | ||
$retryOnAuthorizationError = new RetryOnAuthorizationError( | ||
$oauthProvider, | ||
$tokenOptions, | ||
$cacheHandler | ||
); | ||
|
||
$stack->push(\GuzzleHttp\Middleware::retry($retryOnAuthorizationError)); | ||
return $stack; | ||
} | ||
|
||
private static function mergeOptions(array $defaultOptions, array $options = null): array | ||
{ | ||
$options = $options ?? []; | ||
return array_merge($options, $defaultOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Softonic\OAuth2\Guzzle\Middleware\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Softonic\OAuth2\Guzzle\Middleware\ClientBuilder; | ||
|
||
class ClientBuilderTest extends TestCase | ||
{ | ||
public function testClientBuilderWithoutGuzzleOptions() | ||
{ | ||
$mockCache = $this->createMock(\Psr\Cache\CacheItemPoolInterface::class); | ||
$mockProvider = $this->createMock(\League\OAuth2\Client\Provider\AbstractProvider::class); | ||
$mockTokenOptions = []; | ||
|
||
$client = ClientBuilder::build($mockProvider, $mockTokenOptions, $mockCache); | ||
$this->assertInstanceOf(\GuzzleHttp\ClientInterface::class, $client); | ||
} | ||
|
||
public function testClientBuilderWithGuzzleOptions() | ||
{ | ||
$mockCache = $this->createMock(\Psr\Cache\CacheItemPoolInterface::class); | ||
$mockProvider = $this->createMock(\League\OAuth2\Client\Provider\AbstractProvider::class); | ||
$mockTokenOptions = []; | ||
$baseUri = 'https://foo.bar/'; | ||
$mockGuzzleOptions = [ | ||
'base_uri' => $baseUri, | ||
]; | ||
$client = ClientBuilder::build( | ||
$mockProvider, | ||
$mockTokenOptions, | ||
$mockCache, | ||
$mockGuzzleOptions | ||
); | ||
$this->assertInstanceOf(\GuzzleHttp\ClientInterface::class, $client); | ||
|
||
$guzzleConfig = $client->getConfig(); | ||
$this->assertEquals($baseUri, (string) $guzzleConfig['base_uri']); | ||
} | ||
} |