diff --git a/.gitignore b/.gitignore index fe0b627..caf5f47 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/* +tests/cache ### Composer template composer.phar diff --git a/src/HypixelPHP.php b/src/HypixelPHP.php index 0f62166..a7a2a4e 100644 --- a/src/HypixelPHP.php +++ b/src/HypixelPHP.php @@ -4,7 +4,7 @@ use Closure; use Plancke\HypixelPHP\cache\CacheHandler; use Plancke\HypixelPHP\cache\CacheTimes; -use Plancke\HypixelPHP\cache\impl\flat\FlatFileCacheHandler; +use Plancke\HypixelPHP\cache\impl\FlatFileCacheHandler; use Plancke\HypixelPHP\classes\HypixelObject; use Plancke\HypixelPHP\exceptions\ExceptionCodes; use Plancke\HypixelPHP\exceptions\HypixelPHPException; @@ -128,7 +128,7 @@ public function getLogger() { */ public function setLogger(Logger $logger) { $this->logger = $logger; - $this->loggerGetter = function ($HypixelAPI) use ($logger) { + $this->loggerGetter = function () use ($logger) { return $logger; }; return $this; @@ -161,7 +161,7 @@ public function getFetcher() { */ public function setFetcher(Fetcher $fetcher) { $this->fetcher = $fetcher; - $this->fetcherGetter = function ($HypixelAPI) use ($fetcher) { + $this->fetcherGetter = function () use ($fetcher) { return $fetcher; }; return $this; @@ -194,7 +194,7 @@ public function getCacheHandler() { */ public function setCacheHandler(CacheHandler $cacheHandler) { $this->cacheHandler = $cacheHandler; - $this->cacheHandlerGetter = function ($HypixelAPI) use ($cacheHandler) { + $this->cacheHandlerGetter = function ($HypixelPHP) use ($cacheHandler) { return $cacheHandler; }; return $this; @@ -227,7 +227,7 @@ public function getProvider() { */ public function setProvider(Provider $provider) { $this->provider = $provider; - $this->providerGetter = function ($HypixelAPI) use ($provider) { + $this->providerGetter = function () use ($provider) { return $provider; }; return $this; @@ -260,7 +260,7 @@ public function getResourceManager() { */ public function setResourceManager(ResourceManager $resourceManager) { $this->resourceManager = $resourceManager; - $this->resourceManagerGetter = function ($HypixelAPI) use ($resourceManager) { + $this->resourceManagerGetter = function () use ($resourceManager) { return $resourceManager; }; return $this; diff --git a/src/cache/CacheHandler.php b/src/cache/CacheHandler.php index 4f493d1..830c715 100644 --- a/src/cache/CacheHandler.php +++ b/src/cache/CacheHandler.php @@ -2,6 +2,7 @@ namespace Plancke\HypixelPHP\cache; +use Closure; use Plancke\HypixelPHP\classes\HypixelObject; use Plancke\HypixelPHP\classes\Module; use Plancke\HypixelPHP\exceptions\ExceptionCodes; @@ -92,7 +93,7 @@ public function setCacheTime($for, $int) { * @param HypixelObject $hypixelObject * @throws HypixelPHPException */ - function _setCache($hypixelObject) { + public function _setCache($hypixelObject) { if ($hypixelObject instanceof Player) { $this->setCachedPlayer($hypixelObject); } elseif ($hypixelObject instanceof Guild) { @@ -114,35 +115,90 @@ function _setCache($hypixelObject) { } } - //@formatter:off + protected function objToArray($obj) { + if ($obj instanceof HypixelObject) { + return $obj->getRaw(); + } + return $obj; + } + + protected function wrapProvider(Closure $provider, $data) { + if ($data == null) { + return null; + } + return $provider($this->getHypixelPHP(), $data); + } + abstract function setCachedPlayer(Player $player); + + /** + * @param $uuid + * @return Player + */ abstract function getCachedPlayer($uuid); + abstract function setPlayerUUID($username, $obj); + abstract function getUUID($username); abstract function setCachedGuild(Guild $guild); + + /** + * @param $id + * @return Guild + */ abstract function getCachedGuild($id); + abstract function setGuildIDForUUID($uuid, $obj); + abstract function getGuildIDForUUID($uuid); + abstract function setGuildIDForName($name, $obj); + abstract function getGuildIDForName($name); abstract function setCachedFriends(Friends $friends); + + /** + * @param $uuid + * @return Friends + */ abstract function getCachedFriends($uuid); abstract function setCachedSession(Session $session); + + /** + * @param $uuid + * @return Session + */ abstract function getCachedSession($uuid); abstract function setCachedKeyInfo(KeyInfo $keyInfo); + + /** + * @param $key + * @return KeyInfo + */ abstract function getCachedKeyInfo($key); abstract function setCachedLeaderboards(Leaderboards $leaderboards); + + /** + * @return Leaderboards + */ abstract function getCachedLeaderboards(); abstract function setCachedBoosters(Boosters $boosters); + + /** + * @return Boosters + */ abstract function getCachedBoosters(); abstract function setCachedWatchdogStats(WatchdogStats $watchdogStats); + + /** + * @return WatchdogStats + */ abstract function getCachedWatchdogStats(); - //@formatter:on } \ No newline at end of file diff --git a/src/cache/impl/mongo/CollectionNames.php b/src/cache/CacheTypes.php similarity index 58% rename from src/cache/impl/mongo/CollectionNames.php rename to src/cache/CacheTypes.php index 2b5f1a1..d77f6f8 100644 --- a/src/cache/impl/mongo/CollectionNames.php +++ b/src/cache/CacheTypes.php @@ -1,8 +1,8 @@ baseDirectory; + } + + /** + * @param string $baseDirectory + * @return $this + */ + public function setBaseDirectory($baseDirectory) { + $this->baseDirectory = $baseDirectory; + return $this; + } + + /** + * @param $filename + * @param HypixelObject $obj + */ + protected function setObjCache($filename, HypixelObject $obj) { + $this->setCache($filename, $this->objToArray($obj)); + } + + /** + * @param $filename + * @param $obj + */ + protected function setCache($filename, $obj) { + Utilities::setFileContent($this->baseDirectory . DIRECTORY_SEPARATOR . $filename . '.json', json_encode($obj)); + } + + /** + * @param $filename + * @return array|null + */ + protected function getCache($filename) { + $content = Utilities::getFileContent($this->baseDirectory . DIRECTORY_SEPARATOR . $filename . '.json'); + if ($content == null) { + return null; + } + return json_decode($content, true); + } + + function setCachedPlayer(Player $player) { + $this->setObjCache(CacheTypes::PLAYERS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($player->getUUID()), $player); + } + + function getCachedPlayer($uuid) { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getPlayer(), + $data = $this->getCache(CacheTypes::PLAYERS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid)) + ); + } + + function setPlayerUUID($username, $obj) { + $this->setCache(CacheTypes::PLAYER_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($username), $obj); + } + + function getUUID($username) { + $data = $this->getCache(CacheTypes::PLAYER_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($username)); + if ($data != null) { + if (isset($data['uuid']) && $data['uuid'] != null && $data['uuid'] != '') { + $cacheTime = $this->getCacheTime(CacheTimes::UUID); + } else { + $cacheTime = $this->getCacheTime(CacheTimes::UUID_NOT_FOUND); + } + $timestamp = array_key_exists('timestamp', $data) ? $data['timestamp'] : 0; + $diff = time() - $cacheTime - $timestamp; + + $this->getHypixelPHP()->getLogger()->log("Found name match in PLAYER_UUID! '$diff'"); + + if ($diff < 0) { + return $data['uuid']; + } + } + + return null; + } + + function setCachedGuild(Guild $guild) { + $this->setObjCache(CacheTypes::GUILDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($guild->getID()), $guild); + } + + function getCachedGuild($id) { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getGuild(), + $data = $this->getCache(CacheTypes::GUILDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($id)) + ); + } + + function setGuildIDForUUID($uuid, $obj) { + $this->setCache(CacheTypes::GUILDS_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid), $obj); + } + + function getGuildIDForUUID($uuid) { + return $this->getCache(CacheTypes::GUILDS_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid)); + } + + function setGuildIDForName($name, $obj) { + $this->setCache(CacheTypes::GUILDS_NAME . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($name), $obj); + } + + function getGuildIDForName($name) { + return $this->getCache(CacheTypes::GUILDS_NAME . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($name)); + } + + function setCachedFriends(Friends $friends) { + $this->setObjCache(CacheTypes::FRIENDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($friends->getUUID()), $friends); + } + + function getCachedFriends($uuid) { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getFriends(), + $this->getCache(CacheTypes::FRIENDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid)) + ); + } + + function setCachedSession(Session $session) { + $this->setObjCache(CacheTypes::SESSIONS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($session->getUUID()), $session); + } + + function getCachedSession($uuid) { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getSession(), + $this->getCache(CacheTypes::SESSIONS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid)) + ); + } + + function setCachedKeyInfo(KeyInfo $keyInfo) { + $this->setObjCache(CacheTypes::API_KEYS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($keyInfo->getKey()), $keyInfo); + } + + function getCachedKeyInfo($key) { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getKeyInfo(), + $this->getCache(CacheTypes::SESSIONS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($key)) + ); + } + + function setCachedLeaderboards(Leaderboards $leaderboards) { + $this->setObjCache(CacheTypes::LEADERBOARDS, $leaderboards); + } + + function getCachedLeaderboards() { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getLeaderboards(), + $this->getCache(CacheTypes::LEADERBOARDS) + ); + } + + function setCachedBoosters(Boosters $boosters) { + $this->setObjCache(CacheTypes::BOOSTERS, $boosters); + } + + function getCachedBoosters() { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getBoosters(), + $this->getCache(CacheTypes::BOOSTERS) + ); + } + + function setCachedWatchdogStats(WatchdogStats $watchdogStats) { + $this->setObjCache(CacheTypes::WATCHDOG_STATS, $watchdogStats); + } + + function getCachedWatchdogStats() { + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getWatchdogStats(), + $this->getCache(CacheTypes::WATCHDOG_STATS) + ); + } +} \ No newline at end of file diff --git a/src/cache/impl/mongo/MongoCacheHandler.php b/src/cache/impl/MongoCacheHandler.php similarity index 60% rename from src/cache/impl/mongo/MongoCacheHandler.php rename to src/cache/impl/MongoCacheHandler.php index d9bf8cf..0446796 100644 --- a/src/cache/impl/mongo/MongoCacheHandler.php +++ b/src/cache/impl/MongoCacheHandler.php @@ -1,10 +1,11 @@ selectDB(); - $db->selectCollection(CollectionNames::API_KEYS)->createIndex(['record.key' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::API_KEYS)->createIndex(['record.key' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::PLAYERS)->createIndex(['record.uuid' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::PLAYERS)->createIndex(['record.playername' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::PLAYER_UUID)->createIndex(['name_lowercase' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::PLAYERS)->createIndex(['record.uuid' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::PLAYERS)->createIndex(['record.playername' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::PLAYER_UUID)->createIndex(['name_lowercase' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::GUILDS)->createIndex(['record._id' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::GUILDS)->createIndex(['extra.name_lower' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::GUILDS_UUID)->createIndex(['uuid' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::GUILDS_NAME)->createIndex(['name_lower' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::GUILDS)->createIndex(['record._id' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::GUILDS)->createIndex(['extra.name_lower' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::GUILDS_UUID)->createIndex(['uuid' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::GUILDS_NAME)->createIndex(['name_lower' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::FRIENDS)->createIndex(['record.uuid' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::FRIENDS)->createIndex(['record.uuid' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::SESSIONS)->createIndex(['record.uuid' => 1], ['background' => true]); + $db->selectCollection(CacheTypes::SESSIONS)->createIndex(['record.uuid' => 1], ['background' => true]); - $db->selectCollection(CollectionNames::SINGLE_SAVE)->createIndex(['key' => 1], ['background' => true]); + $db->selectCollection(MongoCacheHandler::SINGLE_SAVE)->createIndex(['key' => 1], ['background' => true]); return $this; } @@ -76,13 +79,6 @@ public function updateCollection($collection, $query, $obj) { $this->selectDB()->selectCollection($collection)->replaceOne($query, $this->objToArray($obj), ['upsert' => true]); } - protected function objToArray($obj) { - if ($obj instanceof HypixelObject) { - return $obj->getRaw(); - } - return $obj; - } - /** * @param $collection * @param $query @@ -108,7 +104,7 @@ function setSingleSave($key, HypixelObject $hypixelObject) { $raw = $hypixelObject->getRaw(); $raw['key'] = $key; - $this->updateCollection(CollectionNames::SINGLE_SAVE, $query, $raw); + $this->updateCollection(MongoCacheHandler::SINGLE_SAVE, $query, $raw); } /** @@ -118,26 +114,20 @@ function setSingleSave($key, HypixelObject $hypixelObject) { */ function getSingleSave($key, $constructor) { $query = ['key' => $key]; - $data = $this->queryCollection(CollectionNames::SINGLE_SAVE, $query); - if ($data != null) { - return $constructor($this->getHypixelPHP(), $data); - } - return null; + return $this->wrapProvider($constructor, $this->queryCollection(MongoCacheHandler::SINGLE_SAVE, $query)); } public function setCachedPlayer(Player $player) { $query = ['record.uuid' => (string)$player->getUUID()]; - $this->updateCollection(CollectionNames::PLAYERS, $query, $player); + $this->updateCollection(CacheTypes::PLAYERS, $query, $player); } function getCachedPlayer($uuid) { $query = ['record.uuid' => (string)$uuid]; - $data = $this->queryCollection(CollectionNames::PLAYERS, $query); - if ($data != null) { - $closure = $this->getHypixelPHP()->getProvider()->getPlayer(); - return $closure($this->getHypixelPHP(), $data); - } - return null; + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getPlayer(), + $this->queryCollection(CacheTypes::PLAYERS, $query) + ); } function setPlayerUUID($username, $obj) { @@ -145,9 +135,9 @@ function setPlayerUUID($username, $obj) { if ($obj['uuid'] == '' || $obj['uuid'] == null) { // still not found, just update time so we don't keep fetching // $this->updateCollection(CollectionNames::PLAYER_UUID, $query, ['$set' => [['timestamp' => time()]]]); - $this->selectDB()->selectCollection(CollectionNames::PLAYER_UUID)->updateOne($query, ['$set' => ['timestamp' => time()]]); + $this->selectDB()->selectCollection(CacheTypes::PLAYER_UUID)->updateOne($query, ['$set' => ['timestamp' => time()]]); } else { - $this->updateCollection(CollectionNames::PLAYER_UUID, $query, $obj); + $this->updateCollection(CacheTypes::PLAYER_UUID, $query, $obj); } } @@ -156,7 +146,7 @@ function getUUID($username) { // check if player_uuid collection has record $query = ['name_lowercase' => $username]; - $data = $this->queryCollection(CollectionNames::PLAYER_UUID, $query); + $data = $this->queryCollection(CacheTypes::PLAYER_UUID, $query); if ($data != null) { if (isset($data['uuid']) && $data['uuid'] != null && $data['uuid'] != '') { $cacheTime = $this->getCacheTime(CacheTimes::UUID); @@ -175,7 +165,7 @@ function getUUID($username) { // check if player database has a player for it $query = ['record.playername' => $username]; - $data = $this->queryCollection(CollectionNames::PLAYERS, $query); + $data = $this->queryCollection(CacheTypes::PLAYERS, $query); if ($data != null) { $timestamp = array_key_exists('timestamp', $data) ? $data['timestamp'] : 0; $diff = time() - $this->getCacheTime(CacheTimes::UUID) - $timestamp; @@ -193,27 +183,25 @@ function getUUID($username) { function setCachedGuild(Guild $guild) { $query = ['record._id' => (string)$guild->getID()]; - $this->updateCollection(CollectionNames::GUILDS, $query, $guild); + $this->updateCollection(CacheTypes::GUILDS, $query, $guild); } function getCachedGuild($id) { $query = ['record._id' => (string)$id]; - $data = $this->queryCollection(CollectionNames::GUILDS, $query); - if ($data != null) { - $closure = $this->getHypixelPHP()->getProvider()->getGuild(); - return $closure($this->getHypixelPHP(), $data); - } - return null; + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getGuild(), + $this->queryCollection(CacheTypes::GUILDS, $query) + ); } function setGuildIDForUUID($uuid, $obj) { $query = ['uuid' => (string)$uuid]; - $this->updateCollection(CollectionNames::GUILDS_UUID, $query, $obj); + $this->updateCollection(CacheTypes::GUILDS_UUID, $query, $obj); } function getGuildIDForUUID($uuid) { $query = ['uuid' => (string)$uuid]; - $data = $this->queryCollection(CollectionNames::GUILDS_UUID, $query); + $data = $this->queryCollection(CacheTypes::GUILDS_UUID, $query); if ($data != null) { if (isset($data['uuid']) && $data['uuid'] != null && $data['uuid'] != '') { $cacheTime = $this->getCacheTime(CacheTimes::GUILD); @@ -232,25 +220,24 @@ function getGuildIDForUUID($uuid) { function setGuildIDForName($name, $obj) { $query = ['name_lower' => strtolower((string)$name)]; - $this->updateCollection(CollectionNames::GUILDS_NAME, $query, $obj); + $this->updateCollection(CacheTypes::GUILDS_NAME, $query, $obj); } function getGuildIDForName($name) { $query = ['extra.name_lower' => strtolower((string)$name)]; - $data = $this->queryCollection(CollectionNames::GUILDS, $query); + $data = $this->queryCollection(CacheTypes::GUILDS, $query); if ($data != null) { $cacheTime = $this->getCacheTime(CacheTimes::GUILD); $timestamp = array_key_exists('timestamp', $data) ? $data['timestamp'] : 0; $diff = time() - $cacheTime - $timestamp; if ($diff < 0) { - $closure = $this->getHypixelPHP()->getProvider()->getGuild(); - return $closure($this->getHypixelPHP(), $data); + return $this->wrapProvider($this->getHypixelPHP()->getProvider()->getGuild(), $data); } } $query = ['name_lower' => strtolower((string)$name)]; - $data = $this->queryCollection(CollectionNames::GUILDS_NAME, $query); + $data = $this->queryCollection(CacheTypes::GUILDS_NAME, $query); if ($data != null) { if (isset($data['name_lower']) && $data['name_lower'] != null && $data['name_lower'] != '') { $cacheTime = $this->getCacheTime(CacheTimes::GUILD); @@ -269,70 +256,64 @@ function getGuildIDForName($name) { function setCachedFriends(Friends $friends) { $query = ['record.uuid' => (string)$friends->getUUID()]; - $this->updateCollection(CollectionNames::FRIENDS, $query, $friends); + $this->updateCollection(CacheTypes::FRIENDS, $query, $friends); } function getCachedFriends($uuid) { $query = ['record.uuid' => (string)$uuid]; - $data = $this->queryCollection(CollectionNames::FRIENDS, $query); - if ($data != null) { - $closure = $this->getHypixelPHP()->getProvider()->getFriends(); - return $closure($this->getHypixelPHP(), $data); - } - return null; + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getFriends(), + $this->queryCollection(CacheTypes::FRIENDS, $query) + ); } function setCachedSession(Session $session) { $query = ['record.uuid' => (string)$session->getUUID()]; - $this->updateCollection(CollectionNames::SESSIONS, $query, $session); + $this->updateCollection(CacheTypes::SESSIONS, $query, $session); } function getCachedSession($uuid) { $query = ['record.uuid' => (string)$uuid]; - $data = $this->queryCollection(CollectionNames::SESSIONS, $query); - if ($data != null) { - $closure = $this->getHypixelPHP()->getProvider()->getSession(); - return $closure($this->getHypixelPHP(), $data); - } - return null; + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getSession(), + $this->queryCollection(CacheTypes::SESSIONS, $query) + ); } function setCachedKeyInfo(KeyInfo $keyInfo) { $query = ['record.key' => (string)$keyInfo->getKey()]; - $this->updateCollection(CollectionNames::API_KEYS, $query, $keyInfo); + $this->updateCollection(CacheTypes::API_KEYS, $query, $keyInfo); } function getCachedKeyInfo($key) { $query = ['record.key' => (string)$key]; - $data = $this->queryCollection(CollectionNames::API_KEYS, $query); - if ($data != null) { - $closure = $this->getHypixelPHP()->getProvider()->getKeyInfo(); - return $closure($this->getHypixelPHP(), $data); - } - return null; + return $this->wrapProvider( + $this->getHypixelPHP()->getProvider()->getKeyInfo(), + $this->queryCollection(CacheTypes::API_KEYS, $query) + ); } function setCachedLeaderboards(Leaderboards $leaderboards) { - $this->setSingleSave(SingleSaveKeys::LEADERBOARDS, $leaderboards); + $this->setSingleSave(CacheTypes::LEADERBOARDS, $leaderboards); } function getCachedLeaderboards() { - return $this->getSingleSave(SingleSaveKeys::LEADERBOARDS, $this->getHypixelPHP()->getProvider()->getLeaderboards()); + return $this->getSingleSave(CacheTypes::LEADERBOARDS, $this->getHypixelPHP()->getProvider()->getLeaderboards()); } function setCachedBoosters(Boosters $boosters) { - $this->setSingleSave(SingleSaveKeys::BOOSTERS, $boosters); + $this->setSingleSave(CacheTypes::BOOSTERS, $boosters); } function getCachedBoosters() { - return $this->getSingleSave(SingleSaveKeys::BOOSTERS, $this->getHypixelPHP()->getProvider()->getBoosters()); + return $this->getSingleSave(CacheTypes::BOOSTERS, $this->getHypixelPHP()->getProvider()->getBoosters()); } function setCachedWatchdogStats(WatchdogStats $watchdogStats) { - $this->setSingleSave(SingleSaveKeys::WATCHDOG_STATS, $watchdogStats); + $this->setSingleSave(CacheTypes::WATCHDOG_STATS, $watchdogStats); } function getCachedWatchdogStats() { - return $this->getSingleSave(SingleSaveKeys::WATCHDOG_STATS, $this->getHypixelPHP()->getProvider()->getWatchdogStats()); + return $this->getSingleSave(CacheTypes::WATCHDOG_STATS, $this->getHypixelPHP()->getProvider()->getWatchdogStats()); } } \ No newline at end of file diff --git a/src/cache/impl/flat/FlatFileCacheHandler.php b/src/cache/impl/flat/FlatFileCacheHandler.php deleted file mode 100644 index f5e7d14..0000000 --- a/src/cache/impl/flat/FlatFileCacheHandler.php +++ /dev/null @@ -1,133 +0,0 @@ -getRaw()); - Utilities::setFileContent($filename, $content); - } - - /** - * @param $filename - * @return array|null - */ - public function getCache($filename) { - $content = Utilities::getFileContent($filename); - if ($content == null) { - return null; - } - return json_decode($content, true); - } - - function setCachedPlayer(Player $player) { - // TODO: Implement setCachedPlayer() method. - } - - function getCachedPlayer($uuid) { - // TODO: Implement getCachedPlayer() method. - } - - function setPlayerUUID($username, $obj) { - // TODO: Implement setPlayerUUID() method. - } - - function getUUID($username) { - // TODO: Implement getUUID() method. - } - - function setCachedGuild(Guild $guild) { - // TODO: Implement setCachedGuild() method. - } - - function getCachedGuild($id) { - // TODO: Implement getCachedGuild() method. - } - - function setGuildIDForUUID($uuid, $obj) { - // TODO: Implement setGuildIDForUUID() method. - } - - function getGuildIDForUUID($uuid) { - // TODO: Implement getGuildIDForUUID() method. - } - - function setGuildIDForName($name, $obj) { - // TODO: Implement setGuildIDForName() method. - } - - function getGuildIDForName($name) { - // TODO: Implement getGuildIDForName() method. - } - - function setCachedFriends(Friends $friends) { - // TODO: Implement setCachedFriends() method. - } - - function getCachedFriends($uuid) { - // TODO: Implement getCachedFriends() method. - } - - function setCachedSession(Session $session) { - // TODO: Implement setCachedSession() method. - } - - function getCachedSession($uuid) { - // TODO: Implement getCachedSession() method. - } - - function setCachedKeyInfo(KeyInfo $keyInfo) { - // TODO: Implement setCachedKeyInfo() method. - } - - function getCachedKeyInfo($key) { - // TODO: Implement getCachedKeyInfo() method. - } - - function setCachedLeaderboards(Leaderboards $leaderboards) { - // TODO: Implement setCachedLeaderboards() method. - } - - function getCachedLeaderboards() { - // TODO: Implement getCachedLeaderboards() method. - } - - function setCachedBoosters(Boosters $boosters) { - // TODO: Implement setCachedBoosters() method. - } - - function getCachedBoosters() { - // TODO: Implement getCachedBoosters() method. - } - - function setCachedWatchdogStats(WatchdogStats $watchdogStats) { - // TODO: Implement setCachedWatchdogStats() method. - } - - function getCachedWatchdogStats() { - // TODO: Implement getCachedWatchdogStats() method. - } -} \ No newline at end of file diff --git a/src/cache/impl/mongo/SingleSaveKeys.php b/src/cache/impl/mongo/SingleSaveKeys.php deleted file mode 100644 index c48dd73..0000000 --- a/src/cache/impl/mongo/SingleSaveKeys.php +++ /dev/null @@ -1,11 +0,0 @@ -getPlayer([FetchParams::PLAYER_BY_NAME => "Plancke"]); + $player = TestUtil::getHypixelPHP()->getPlayer([FetchParams::PLAYER_BY_UUID => TestUtil::PLANCKE]); $this->assertTrue($player instanceof Player); $this->assertTrue($player->getStats() instanceof Stats); } function testGuildResponse() { - $this->assertTrue(TestUtil::getHypixelPHP()->getGuild([FetchParams::GUILD_BY_PLAYER_NAME => "Plancke"]) instanceof Guild); + $this->assertTrue(TestUtil::getHypixelPHP()->getGuild([FetchParams::GUILD_BY_PLAYER_UUID => TestUtil::PLANCKE]) instanceof Guild); } } diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 57d98e2..8f360e4 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -2,7 +2,11 @@ namespace Plancke\Tests; +use Plancke\HypixelPHP\cache\impl\FlatFileCacheHandler; +use Plancke\HypixelPHP\fetch\FetchParams; +use Plancke\HypixelPHP\responses\player\Player; use Plancke\HypixelPHP\util\CacheUtil; +use Plancke\Tests\util\TestUtil; class CacheTest extends \PHPUnit_Framework_TestCase { @@ -19,4 +23,11 @@ function testExpire() { $this->assertTrue($remain >= 2000 && $remain <= 3000); } + function testFlatFile() { + $player = TestUtil::getHypixelPHP() + ->setCacheHandler(new FlatFileCacheHandler(TestUtil::getHypixelPHP())) + ->getPlayer([FetchParams::PLAYER_BY_NAME => "Plancke"]); + $this->assertTrue($player instanceof Player); + $this->assertTrue($player->getUUID() == TestUtil::PLANCKE); + } } \ No newline at end of file diff --git a/tests/util/TestUtil.php b/tests/util/TestUtil.php index a2b56d3..6585509 100644 --- a/tests/util/TestUtil.php +++ b/tests/util/TestUtil.php @@ -6,6 +6,8 @@ class TestUtil { + const PLANCKE = 'f025c1c7f55a4ea0b8d93f47d17dfe0f'; + static function getHypixelPHP() { $HypixelPHP = new HypixelPHP(''); // log to error