This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathAuthorizationCache.php
123 lines (112 loc) · 3.86 KB
/
AuthorizationCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace PayPal\Cache;
use PayPal\Core\PayPalConfigManager;
use PayPal\Validation\JsonValidator;
abstract class AuthorizationCache
{
public static $CACHE_PATH = '/../../../var/auth.cache';
/**
* A pull method which would read the persisted data based on clientId.
* If clientId is not provided, an array with all the tokens would be passed.
*
* @param array|null $config
* @param string $clientId
* @return mixed|null
*/
public static function pull($config = null, $clientId = null)
{
// Return if not enabled
if (!self::isEnabled($config)) {
return null;
}
$tokens = null;
$cachePath = self::cachePath($config);
if (file_exists($cachePath)) {
// Read from the file
$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)) {
// If client Id is found, just send in that data only
return $tokens[$clientId];
} elseif ($clientId) {
// If client Id is provided, but no key in persisted data found matching it.
return null;
}
}
}
return $tokens;
}
/**
* Persists the data into a cache file provided in $CACHE_PATH
*
* @param array|null $config
* @param $clientId
* @param $accessToken
* @param $tokenCreateTime
* @param $tokenExpiresIn
* @throws \Exception
*/
public static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn)
{
// Return if not enabled
if (!self::isEnabled($config)) {
return;
}
$cachePath = self::cachePath($config);
if (!is_dir(dirname($cachePath))) {
if (mkdir(dirname($cachePath), 0755, true) == false) {
throw new \Exception("Failed to create directory at $cachePath");
}
}
// Reads all the existing persisted data
$tokens = self::pull();
$tokens = $tokens ? $tokens : array();
if (is_array($tokens)) {
$tokens[$clientId] = array(
'clientId' => $clientId,
'accessTokenEncrypted' => $accessToken,
'tokenCreateTime' => $tokenCreateTime,
'tokenExpiresIn' => $tokenExpiresIn
);
}
if (!file_put_contents($cachePath, json_encode($tokens))) {
throw new \Exception("Failed to write cache");
};
}
/**
* Determines from the Configuration if caching is currently enabled/disabled
*
* @param $config
* @return bool
*/
public static function isEnabled($config)
{
$value = self::getConfigValue('cache.enabled', $config);
return empty($value) ? false : ((trim($value) == true || trim($value) == 'true'));
}
/**
* Returns the cache file path
*
* @param $config
* @return string
*/
public static function cachePath($config)
{
$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;
}
}