Skip to content

Commit

Permalink
use PSR-6 for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed May 5, 2016
1 parent fb54a20 commit df65e8e
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 180 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"homepage": "http://github.com/google/google-auth-library-php",
"license": "Apache-2.0",
"require": {
"php": ">=5.4",
"firebase/php-jwt": "~2.0|~3.0",
"guzzlehttp/guzzle": "~5.2|~6.0",
"php": ">=5.4",
"guzzlehttp/psr7": "1.2.*",
"psr/http-message": "1.0.*"
"psr/http-message": "^1.0",
"psr/cache": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
Expand Down
9 changes: 5 additions & 4 deletions src/ApplicationDefaultCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Google\Auth\Credentials\GCECredentials;
use Google\Auth\Middleware\AuthTokenMiddleware;
use Google\Auth\Subscriber\AuthTokenSubscriber;
use Psr\Cache\CacheItemPoolInterface;

/**
* ApplicationDefaultCredentials obtains the default credentials for
Expand Down Expand Up @@ -71,7 +72,7 @@ class ApplicationDefaultCredentials
* either as an Array or as a space-delimited String.
* @param callable $httpHandler callback which delivers psr7 request
* @param array $cacheConfig configuration for the cache when it's present
* @param CacheInterface $cache an implementation of CacheInterface
* @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface
*
* @return AuthTokenSubscriber
*
Expand All @@ -81,7 +82,7 @@ public static function getSubscriber(
$scope = null,
callable $httpHandler = null,
array $cacheConfig = null,
CacheInterface $cache = null
CacheItemPoolInterface $cache = null
) {
$creds = self::getCredentials($scope, $httpHandler);

Expand All @@ -99,7 +100,7 @@ public static function getSubscriber(
* either as an Array or as a space-delimited String.
* @param callable $httpHandler callback which delivers psr7 request
* @param array $cacheConfig configuration for the cache when it's present
* @param CacheInterface $cache
* @param CacheItemPoolInterface $cache
*
* @return AuthTokenMiddleware
*
Expand All @@ -109,7 +110,7 @@ public static function getMiddleware(
$scope = null,
callable $httpHandler = null,
array $cacheConfig = null,
CacheInterface $cache = null
CacheItemPoolInterface $cache = null
) {
$creds = self::getCredentials($scope, $httpHandler);

Expand Down
50 changes: 0 additions & 50 deletions src/CacheInterface.php

This file was deleted.

20 changes: 15 additions & 5 deletions src/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ private function getCachedValue()
return;
}

$key = $this->cacheConfig['prefix'].$fetcherKey;

return $this->cache->get($key, $this->cacheConfig['lifetime']);
$key = self::getValidKeyName($this->cacheConfig['prefix'].$fetcherKey);
$cacheItem = $this->cache->getItem($key);
return $cacheItem->get();
}

/**
Expand All @@ -62,7 +62,17 @@ private function setCachedValue($v)
if (is_null($fetcherKey)) {
return;
}
$key = $this->cacheConfig['prefix'].$fetcherKey;
$this->cache->set($key, $v);

$key = self::getValidKeyName($this->cacheConfig['prefix'].$fetcherKey);
$cacheItem = $this->cache->getItem($key);
$cacheItem->set($v);
$cacheItem->expiresAfter($this->cacheConfig['lifetime']);
return $this->cache->save($cacheItem);
}

public static function getValidKeyName($key)
{
// ensure we do not have illegal characters
return str_replace(['{','}','(',')','/','\\','@',':'], '-', $key);
}
}
8 changes: 4 additions & 4 deletions src/Middleware/AuthTokenMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

namespace Google\Auth\Middleware;

use Google\Auth\CacheInterface;
use Google\Auth\CacheTrait;
use Google\Auth\FetchAuthTokenInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* AuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header
Expand All @@ -40,7 +40,7 @@ class AuthTokenMiddleware
const DEFAULT_CACHE_LIFETIME = 1500;

/**
* @var CacheInterface
* @var CacheItemPoolInterface
*/
private $cache;

Expand All @@ -64,13 +64,13 @@ class AuthTokenMiddleware
*
* @param FetchAuthTokenInterface $fetcher is used to fetch the auth token
* @param array $cacheConfig configures the cache
* @param CacheInterface $cache (optional) caches the token.
* @param CacheItemPoolInterface $cache (optional) caches the token.
* @param callable $httpHandler (optional) callback which delivers psr7 request
*/
public function __construct(
FetchAuthTokenInterface $fetcher,
array $cacheConfig = null,
CacheInterface $cache = null,
CacheItemPoolInterface $cache = null,
callable $httpHandler = null
) {
$this->fetcher = $fetcher;
Expand Down
8 changes: 4 additions & 4 deletions src/Middleware/ScopedAccessTokenMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

namespace Google\Auth\Middleware;

use Google\Auth\CacheInterface;
use Google\Auth\CacheTrait;
use Google\Auth\FetchAuthTokenInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* ScopedAccessTokenMiddleware is a Guzzle Middleware that adds an Authorization
Expand All @@ -41,7 +41,7 @@ class ScopedAccessTokenMiddleware
const DEFAULT_CACHE_LIFETIME = 1500;

/**
* @var CacheInterface
* @var CacheItemPoolInterface
*/
private $cache;

Expand Down Expand Up @@ -76,13 +76,13 @@ class ScopedAccessTokenMiddleware
* @param callable $tokenFunc a token generator function
* @param array|string $scopes the token authentication scopes
* @param array $cacheConfig configuration for the cache when it's present
* @param CacheInterface $cache an implementation of CacheInterface
* @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface
*/
public function __construct(
callable $tokenFunc,
$scopes,
array $cacheConfig = null,
CacheInterface $cache = null
CacheItemPoolInterface $cache = null
) {
$this->tokenFunc = $tokenFunc;
if (!(is_string($scopes) || is_array($scopes))) {
Expand Down
8 changes: 4 additions & 4 deletions src/Subscriber/AuthTokenSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

namespace Google\Auth\Subscriber;

use Google\Auth\CacheInterface;
use Google\Auth\CacheTrait;
use Google\Auth\FetchAuthTokenInterface;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\RequestEvents;
use GuzzleHttp\Event\SubscriberInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* AuthTokenSubscriber is a Guzzle Subscriber that adds an Authorization header
Expand All @@ -42,7 +42,7 @@ class AuthTokenSubscriber implements SubscriberInterface
const DEFAULT_CACHE_LIFETIME = 1500;

/**
* @var CacheInterface
* @var CacheItemPoolInterface
*/
private $cache;

Expand All @@ -66,13 +66,13 @@ class AuthTokenSubscriber implements SubscriberInterface
*
* @param FetchAuthTokenInterface $fetcher is used to fetch the auth token
* @param array $cacheConfig configures the cache
* @param CacheInterface $cache (optional) caches the token.
* @param CacheItemPoolInterface $cache (optional) caches the token.
* @param callable $httpHandler (optional) http client to fetch the token.
*/
public function __construct(
FetchAuthTokenInterface $fetcher,
array $cacheConfig = null,
CacheInterface $cache = null,
CacheItemPoolInterface $cache = null,
callable $httpHandler = null
) {
$this->fetcher = $fetcher;
Expand Down
8 changes: 4 additions & 4 deletions src/Subscriber/ScopedAccessTokenSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

namespace Google\Auth\Subscriber;

use Google\Auth\CacheInterface;
use Google\Auth\CacheTrait;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\RequestEvents;
use GuzzleHttp\Event\SubscriberInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* ScopedAccessTokenSubscriber is a Guzzle Subscriber that adds an Authorization
Expand All @@ -42,7 +42,7 @@ class ScopedAccessTokenSubscriber implements SubscriberInterface
const DEFAULT_CACHE_LIFETIME = 1500;

/**
* @var CacheInterface
* @var CacheItemPoolInterface
*/
private $cache;

Expand All @@ -67,13 +67,13 @@ class ScopedAccessTokenSubscriber implements SubscriberInterface
* @param callable $tokenFunc a token generator function
* @param array|string $scopes the token authentication scopes
* @param array $cacheConfig configuration for the cache when it's present
* @param CacheInterface $cache an implementation of CacheInterface
* @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface
*/
public function __construct(
callable $tokenFunc,
$scopes,
array $cacheConfig = null,
CacheInterface $cache = null
CacheItemPoolInterface $cache = null
) {
$this->tokenFunc = $tokenFunc;
if (!(is_string($scopes) || is_array($scopes))) {
Expand Down
38 changes: 31 additions & 7 deletions tests/CacheTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class CacheTraitTest extends \PHPUnit_Framework_TestCase
{
private $mockFetcher;
private $mockCacheItem;
private $mockCache;

public function setUp()
Expand All @@ -30,19 +31,27 @@ public function setUp()
$this
->getMockBuilder('Google\Auth\FetchAuthTokenInterface')
->getMock();
$this->mockCacheItem =
$this
->getMockBuilder('Psr\Cache\CacheItemInterface')
->getMock();
$this->mockCache =
$this
->getMockBuilder('Google\Auth\CacheInterface')
->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
->getMock();
}

public function testSuccessfullyPullsFromCacheWithoutFetcher()
{
$expectedValue = '1234';
$this->mockCache
$this->mockCacheItem
->expects($this->once())
->method('get')
->will($this->returnValue($expectedValue));
$this->mockCache
->expects($this->once())
->method('getItem')
->will($this->returnValue($this->mockCacheItem));

$implementation = new CacheTraitImplementation([
'cache' => $this->mockCache,
Expand All @@ -55,10 +64,14 @@ public function testSuccessfullyPullsFromCacheWithoutFetcher()
public function testSuccessfullyPullsFromCacheWithFetcher()
{
$expectedValue = '1234';
$this->mockCache
$this->mockCacheItem
->expects($this->once())
->method('get')
->will($this->returnValue($expectedValue));
$this->mockCache
->expects($this->once())
->method('getItem')
->will($this->returnValue($this->mockCacheItem));
$this->mockFetcher
->expects($this->once())
->method('getCacheKey')
Expand Down Expand Up @@ -99,10 +112,15 @@ public function testFailsPullFromCacheWithoutKey()
public function testSuccessfullySetsToCacheWithoutFetcher()
{
$value = '1234';
$this->mockCache
$this->mockCacheItem
->expects($this->once())
->method('set')
->with('key', $value);
->with($value);
$this->mockCache
->expects($this->once())
->method('getItem')
->with($this->equalTo('key'))
->will($this->returnValue($this->mockCacheItem));

$implementation = new CacheTraitImplementation([
'cache' => $this->mockCache,
Expand All @@ -114,10 +132,15 @@ public function testSuccessfullySetsToCacheWithoutFetcher()
public function testSuccessfullySetsToCacheWithFetcher()
{
$value = '1234';
$this->mockCache
$this->mockCacheItem
->expects($this->once())
->method('set')
->with('key', $value);
->with($value);
$this->mockCache
->expects($this->once())
->method('getItem')
->with($this->equalTo('key'))
->will($this->returnValue($this->mockCacheItem));
$this->mockFetcher
->expects($this->once())
->method('getCacheKey')
Expand Down Expand Up @@ -157,6 +180,7 @@ public function testFailsSetToCacheWithoutKey()
]);

$cachedValue = $implementation->sCachedValue('1234');
$this->assertNull($cachedValue);
}
}

Expand Down
Loading

0 comments on commit df65e8e

Please sign in to comment.