-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ClosureTokenPersistence for wrapping other cache providers.
- Loading branch information
Showing
3 changed files
with
134 additions
and
1 deletion.
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,49 @@ | ||
<?php | ||
|
||
namespace kamermans\OAuth2\Persistence; | ||
|
||
use Illuminate\Contracts\Cache\Repository; | ||
use kamermans\OAuth2\Token\TokenInterface; | ||
|
||
class ClosureTokenPersistence implements TokenPersistenceInterface | ||
{ | ||
|
||
private $doSaveToken; | ||
private $doRestoreToken; | ||
private $doDeleteToken; | ||
private $doHasToken; | ||
|
||
public function __construct(callable $saveToken, callable $restoreToken, callable $deleteToken, callable $hasToken) | ||
{ | ||
$this->doSaveToken = $saveToken; | ||
$this->doRestoreToken = $restoreToken; | ||
$this->doDeleteToken = $deleteToken; | ||
$this->doHasToken = $hasToken; | ||
} | ||
|
||
public function saveToken(TokenInterface $token) | ||
{ | ||
call_user_func($this->doSaveToken, $token->serialize()); | ||
} | ||
|
||
public function restoreToken(TokenInterface $token) | ||
{ | ||
$data = call_user_func($this->doRestoreToken); | ||
|
||
if (!is_array($data)) { | ||
return null; | ||
} | ||
|
||
return $token->unserialize($data); | ||
} | ||
|
||
public function deleteToken() | ||
{ | ||
call_user_func($this->doDeleteToken); | ||
} | ||
|
||
public function hasToken() | ||
{ | ||
return call_user_func($this->doHasToken); | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
namespace kamermans\OAuth2\Tests\Persistence; | ||
|
||
use Illuminate\Cache\ArrayStore; | ||
use Illuminate\Cache\Repository; | ||
use kamermans\OAuth2\Persistence\ClosureTokenPersistence; | ||
use kamermans\OAuth2\Token\RawToken; | ||
use kamermans\OAuth2\Token\RawTokenFactory; | ||
|
||
class ClosureTokenPersistenceTest extends TokenPersistenceTestBase | ||
{ | ||
|
||
private $cache = []; | ||
|
||
public function setUp() | ||
{ | ||
$this->cache = []; | ||
} | ||
|
||
public function getInstance() | ||
{ | ||
$cache = &$this->cache; | ||
$cache_key = "foo"; | ||
|
||
$exists = function() use (&$cache, $cache_key) { | ||
return array_key_exists($cache_key, $cache); | ||
}; | ||
|
||
$set = function(array $value) use (&$cache, $cache_key) { | ||
$cache[$cache_key] = $value; | ||
}; | ||
|
||
$get = function() use (&$cache, $cache_key, $exists) { | ||
return $exists()? $cache[$cache_key]: null; | ||
}; | ||
|
||
$delete = function() use (&$cache, $cache_key, $exists) { | ||
if ($exists()) { | ||
unset($cache[$cache_key]); | ||
} | ||
}; | ||
|
||
return new ClosureTokenPersistence($set, $get, $delete, $exists); | ||
} | ||
} |