Remove unused fetchLiveVaultBalance and its test
This commit is contained in:
@@ -14,11 +14,6 @@
|
||||
* @link https://github.com/ksolomon/Torn-Vault-Tracker
|
||||
*/
|
||||
|
||||
// Test hook for fetchLiveVaultBalance(). When set, this closure is called
|
||||
// instead of executeApiCall. Set $GLOBALS['liveBalanceTestHook'] to a
|
||||
// closure($url, $apiKey): array in test code. Production code leaves it null.
|
||||
$GLOBALS['liveBalanceTestHook'] = $GLOBALS['liveBalanceTestHook'] ?? null;
|
||||
|
||||
/**
|
||||
* Checks if the database is new and empty.
|
||||
*
|
||||
@@ -343,66 +338,3 @@ function refetchRunningBalances($pdo, $user) {
|
||||
$url = $responseData['_metadata']['links']['next'] ?? null;
|
||||
} while ($url !== null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the live vault balance for a user from the v2 /money endpoint.
|
||||
*
|
||||
* Returns the `money.vault` value as an int, or null if the API call
|
||||
* fails, returns invalid JSON, or doesn't include `money.vault`. The
|
||||
* result is cached for the duration of the PHP request so multiple
|
||||
* callers (e.g., `vaultLoop` for one user, then the all-users sum)
|
||||
* don't re-fetch.
|
||||
*
|
||||
* @param string $user The user whose vault balance to fetch.
|
||||
*
|
||||
* @return int|null The vault amount in pennies, or null on failure.
|
||||
*/
|
||||
function fetchLiveVaultBalance($user) {
|
||||
static $prodCache = []; // production: hook is null, plain per-request array
|
||||
static $testCache = null; // test: keyed by hook closure (weakly held)
|
||||
|
||||
if (!array_key_exists($user, USER_KEYS)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hook = $GLOBALS['liveBalanceTestHook'] ?? null;
|
||||
|
||||
if ($hook === null) {
|
||||
// Production path: plain array cache.
|
||||
if (array_key_exists($user, $prodCache)) {
|
||||
return $prodCache[$user];
|
||||
}
|
||||
try {
|
||||
$responseData = executeApiCall('https://api.torn.com/v2/user?selections=money', USER_KEYS[$user]);
|
||||
if (!isset($responseData['money']['vault'])) {
|
||||
$prodCache[$user] = null;
|
||||
return null;
|
||||
}
|
||||
$prodCache[$user] = (int)$responseData['money']['vault'];
|
||||
return $prodCache[$user];
|
||||
} catch (Exception $e) {
|
||||
$prodCache[$user] = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Test path: key by hook identity so test scenarios get fresh fetches.
|
||||
if ($testCache === null) {
|
||||
$testCache = new \WeakMap();
|
||||
}
|
||||
if (isset($testCache[$hook]) && array_key_exists($user, $testCache[$hook])) {
|
||||
return $testCache[$hook][$user];
|
||||
}
|
||||
try {
|
||||
$responseData = $hook('https://api.torn.com/v2/user?selections=money', USER_KEYS[$user]);
|
||||
if (!isset($responseData['money']['vault'])) {
|
||||
$testCache[$hook] = [$user => null];
|
||||
return null;
|
||||
}
|
||||
$testCache[$hook] = [$user => (int)$responseData['money']['vault']];
|
||||
return $testCache[$hook][$user];
|
||||
} catch (Exception $e) {
|
||||
$testCache[$hook] = [$user => null];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user