122 lines
4.1 KiB
PHP
122 lines
4.1 KiB
PHP
<?php
|
|
namespace Mince;
|
|
|
|
use InvalidArgumentException;
|
|
use RuntimeException;
|
|
use Index\Data\{DbStatementCache,IDbConnection};
|
|
use Ramsey\Uuid\UuidInterface;
|
|
|
|
class AccountLinks {
|
|
private DbStatementCache $cache;
|
|
|
|
public function __construct(IDbConnection $dbConn) {
|
|
$this->cache = new DbStatementCache($dbConn);
|
|
}
|
|
|
|
public function checkHasLink(UserInfo|string $userInfo): bool {
|
|
if($userInfo instanceof UserInfo)
|
|
$userInfo = $userInfo->getId();
|
|
|
|
$stmt = $this->cache->get('SELECT COUNT(*) FROM links WHERE user_id = ?');
|
|
$stmt->addParameter(1, $userInfo);
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->getResult();
|
|
return $result->next() && $result->getInteger(0) > 0;
|
|
}
|
|
|
|
public function getLink(
|
|
UserInfo|string|null $userInfo = null,
|
|
UuidInterface|string|null $uuid = null,
|
|
?string $name = null
|
|
): AccountLinkInfo {
|
|
$hasUserInfo = $userInfo !== null;
|
|
$hasUuid = $uuid !== null;
|
|
$hasName = $name !== null;
|
|
|
|
if(!$hasUserInfo && !$hasUuid && !$hasName)
|
|
throw new InvalidArgumentException('At least one argument must be specified.');
|
|
if(($hasUserInfo && ($hasUuid || $hasName))
|
|
|| ($hasUuid && ($hasUserInfo || $hasName))
|
|
|| ($hasName && ($hasUuid || $hasUserInfo)))
|
|
throw new InvalidArgumentException('Only one argument may be used.');
|
|
|
|
$field = null;
|
|
$value = null;
|
|
|
|
if($hasUserInfo) {
|
|
$field = 'user_id';
|
|
$value = $userInfo instanceof UserInfo ? $userInfo->getId() : $userInfo;
|
|
} elseif($hasUuid) {
|
|
$field = 'link_uuid';
|
|
$value = $uuid instanceof UuidInterface ? $uuid->getBytes() : $uuid;
|
|
} elseif($hasName) {
|
|
$field = 'link_name';
|
|
$value = $name;
|
|
}
|
|
|
|
$stmt = $this->cache->get(sprintf('SELECT user_id, link_uuid, link_name, UNIX_TIMESTAMP(link_created) FROM links WHERE %s = ?', $field));
|
|
$stmt->addParameter(1, $value);
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->getResult();
|
|
if(!$result->next())
|
|
throw new RuntimeException('Link info not found.');
|
|
|
|
return AccountLinkInfo::fromResult($result);
|
|
}
|
|
|
|
public function createLink(
|
|
UserInfo|string $userInfo,
|
|
VerificationInfo|UuidInterface|string $uuid,
|
|
?string $name = null
|
|
): void {
|
|
if($userInfo instanceof UserInfo)
|
|
$userInfo = $userInfo->getId();
|
|
if($uuid instanceof VerificationInfo) {
|
|
$name = $uuid->getName();
|
|
$uuid = $uuid->getUUIDRaw();
|
|
} else {
|
|
if($name === null)
|
|
throw new InvalidArgumentException('$name may not be null (unless $uuid is a valid VerificationInfo instance)');
|
|
|
|
if($uuid instanceof UuidInterface)
|
|
$uuid = $uuid->getBytes();
|
|
}
|
|
|
|
$stmt = $this->cache->get('INSERT INTO links (user_id, link_uuid, link_name) VALUES (?, ?, ?)');
|
|
$stmt->addParameter(1, $userInfo);
|
|
$stmt->addParameter(2, $uuid);
|
|
$stmt->addParameter(3, $name);
|
|
$stmt->execute();
|
|
}
|
|
|
|
public function deleteLink(
|
|
UserInfo|string|null $userInfo = null,
|
|
UuidInterface|string|null $uuid = null
|
|
): void {
|
|
$hasUserInfo = $userInfo !== null;
|
|
$hasUuid = $uuid !== null;
|
|
|
|
if(!$hasUserInfo && !$hasUuid)
|
|
throw new InvalidArgumentException('At least one argument must be specified.');
|
|
if($hasUserInfo && $hasUuid)
|
|
throw new InvalidArgumentException('Only one argument may be used.');
|
|
|
|
$name = null;
|
|
$value = null;
|
|
|
|
if($hasUserInfo) {
|
|
$name = 'user_id';
|
|
$value = $userInfo instanceof UserInfo ? $userInfo->getId() : $userInfo;
|
|
} elseif($hasUuid) {
|
|
$name = 'link_uuid';
|
|
$value = $uuid instanceof UuidInterface ? $uuid->getBytes() : $uuid;
|
|
}
|
|
|
|
$stmt = $this->cache->get(sprintf('DELETE FROM links WHERE %s = ?', $name));
|
|
$stmt->addParameter(1, $value);
|
|
$stmt->execute();
|
|
}
|
|
}
|