mince/src/Users.php
2023-08-22 23:47:37 +00:00

41 lines
1.3 KiB
PHP

<?php
namespace Mince;
use RuntimeException;
use Index\Data\DbStatementCache;
use Index\Data\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 new UserInfo($result);
}
}