89 lines
3.2 KiB
PHP
89 lines
3.2 KiB
PHP
<?php
|
|
namespace Mince;
|
|
|
|
use InvalidArgumentException;
|
|
use RuntimeException;
|
|
use Index\Data\{DbStatementCache,IDbConnection};
|
|
|
|
class Skins {
|
|
public const MODELS = ['classic', 'slim'];
|
|
|
|
private DbStatementCache $cache;
|
|
|
|
public function __construct(IDbConnection $dbConn) {
|
|
$this->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 skins WHERE skin_hash = UNHEX(?)');
|
|
$stmt->addParameter(1, $hash);
|
|
$stmt->execute();
|
|
$result = $stmt->getResult();
|
|
return $result->next() && $result->getInteger(0) > 0;
|
|
}
|
|
|
|
public function getSkin(AccountLinkInfo|UserInfo|string $userInfo): ?SkinInfo {
|
|
if($userInfo instanceof AccountLinkInfo)
|
|
$userInfo = $userInfo->getUserId();
|
|
elseif($userInfo instanceof UserInfo)
|
|
$userInfo = $userInfo->getId();
|
|
|
|
$stmt = $this->cache->get('SELECT user_id, LOWER(HEX(skin_hash)), skin_model, UNIX_TIMESTAMP(skin_updated) FROM skins WHERE user_id = ?');
|
|
$stmt->addParameter(1, $userInfo);
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->getResult();
|
|
return $result->next() ? SkinInfo::fromResult($result) : null;
|
|
}
|
|
|
|
public function updateSkin(AccountLinkInfo|UserInfo|string $userInfo, string $hash, string $model): void {
|
|
if(!ctype_xdigit($hash) || strlen($hash) !== 64)
|
|
throw new InvalidArgumentException('$hash is not a valid hash string.');
|
|
if(!in_array($model, self::MODELS))
|
|
throw new InvalidArgumentException('$model is not a valid skin model.');
|
|
|
|
if($userInfo instanceof AccountLinkInfo)
|
|
$userInfo = $userInfo->getUserId();
|
|
elseif($userInfo instanceof UserInfo)
|
|
$userInfo = $userInfo->getId();
|
|
|
|
$stmt = $this->cache->get('REPLACE INTO skins (user_id, skin_hash, skin_model) VALUES (?, UNHEX(?), ?)');
|
|
$stmt->addParameter(1, $userInfo);
|
|
$stmt->addParameter(2, $hash);
|
|
$stmt->addParameter(3, $model);
|
|
$stmt->execute();
|
|
}
|
|
|
|
public function deleteSkin(
|
|
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 skins';
|
|
if($hasUserInfo) {
|
|
$query .= ' WHERE user_id = ?';
|
|
$value = $userInfo instanceof AccountLinkInfo
|
|
? $userInfo->getUserId()
|
|
: ($userInfo instanceof UserInfo ? $userInfo->getId() : $userInfo);
|
|
} elseif($hasHash) {
|
|
$query .= ' WHERE skin_hash = UNHEX(?)';
|
|
$value = $hash;
|
|
}
|
|
|
|
$stmt = $this->cache->get($query);
|
|
$stmt->addParameter(1, $value);
|
|
$stmt->execute();
|
|
}
|
|
}
|