From 2b59713253ad6f5fb3853a5ac2086f4f8883f024 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 29 May 2025 21:39:07 -0400 Subject: [PATCH] Add EntitiesManager for managing entity records and operations --- .idea/sqldialects.xml | 1 + .../Classes/Managers/EntitiesManager.php | 170 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 src/FederationServer/Classes/Managers/EntitiesManager.php diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index bbca47e..aee3c04 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -2,6 +2,7 @@ + diff --git a/src/FederationServer/Classes/Managers/EntitiesManager.php b/src/FederationServer/Classes/Managers/EntitiesManager.php new file mode 100644 index 0000000..ef5f3ae --- /dev/null +++ b/src/FederationServer/Classes/Managers/EntitiesManager.php @@ -0,0 +1,170 @@ + 255) + { + throw new \InvalidArgumentException("Entity ID cannot exceed 255 characters."); + } + if(!filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) && $domain !== null) + { + throw new \InvalidArgumentException("Invalid domain format."); + } + if(strlen($domain) > 255) + { + throw new \InvalidArgumentException("Domain cannot exceed 255 characters."); + } + + try + { + $stmt = DatabaseConnection::getConnection()->prepare("INSERT INTO entities (id, domain) VALUES (:id, :domain)"); + $stmt->bindParam(':id', $id); + $stmt->bindParam(':domain', $domain); + $stmt->execute(); + } + catch (\PDOException $e) + { + throw new DatabaseOperationException("Failed to register entity: " . $e->getMessage(), $e->getCode(), $e); + } + } + /** + * Retrieves an entity by its ID and domain. + * + * @param string $id The ID of the entity. + * @param string $domain The domain of the entity. + * @return EntityRecord|null The EntityRecord object if found, null otherwise. + * @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. + */ + public static function getEntityByDomain(string $id, string $domain): ?EntityRecord + { + if(strlen($id) < 1 || strlen($domain) < 1) + { + throw new \InvalidArgumentException("Entity ID and domain must be provided."); + } + + try + { + $stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM entities WHERE id = :id AND domain = :domain"); + $stmt->bindParam(':id', $id); + $stmt->bindParam(':domain', $domain); + $stmt->execute(); + + $data = $stmt->fetch(\PDO::FETCH_ASSOC); + if($data) + { + return new EntityRecord($data); + } + return null; + } + catch (\PDOException $e) + { + throw new DatabaseOperationException("Failed to retrieve entity by domain: " . $e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Retrieves an entity by its UUID. + * + * @param string $uuid The UUID of the entity. + * @return EntityRecord|null The EntityRecord object if found, null otherwise. + * @throws \InvalidArgumentException If the UUID is not provided or is invalid. + * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. + */ + public static function getEntityByUuid(string $uuid): ?EntityRecord + { + if(strlen($uuid) < 1) + { + throw new \InvalidArgumentException("Entity UUID must be provided."); + } + + try + { + $stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM entities WHERE uuid = :uuid"); + $stmt->bindParam(':uuid', $uuid); + $stmt->execute(); + + $data = $stmt->fetch(\PDO::FETCH_ASSOC); + if($data) + { + return new EntityRecord($data); + } + return null; + } + catch (\PDOException $e) + { + throw new DatabaseOperationException("Failed to retrieve entity by UUID: " . $e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Deletes an entity by its UUID. + * + * @param string $uuid The UUID of the entity to delete. + * @throws \InvalidArgumentException If the UUID is not provided or is invalid. + * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. + */ + public static function deleteEntity(string $uuid): void + { + if(strlen($uuid) < 1) + { + throw new \InvalidArgumentException("Entity UUID must be provided."); + } + + try + { + $stmt = DatabaseConnection::getConnection()->prepare("DELETE FROM entities WHERE uuid = :uuid"); + $stmt->bindParam(':uuid', $uuid); + $stmt->execute(); + } + catch (\PDOException $e) + { + throw new DatabaseOperationException("Failed to delete entity: " . $e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Deletes an entity by its ID and domain. + * + * @param string $id The ID 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 DatabaseOperationException If there is an error preparing or executing the SQL statement. + */ + public static function deleteEntityById(string $id, string $domain): void + { + if(strlen($id) < 1 || strlen($domain) < 1) + { + throw new \InvalidArgumentException("Entity ID and domain must be provided."); + } + + try + { + $stmt = DatabaseConnection::getConnection()->prepare("DELETE FROM entities WHERE id = :id AND domain = :domain"); + $stmt->bindParam(':id', $id); + $stmt->bindParam(':domain', $domain); + $stmt->execute(); + } + catch (\PDOException $e) + { + throw new DatabaseOperationException("Failed to delete entity by ID and domain: " . $e->getMessage(), $e->getCode(), $e); + } + } + + } \ No newline at end of file