Skip to content

Commit

Permalink
Refactoring authorization module.
Browse files Browse the repository at this point in the history
  • Loading branch information
iodine-http committed Mar 19, 2021
1 parent 14f6d42 commit 4c808b6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
"dev-master": "1.2-dev"
}
},
"autoload": {
Expand Down
34 changes: 32 additions & 2 deletions src/Auth/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,36 @@ class Authorization extends AbstractService implements AuthorizationInterface
*/
private $cacheTag = 'authorization_metadata';

/**
* @var bool
*/
private $cached;

/**
* @param CacheItemPoolInterface $cachePool
* @param array $config
* @param bool $cached
* @return void
*/
public function __construct(CacheItemPoolInterface $cachePool, array $config)
{
public function __construct(
CacheItemPoolInterface $cachePool,
array $config,
bool $cached = false
) {
parent::__construct($config);
$this->setCachePool($cachePool);
$this->useCache($cached);
}

/**
* {@inheritdoc}
*/
public function authorize()
{
if (!$this->isCached()) {
return $this->getCredentialFromServer();
}

$currentCredential = $this->getCurrentCredentialFromCache();

if (null === $currentCredential) {
Expand Down Expand Up @@ -91,6 +105,22 @@ public function getCacheTag()
return $this->cacheTag;
}

/**
* {@inheritdoc}
*/
public function useCache(bool $cached)
{
$this->cached = $cached;
}

/**
* {@inheritdoc}
*/
public function isCached(): bool
{
return $this->cached;
}

/**
* Fetch current credential from cache pool, if any.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Auth/AuthorizationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,22 @@ public function setCacheTag(string $cacheTag);
* @return string
*/
public function getCacheTag();

/**
* Determine if authorization object caller
* want to save fetched authorization metadata
* or otherwise.
*
* @var bool $cached
* @return void
*/
public function useCache(bool $cached);

/**
* Check if current authorization metadata
* is cached or not.
*
* @return bool
*/
public function isCached(): bool;
}
3 changes: 2 additions & 1 deletion tests/Auth/AuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ protected function setUp(): void
{
$this->setAuthorization(new Authorization(
new ArrayCachePool(),
['client_secret' => 'abc', 'client_id' => 'cba']
['client_secret' => 'abc', 'client_id' => 'cba'],
true
));
}

Expand Down

0 comments on commit 4c808b6

Please sign in to comment.