cache = new DbStatementCache($dbConn); } public function checkHash(string $hash): bool { if(!ctype_xdigit($hash) || strlen($hash) !== 64) throw new InvalidArgumentException('$hash is not a valid hash string.'); $stmt = $this->cache->get('SELECT COUNT(*) FROM capes WHERE cape_hash = UNHEX(?)'); $stmt->addParameter(1, $hash); $stmt->execute(); $result = $stmt->getResult(); return $result->next() && $result->getInteger(0) > 0; } public function getCape(AccountLinkInfo|UserInfo|string $userInfo): ?CapeInfo { if($userInfo instanceof AccountLinkInfo) $userInfo = $userInfo->getUserId(); elseif($userInfo instanceof UserInfo) $userInfo = $userInfo->getId(); $stmt = $this->cache->get('SELECT user_id, LOWER(HEX(cape_hash)), UNIX_TIMESTAMP(cape_updated) FROM capes WHERE user_id = ?'); $stmt->addParameter(1, $userInfo); $stmt->execute(); $result = $stmt->getResult(); return $result->next() ? CapeInfo::fromResult($result) : null; } public function updateCape(AccountLinkInfo|UserInfo|string $userInfo, string $hash): void { if(!ctype_xdigit($hash) || strlen($hash) !== 64) throw new InvalidArgumentException('$hash is not a valid hash string.'); if($userInfo instanceof AccountLinkInfo) $userInfo = $userInfo->getUserId(); elseif($userInfo instanceof UserInfo) $userInfo = $userInfo->getId(); $stmt = $this->cache->get('REPLACE INTO capes (user_id, cape_hash) VALUES (?, UNHEX(?))'); $stmt->addParameter(1, $userInfo); $stmt->addParameter(2, $hash); $stmt->execute(); } public function deleteCape( AccountLinkInfo|UserInfo|string|null $userInfo = null, ?string $hash = null ): void { $hasUserInfo = $userInfo !== null; $hasHash = $hash !== null; if(!$hasUserInfo && !$hasHash) throw new InvalidArgumentException('At least one argument must be specified.'); if($hasUserInfo && $hasHash) throw new InvalidArgumentException('Only one argument may be specified.'); $value = null; $query = 'DELETE FROM capes'; if($hasUserInfo) { $query .= ' WHERE user_id = ?'; $value = $userInfo instanceof AccountLinkInfo ? $userInfo->getUserId() : ($userInfo instanceof UserInfo ? $userInfo->getId() : $userInfo); } elseif($hasHash) { $query .= ' WHERE cape_hash = UNHEX(?)'; $value = $hash; } $stmt = $this->cache->get($query); $stmt->addParameter(1, $value); $stmt->execute(); } }