From d69ecbc1706eae3c4037f807dafb31e9b9aace85 Mon Sep 17 00:00:00 2001 From: japatel Date: Mon, 22 Dec 2014 14:09:48 -0600 Subject: [PATCH 1/3] Authorization Cache to use both boolean and string in configuration - both true and 'true' will enable authorization cache --- lib/PayPal/Cache/AuthorizationCache.php | 2 +- sample/bootstrap.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/PayPal/Cache/AuthorizationCache.php b/lib/PayPal/Cache/AuthorizationCache.php index c1f7cfd..0f0be78 100644 --- a/lib/PayPal/Cache/AuthorizationCache.php +++ b/lib/PayPal/Cache/AuthorizationCache.php @@ -85,7 +85,7 @@ abstract class AuthorizationCache $config = ($config && is_array($config)) ? $config : PPConfigManager::getInstance()->getConfigHashmap(); if (array_key_exists("cache.enabled", $config)) { $value = $config['cache.enabled']; - return (trim($value) == 'true') ? true : false; + return (trim($value) == true || trim($value) == 'true') ? true : false; } return false; } diff --git a/sample/bootstrap.php b/sample/bootstrap.php index 00f7fac..ad6509b 100644 --- a/sample/bootstrap.php +++ b/sample/bootstrap.php @@ -77,7 +77,7 @@ function getApiContext($clientId, $clientSecret) 'log.FileName' => '../PayPal.log', 'log.LogLevel' => 'FINE', 'validation.level' => 'log', - 'cache.enabled' => 'true', + 'cache.enabled' => true, // 'http.headers.PayPal-Partner-Attribution-Id' => '123123123' ) ); From f29c6274665a1113ee861f04a3c90b5c77c36eba Mon Sep 17 00:00:00 2001 From: Steve Angel Date: Mon, 5 Jan 2015 16:54:43 +0000 Subject: [PATCH 2/3] Added Custom Cache Directory Option --- README.md | 12 +++++++++--- lib/PayPal/Cache/AuthorizationCache.php | 25 ++++++++++++++++++++----- sample/sdk_config.ini | 10 ++++++++-- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 59e30a9..0ed6be5 100644 --- a/README.md +++ b/README.md @@ -144,16 +144,22 @@ Please visit our [sample sdk_config.ini](https://github.com/paypal/PayPal-PHP-SD ;Caching Configuration [cache] ; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the -; server into a file provided in constant $CACHE_PATH in PayPal/Cache/AuthorizationCache. +; server into a file provided by the cache.FileName option or by using +; the constant $CACHE_PATH value in PayPal/Cache/AuthorizationCache if the option is omitted/empty. ; If the value is set to 'true', it would try to create a file and store the information. ; For any other value, it would disable it ; Please note, this is a very good performance improvement, and we would encourage you to ; set this up properly to reduce the number of calls, to almost 50% on normal use cases ; PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on -; your hosting server +; your hosting server or whichever custom directory you choose cache.enabled=true +; When using a relative path, the cache file is created +; relative to the .php file that is the entry point +; for this request. You can also provide an absolute +; path here +cache.FileName=../auth.cache ``` -##### PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on your hosting server +##### PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK (or your custom directory) on your hosting server ### API Model Constructor diff --git a/lib/PayPal/Cache/AuthorizationCache.php b/lib/PayPal/Cache/AuthorizationCache.php index 0f0be78..4505ab4 100644 --- a/lib/PayPal/Cache/AuthorizationCache.php +++ b/lib/PayPal/Cache/AuthorizationCache.php @@ -23,9 +23,10 @@ abstract class AuthorizationCache if (!self::isEnabled($config)) { return null; } $tokens = null; - if (file_exists(__DIR__ . self::$CACHE_PATH)) { + $cachePath = self::cachePath($config); + if (file_exists($cachePath)) { // Read from the file - $cachedToken = file_get_contents(__DIR__ . self::$CACHE_PATH); + $cachedToken = file_get_contents($cachePath); if ($cachedToken && JsonValidator::validate($cachedToken, true)) { $tokens = json_decode($cachedToken, true); if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) { @@ -54,8 +55,9 @@ abstract class AuthorizationCache // Return if not enabled if (!self::isEnabled($config)) { return; } - if (!is_dir(dirname(__DIR__ . self::$CACHE_PATH))) { - if (mkdir(dirname(__DIR__ . self::$CACHE_PATH), 0755, true) == false) { + $cachePath = self::cachePath($config); + if (!is_dir(dirname($cachePath))) { + if (mkdir(dirname($cachePath), 0755, true) == false) { return; } } @@ -71,7 +73,7 @@ abstract class AuthorizationCache 'tokenExpiresIn' => $tokenExpiresIn ); } - file_put_contents(__DIR__ . self::$CACHE_PATH, json_encode($tokens)); + file_put_contents($cachePath, json_encode($tokens)); } /** @@ -89,6 +91,19 @@ abstract class AuthorizationCache } return false; } + + /** + * Returns the cache file path + * + * @param $config + * @return string + */ + public static function cachePath($config) + { + $config = ($config && is_array($config)) ? $config : PPConfigManager::getInstance()->getConfigHashmap(); + $cachePath = (array_key_exists("cache.FileName", $config)) ? trim($config['cache.FileName']) : null; + return empty($cachePath) ? __DIR__ . self::$CACHE_PATH : $cachePath; + } } diff --git a/sample/sdk_config.ini b/sample/sdk_config.ini index 54a6a5a..6cf1918 100644 --- a/sample/sdk_config.ini +++ b/sample/sdk_config.ini @@ -49,11 +49,17 @@ validation.level=log ;Caching Configuration [cache] ; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the -; server into a file provided in constant $CACHE_PATH in PayPal/Cache/AuthorizationCache. +; server into a file provided by the cache.FileName option or by using +; the constant $CACHE_PATH value in PayPal/Cache/AuthorizationCache if the option is omitted/empty. ; If the value is set to 'true', it would try to create a file and store the information. ; For any other value, it would disable it ; Please note, this is a very good performance improvement, and we would encourage you to ; set this up properly to reduce the number of calls, to almost 50% on normal use cases ; PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on -; your hosting server +; your hosting server or whichever custom directory you choose cache.enabled=true +; When using a relative path, the cache file is created +; relative to the .php file that is the entry point +; for this request. You can also provide an absolute +; path here +cache.FileName=../auth.cache From 2cd71ea19056b44465b7681df9e46bdfc3a2391e Mon Sep 17 00:00:00 2001 From: japatel Date: Mon, 5 Jan 2015 14:10:42 -0600 Subject: [PATCH 3/3] Refactored Authorization Cache - Updated Authorization Cache Common Code - Unit Tests --- lib/PayPal/Cache/AuthorizationCache.php | 25 ++++--- .../Test/Cache/AuthorizationCacheTest.php | 68 +++++++++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 tests/PayPal/Test/Cache/AuthorizationCacheTest.php diff --git a/lib/PayPal/Cache/AuthorizationCache.php b/lib/PayPal/Cache/AuthorizationCache.php index 6667ef7..32649a2 100644 --- a/lib/PayPal/Cache/AuthorizationCache.php +++ b/lib/PayPal/Cache/AuthorizationCache.php @@ -84,12 +84,8 @@ abstract class AuthorizationCache */ public static function isEnabled($config) { - $config = ($config && is_array($config)) ? $config : PayPalConfigManager::getInstance()->getConfigHashmap(); - if (array_key_exists("cache.enabled", $config)) { - $value = $config['cache.enabled']; - return (trim($value) == true || trim($value) == 'true') ? true : false; - } - return false; + $value = self::getConfigValue('cache.enabled', $config); + return empty($value) ? false : ((trim($value) == true || trim($value) == 'true') ? true : false); } /** @@ -100,10 +96,23 @@ abstract class AuthorizationCache */ public static function cachePath($config) { - $config = ($config && is_array($config)) ? $config : PPConfigManager::getInstance()->getConfigHashmap(); - $cachePath = (array_key_exists("cache.FileName", $config)) ? trim($config['cache.FileName']) : null; + $cachePath = self::getConfigValue('cache.FileName', $config); return empty($cachePath) ? __DIR__ . self::$CACHE_PATH : $cachePath; } + /** + * Returns the Value of the key if found in given config, or from PayPal Config Manager + * Returns null if not found + * + * @param $key + * @param $config + * @return null|string + */ + private static function getConfigValue($key, $config) + { + $config = ($config && is_array($config)) ? $config : PayPalConfigManager::getInstance()->getConfigHashmap(); + return (array_key_exists($key, $config)) ? trim($config[$key]) : null; + } + } diff --git a/tests/PayPal/Test/Cache/AuthorizationCacheTest.php b/tests/PayPal/Test/Cache/AuthorizationCacheTest.php new file mode 100644 index 0000000..b3b4b0c --- /dev/null +++ b/tests/PayPal/Test/Cache/AuthorizationCacheTest.php @@ -0,0 +1,68 @@ + 'true'), true), + array(array('cache.enabled' => true), true), + array(array(), false), + array(null, false) + ); + } + + public static function CachePathProvider() + { + return array( + array(array('cache.FileName' => 'temp.cache'), 'temp.cache'), + array(array(), 'auth.cache'), + array(null, 'auth.cache') + ); + } + + /** + * + * @dataProvider EnabledProvider + */ + public function testIsEnabled($config, $expected) + { + $result = AuthorizationCache::isEnabled($config); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider CachePathProvider + */ + public function testCachePath($config, $expected) + { + $result = AuthorizationCache::cachePath($config); + $this->assertContains($expected, $result); + } + +}