From 1739c8dd59e1287fc07a24a3438d3a4b9a62ed63 Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 4 Jun 2025 15:13:27 -0400 Subject: [PATCH] Refactor audit log retrieval to use toPublicAuditRecord method for improved readability and maintainability --- .../Classes/Managers/AuditLogManager.php | 26 +++++++++++++++++++ .../Methods/Audit/ListAuditLogs.php | 22 +++------------- .../Methods/Entities/ListEntityAuditLogs.php | 21 +++------------ .../Operators/ListOperatorAuditLogs.php | 21 +++------------ 4 files changed, 35 insertions(+), 55 deletions(-) diff --git a/src/FederationServer/Classes/Managers/AuditLogManager.php b/src/FederationServer/Classes/Managers/AuditLogManager.php index 013ddcd..fd9018e 100644 --- a/src/FederationServer/Classes/Managers/AuditLogManager.php +++ b/src/FederationServer/Classes/Managers/AuditLogManager.php @@ -8,6 +8,7 @@ use FederationServer\Classes\Logger; use FederationServer\Exceptions\DatabaseOperationException; use FederationServer\Objects\AuditLogRecord; + use FederationServer\Objects\PublicAuditRecord; use InvalidArgumentException; use PDO; use PDOException; @@ -313,6 +314,31 @@ } } + /** + * Converts an AuditLogRecord to a PublicAuditRecord. + * + * @param AuditLogRecord $record The AuditLogRecord to convert. + * @return PublicAuditRecord The converted PublicAuditRecord. + * @throws DatabaseOperationException Thrown if there was a database error while transforming the record. + */ + public static function toPublicAuditRecord(AuditLogRecord $record): PublicAuditRecord + { + $operatorRecord = null; + $entityRecord = null; + + if($record->getOperator() !== null) + { + $operatorRecord = OperatorManager::getOperator($record->getOperator()); + } + + if($record->getEntity() !== null) + { + $entityRecord = EntitiesManager::getEntityByUuid($record->getEntity()); + } + + return new PublicAuditRecord($record, $operatorRecord, $entityRecord); + } + /** * Deletes all audit log entries. * diff --git a/src/FederationServer/Methods/Audit/ListAuditLogs.php b/src/FederationServer/Methods/Audit/ListAuditLogs.php index 5a8e79b..9cc5429 100644 --- a/src/FederationServer/Methods/Audit/ListAuditLogs.php +++ b/src/FederationServer/Methods/Audit/ListAuditLogs.php @@ -51,27 +51,11 @@ $filteredEntries = null; } - try { - $auditLogs = AuditLogManager::getEntries($limit, $page, $filteredEntries); - foreach($auditLogs as $logRecord) - { - $operatorRecord = null; - $entityRecord = null; - - if($logRecord->getOperator() !== null) - { - $operatorRecord = OperatorManager::getOperator($logRecord->getOperator()); - } - - if($logRecord->getEntity() !== null) - { - $entityRecord = EntitiesManager::getEntityByUuid($logRecord->getEntity()); - } - - $results[] = new PublicAuditRecord($logRecord, $operatorRecord, $entityRecord); - } + $results = array_map(fn($log) => AuditLogManager::toPublicAuditRecord($log), + AuditLogManager::getEntries($limit, $page, $filteredEntries) + ); } catch (DatabaseOperationException $e) { diff --git a/src/FederationServer/Methods/Entities/ListEntityAuditLogs.php b/src/FederationServer/Methods/Entities/ListEntityAuditLogs.php index 00aa2b0..d358527 100644 --- a/src/FederationServer/Methods/Entities/ListEntityAuditLogs.php +++ b/src/FederationServer/Methods/Entities/ListEntityAuditLogs.php @@ -69,24 +69,9 @@ throw new RequestException('Not Found: Entity with the specified UUID does not exist', 404); } - $auditLogs = AuditLogManager::getEntriesByEntity($entityUuid, $limit, $page, $filteredEntries); - foreach($auditLogs as $logRecord) - { - $operatorRecord = null; - $entityRecord = null; - - if($logRecord->getOperator() !== null) - { - $operatorRecord = OperatorManager::getOperator($logRecord->getOperator()); - } - - if($logRecord->getEntity() !== null) - { - $entityRecord = EntitiesManager::getEntityByUuid($logRecord->getEntity()); - } - - $results[] = new PublicAuditRecord($logRecord, $operatorRecord, $entityRecord); - } + $results = array_map(fn($log) => AuditLogManager::toPublicAuditRecord($log), + AuditLogManager::getEntriesByEntity($entityUuid, $limit, $page, $filteredEntries) + ); } catch (DatabaseOperationException $e) { diff --git a/src/FederationServer/Methods/Operators/ListOperatorAuditLogs.php b/src/FederationServer/Methods/Operators/ListOperatorAuditLogs.php index 42c8e8f..e0103f5 100644 --- a/src/FederationServer/Methods/Operators/ListOperatorAuditLogs.php +++ b/src/FederationServer/Methods/Operators/ListOperatorAuditLogs.php @@ -69,24 +69,9 @@ throw new RequestException('Not Found: Operator with the specified UUID does not exist', 404); } - $auditLogs = AuditLogManager::getEntriesByOperator($operatorUuid, $limit, $page, $filteredEntries); - foreach($auditLogs as $logRecord) - { - $operatorRecord = null; - $entityRecord = null; - - if($logRecord->getOperator() !== null) - { - $operatorRecord = OperatorManager::getOperator($logRecord->getOperator()); - } - - if($logRecord->getEntity() !== null) - { - $entityRecord = EntitiesManager::getEntityByUuid($logRecord->getEntity()); - } - - $results[] = new PublicAuditRecord($logRecord, $operatorRecord, $entityRecord); - } + $results = array_map(fn($log) => AuditLogManager::toPublicAuditRecord($log), + AuditLogManager::getEntriesByOperator($operatorUuid, $limit, $page, $filteredEntries) + ); } catch (DatabaseOperationException $e) {