Refactor EntitiesManager to use local exceptions and improve error handling
This commit is contained in:
parent
2b59713253
commit
439316de55
1 changed files with 23 additions and 20 deletions
|
@ -5,6 +5,9 @@
|
||||||
use FederationServer\Classes\DatabaseConnection;
|
use FederationServer\Classes\DatabaseConnection;
|
||||||
use FederationServer\Exceptions\DatabaseOperationException;
|
use FederationServer\Exceptions\DatabaseOperationException;
|
||||||
use FederationServer\Objects\EntityRecord;
|
use FederationServer\Objects\EntityRecord;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
|
|
||||||
class EntitiesManager
|
class EntitiesManager
|
||||||
{
|
{
|
||||||
|
@ -13,22 +16,22 @@
|
||||||
*
|
*
|
||||||
* @param string $id The ID of the entity.
|
* @param string $id The ID of the entity.
|
||||||
* @param string|null $domain The domain of the entity, can be null.
|
* @param string|null $domain The domain of the entity, can be null.
|
||||||
* @throws \InvalidArgumentException If the ID exceeds 255 characters or if the domain is invalid.
|
* @throws InvalidArgumentException If the ID exceeds 255 characters or if the domain is invalid.
|
||||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||||
*/
|
*/
|
||||||
public static function registerEntity(string $id, ?string $domain=null): void
|
public static function registerEntity(string $id, ?string $domain=null): void
|
||||||
{
|
{
|
||||||
if(strlen($id) > 255)
|
if(strlen($id) > 255)
|
||||||
{
|
{
|
||||||
throw new \InvalidArgumentException("Entity ID cannot exceed 255 characters.");
|
throw new InvalidArgumentException("Entity ID cannot exceed 255 characters.");
|
||||||
}
|
}
|
||||||
if(!filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) && $domain !== null)
|
if(!filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) && $domain !== null)
|
||||||
{
|
{
|
||||||
throw new \InvalidArgumentException("Invalid domain format.");
|
throw new InvalidArgumentException("Invalid domain format.");
|
||||||
}
|
}
|
||||||
if(strlen($domain) > 255)
|
if(strlen($domain) > 255)
|
||||||
{
|
{
|
||||||
throw new \InvalidArgumentException("Domain cannot exceed 255 characters.");
|
throw new InvalidArgumentException("Domain cannot exceed 255 characters.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -38,25 +41,26 @@
|
||||||
$stmt->bindParam(':domain', $domain);
|
$stmt->bindParam(':domain', $domain);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
catch (\PDOException $e)
|
catch (PDOException $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException("Failed to register entity: " . $e->getMessage(), $e->getCode(), $e);
|
throw new DatabaseOperationException("Failed to register entity: " . $e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves an entity by its ID and domain.
|
* Retrieves an entity by its ID and domain.
|
||||||
*
|
*
|
||||||
* @param string $id The ID of the entity.
|
* @param string $id The ID of the entity.
|
||||||
* @param string $domain The domain of the entity.
|
* @param string $domain The domain of the entity.
|
||||||
* @return EntityRecord|null The EntityRecord object if found, null otherwise.
|
* @return EntityRecord|null The EntityRecord object if found, null otherwise.
|
||||||
* @throws \InvalidArgumentException If the ID or domain is not provided or is invalid.
|
* @throws InvalidArgumentException If the ID or domain is not provided or is invalid.
|
||||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||||
*/
|
*/
|
||||||
public static function getEntityByDomain(string $id, string $domain): ?EntityRecord
|
public static function getEntityByDomain(string $id, string $domain): ?EntityRecord
|
||||||
{
|
{
|
||||||
if(strlen($id) < 1 || strlen($domain) < 1)
|
if(strlen($id) < 1 || strlen($domain) < 1)
|
||||||
{
|
{
|
||||||
throw new \InvalidArgumentException("Entity ID and domain must be provided.");
|
throw new InvalidArgumentException("Entity ID and domain must be provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -66,14 +70,14 @@
|
||||||
$stmt->bindParam(':domain', $domain);
|
$stmt->bindParam(':domain', $domain);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
|
|
||||||
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
|
$data = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
if($data)
|
if($data)
|
||||||
{
|
{
|
||||||
return new EntityRecord($data);
|
return new EntityRecord($data);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (\PDOException $e)
|
catch (PDOException $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException("Failed to retrieve entity by domain: " . $e->getMessage(), $e->getCode(), $e);
|
throw new DatabaseOperationException("Failed to retrieve entity by domain: " . $e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
|
@ -84,14 +88,14 @@
|
||||||
*
|
*
|
||||||
* @param string $uuid The UUID of the entity.
|
* @param string $uuid The UUID of the entity.
|
||||||
* @return EntityRecord|null The EntityRecord object if found, null otherwise.
|
* @return EntityRecord|null The EntityRecord object if found, null otherwise.
|
||||||
* @throws \InvalidArgumentException If the UUID is not provided or is invalid.
|
* @throws InvalidArgumentException If the UUID is not provided or is invalid.
|
||||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||||
*/
|
*/
|
||||||
public static function getEntityByUuid(string $uuid): ?EntityRecord
|
public static function getEntityByUuid(string $uuid): ?EntityRecord
|
||||||
{
|
{
|
||||||
if(strlen($uuid) < 1)
|
if(strlen($uuid) < 1)
|
||||||
{
|
{
|
||||||
throw new \InvalidArgumentException("Entity UUID must be provided.");
|
throw new InvalidArgumentException("Entity UUID must be provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -100,14 +104,14 @@
|
||||||
$stmt->bindParam(':uuid', $uuid);
|
$stmt->bindParam(':uuid', $uuid);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
|
|
||||||
$data = $stmt->fetch(\PDO::FETCH_ASSOC);
|
$data = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
if($data)
|
if($data)
|
||||||
{
|
{
|
||||||
return new EntityRecord($data);
|
return new EntityRecord($data);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (\PDOException $e)
|
catch (PDOException $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException("Failed to retrieve entity by UUID: " . $e->getMessage(), $e->getCode(), $e);
|
throw new DatabaseOperationException("Failed to retrieve entity by UUID: " . $e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
|
@ -117,14 +121,14 @@
|
||||||
* Deletes an entity by its UUID.
|
* Deletes an entity by its UUID.
|
||||||
*
|
*
|
||||||
* @param string $uuid The UUID of the entity to delete.
|
* @param string $uuid The UUID of the entity to delete.
|
||||||
* @throws \InvalidArgumentException If the UUID is not provided or is invalid.
|
* @throws InvalidArgumentException If the UUID is not provided or is invalid.
|
||||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||||
*/
|
*/
|
||||||
public static function deleteEntity(string $uuid): void
|
public static function deleteEntity(string $uuid): void
|
||||||
{
|
{
|
||||||
if(strlen($uuid) < 1)
|
if(strlen($uuid) < 1)
|
||||||
{
|
{
|
||||||
throw new \InvalidArgumentException("Entity UUID must be provided.");
|
throw new InvalidArgumentException("Entity UUID must be provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -133,7 +137,7 @@
|
||||||
$stmt->bindParam(':uuid', $uuid);
|
$stmt->bindParam(':uuid', $uuid);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
catch (\PDOException $e)
|
catch (PDOException $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException("Failed to delete entity: " . $e->getMessage(), $e->getCode(), $e);
|
throw new DatabaseOperationException("Failed to delete entity: " . $e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
|
@ -144,14 +148,14 @@
|
||||||
*
|
*
|
||||||
* @param string $id The ID of the entity to delete.
|
* @param string $id The ID of the entity to delete.
|
||||||
* @param string $domain The domain of the entity to delete.
|
* @param string $domain The domain of the entity to delete.
|
||||||
* @throws \InvalidArgumentException If the ID or domain is not provided or is invalid.
|
* @throws InvalidArgumentException If the ID or domain is not provided or is invalid.
|
||||||
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
|
||||||
*/
|
*/
|
||||||
public static function deleteEntityById(string $id, string $domain): void
|
public static function deleteEntityById(string $id, string $domain): void
|
||||||
{
|
{
|
||||||
if(strlen($id) < 1 || strlen($domain) < 1)
|
if(strlen($id) < 1 || strlen($domain) < 1)
|
||||||
{
|
{
|
||||||
throw new \InvalidArgumentException("Entity ID and domain must be provided.");
|
throw new InvalidArgumentException("Entity ID and domain must be provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -161,10 +165,9 @@
|
||||||
$stmt->bindParam(':domain', $domain);
|
$stmt->bindParam(':domain', $domain);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
catch (\PDOException $e)
|
catch (PDOException $e)
|
||||||
{
|
{
|
||||||
throw new DatabaseOperationException("Failed to delete entity by ID and domain: " . $e->getMessage(), $e->getCode(), $e);
|
throw new DatabaseOperationException("Failed to delete entity by ID and domain: " . $e->getMessage(), $e->getCode(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue