mince/src/Users.php
2024-02-21 16:08:45 +00:00

40 lines
1.3 KiB
PHP

<?php
namespace Mince;
use RuntimeException;
use Index\Data\{DbStatementCache,IDbConnection};
class Users {
private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) {
$this->cache = new DbStatementCache($dbConn);
}
public function syncChatUser(object $authInfo): void {
if(!$authInfo->success)
return;
$userColourFixed = ($authInfo->colour_raw & 0x40000000) ? null : $authInfo->colour_raw;
$stmt = $this->cache->get('INSERT INTO users (user_id, user_name, user_colour) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE user_name = ?, user_colour = ?');
$stmt->addParameter(1, $authInfo->user_id);
$stmt->addParameter(2, $authInfo->username);
$stmt->addParameter(3, $userColourFixed);
$stmt->addParameter(4, $authInfo->username);
$stmt->addParameter(5, $userColourFixed);
$stmt->execute();
}
public function getUser(string $userId): UserInfo {
$stmt = $this->cache->get('SELECT user_id, user_name, user_colour FROM users WHERE user_id = ?');
$stmt->addParameter(1, $userId);
$stmt->execute();
$result = $stmt->getResult();
if(!$result->next())
throw new RuntimeException('User info not found.');
return UserInfo::fromResult($result);
}
}