Add SettingsSetDisplayName method and refactor unused imports

This commit is contained in:
netkas 2024-12-24 00:51:13 -05:00
parent 9da2ac2db2
commit 85bdff7d3c
15 changed files with 98 additions and 16 deletions

View file

@ -2,6 +2,7 @@
namespace Socialbox\Managers;
use InvalidArgumentException;
use PDO;
use PDOException;
use Socialbox\Classes\Configuration;
@ -317,6 +318,50 @@
}
}
/**
* Updates the display name of a registered peer based on the given unique identifier or RegisteredPeerRecord object.
*
* @param string|RegisteredPeerRecord $peer The unique identifier of the registered peer, or an instance of RegisteredPeerRecord.
* @param string $name The new
*/
public static function updateDisplayName(string|RegisteredPeerRecord $peer, string $name): void
{
if(empty($name))
{
throw new InvalidArgumentException('The display name cannot be empty');
}
if(strlen($name) > 256)
{
throw new InvalidArgumentException('The display name cannot exceed 256 characters');
}
if(is_string($peer))
{
$peer = self::getPeer($peer);
}
if($peer->isExternal())
{
throw new InvalidArgumentException('Cannot update the display name of an external peer');
}
Logger::getLogger()->verbose(sprintf("Updating display name of peer %s to %s", $peer->getUuid(), $name));
try
{
$statement = Database::getConnection()->prepare('UPDATE `registered_peers` SET display_name=? WHERE uuid=?');
$statement->bindParam(1, $name);
$uuid = $peer->getUuid();
$statement->bindParam(2, $uuid);
$statement->execute();
}
catch(PDOException $e)
{
throw new DatabaseOperationException('Failed to update the display name of the peer in the database', $e);
}
}
/**
* Retrieves the password authentication record associated with the given unique peer identifier or a RegisteredPeerRecord object.
*