Verified Commit a6a64fba authored by dockx thibault's avatar dockx thibault
Browse files

:art: style(extractor) - improve code formatting by adding spaces in function...

:art: style(extractor) - improve code formatting by adding spaces in function signatures for better readability
parent 1e3d445c
1 merge request!83Resolve "[Orchestrator] - Task for CSV Extraction Based on Group and Member Criteria"
Pipeline #32660 passed with stages
in 23 seconds
Showing with 70 additions and 70 deletions
+70 -70
...@@ -4,7 +4,7 @@ class Extractor implements EndpointInterface ...@@ -4,7 +4,7 @@ class Extractor implements EndpointInterface
{ {
private TaskGateway $gateway; private TaskGateway $gateway;
public function __construct(TaskGateway $gateway) public function __construct (TaskGateway $gateway)
{ {
$this->gateway = $gateway; $this->gateway = $gateway;
} }
...@@ -13,7 +13,7 @@ class Extractor implements EndpointInterface ...@@ -13,7 +13,7 @@ class Extractor implements EndpointInterface
* @return array * @return array
* Part of the interface of orchestrator plugin to treat GET method * Part of the interface of orchestrator plugin to treat GET method
*/ */
public function processEndPointGet(): array public function processEndPointGet (): array
{ {
// Retrieve tasks of type 'extract' // Retrieve tasks of type 'extract'
return $this->gateway->getObjectTypeTask('extract'); return $this->gateway->getObjectTypeTask('extract');
...@@ -24,7 +24,7 @@ class Extractor implements EndpointInterface ...@@ -24,7 +24,7 @@ class Extractor implements EndpointInterface
* @return array * @return array
* Note: Part of the interface of orchestrator plugin to treat POST method * Note: Part of the interface of orchestrator plugin to treat POST method
*/ */
public function processEndPointPost(array $data = NULL): array public function processEndPointPost (array $data = NULL): array
{ {
return []; return [];
} }
...@@ -34,7 +34,7 @@ class Extractor implements EndpointInterface ...@@ -34,7 +34,7 @@ class Extractor implements EndpointInterface
* @return array * @return array
* Note: Part of the interface of orchestrator plugin to treat DELETE method * Note: Part of the interface of orchestrator plugin to treat DELETE method
*/ */
public function processEndPointDelete(array $data = NULL): array public function processEndPointDelete (array $data = NULL): array
{ {
return []; return [];
} }
...@@ -45,11 +45,11 @@ class Extractor implements EndpointInterface ...@@ -45,11 +45,11 @@ class Extractor implements EndpointInterface
* @throws Exception * @throws Exception
* Note: Part of the interface of orchestrator plugin to treat PATCH method * Note: Part of the interface of orchestrator plugin to treat PATCH method
*/ */
public function processEndPointPatch(array $data = NULL): array public function processEndPointPatch (array $data = NULL): array
{ {
$result = []; $result = [];
$extractTasks = $this->gateway->getObjectTypeTask('extract'); $extractTasks = $this->gateway->getObjectTypeTask('extract');
// Path is now expected in the JSON body ($data) // Path is now expected in the JSON body ($data)
$path = $data['path'] ?? '/srv/orchestrator/'; $path = $data['path'] ?? '/srv/orchestrator/';
...@@ -62,39 +62,39 @@ class Extractor implements EndpointInterface ...@@ -62,39 +62,39 @@ class Extractor implements EndpointInterface
// Get the main task configuration // Get the main task configuration
$mainTaskConfig = $this->getExtractMainTaskConfig($task['fdtasksgranularmaster'][0]); $mainTaskConfig = $this->getExtractMainTaskConfig($task['fdtasksgranularmaster'][0]);
// Get user DN from the task // Get user DN from the task
$userDn = $task['fdtasksgranulardn'][0]; $userDn = $task['fdtasksgranulardn'][0];
// Get user attributes // Get user attributes
$userAttributes = $this->getUserAttributes($userDn, $mainTaskConfig); $userAttributes = $this->getUserAttributes($userDn, $mainTaskConfig);
// Format comes from the main task configuration // Format comes from the main task configuration
$format = isset($mainTaskConfig[0]['fdextractortaskformat']) ? $format = isset($mainTaskConfig[0]['fdextractortaskformat']) ?
strtolower($mainTaskConfig[0]['fdextractortaskformat'][0]) : 'csv'; strtolower($mainTaskConfig[0]['fdextractortaskformat'][0]) : 'csv';
// Create directory if it doesn't exist // Create directory if it doesn't exist
$this->ensureDirectoryExists($path); $this->ensureDirectoryExists($path);
// Get main task CN for filename // Get main task CN for filename
$mainTaskCn = $this->getMainTaskCn($task['fdtasksgranularmaster'][0]); $mainTaskCn = $this->getMainTaskCn($task['fdtasksgranularmaster'][0]);
// Determine filename with main task name and date with hour (no minutes or seconds) // Determine filename with main task name and date with hour (no minutes or seconds)
$date = date('Y-m-d_H'); // Using only year-month-day_hour format $date = date('Y-m-d_H'); // Using only year-month-day_hour format
$filename = isset($data['filename']) ? $filename = isset($data['filename']) ?
$path . $data['filename'] . '_' . $date . '.' . $format : $path . $data['filename'] . '_' . $date . '.' . $format :
$path . $mainTaskCn . '_' . $date . '.' . $format; $path . $mainTaskCn . '_' . $date . '.' . $format;
// Extract and write to file // Extract and write to file
$success = $this->extractToFile($userAttributes, $filename, $format); $success = $this->extractToFile($userAttributes, $filename, $format);
if ($success) { if ($success) {
$result[$task['dn']]['result'] = "User attributes successfully extracted to $filename"; $result[$task['dn']]['result'] = "User attributes successfully extracted to $filename";
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], '2'); $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], '2');
} else { } else {
throw new Exception("Failed to write data to $filename"); throw new Exception("Failed to write data to $filename");
} }
} catch (Exception $e) { } catch (Exception $e) {
$result[$task['dn']]['result'] = "Error extracting user attributes: " . $e->getMessage(); $result[$task['dn']]['result'] = "Error extracting user attributes: " . $e->getMessage();
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $e->getMessage()); $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $e->getMessage());
...@@ -109,7 +109,7 @@ class Extractor implements EndpointInterface ...@@ -109,7 +109,7 @@ class Extractor implements EndpointInterface
* @return array * @return array
* Note: Retrieve the configuration from the main extract task. * Note: Retrieve the configuration from the main extract task.
*/ */
private function getExtractMainTaskConfig(string $mainTaskDn): array private function getExtractMainTaskConfig (string $mainTaskDn): array
{ {
return $this->gateway->getLdapTasks( return $this->gateway->getLdapTasks(
'(objectClass=fdExtractorTasks)', '(objectClass=fdExtractorTasks)',
...@@ -125,7 +125,7 @@ class Extractor implements EndpointInterface ...@@ -125,7 +125,7 @@ class Extractor implements EndpointInterface
* @return array * @return array
* Note: Get all user attributes from the user DN. * Note: Get all user attributes from the user DN.
*/ */
private function getUserAttributes(string $userDn, array $mainTaskConfig): array private function getUserAttributes (string $userDn, array $mainTaskConfig): array
{ {
// Get all user data from LDAP // Get all user data from LDAP
$userData = $this->gateway->getLdapTasks( $userData = $this->gateway->getLdapTasks(
...@@ -134,7 +134,7 @@ class Extractor implements EndpointInterface ...@@ -134,7 +134,7 @@ class Extractor implements EndpointInterface
'', '',
$userDn $userDn
); );
// Process and return user data // Process and return user data
$this->gateway->unsetCountKeys($userData); $this->gateway->unsetCountKeys($userData);
return $userData; return $userData;
...@@ -146,14 +146,14 @@ class Extractor implements EndpointInterface ...@@ -146,14 +146,14 @@ class Extractor implements EndpointInterface
* @throws Exception * @throws Exception
* Note: Create directory if it doesn't exist. * Note: Create directory if it doesn't exist.
*/ */
private function ensureDirectoryExists(string $path): bool private function ensureDirectoryExists (string $path): bool
{ {
if (!is_dir($path)) { if (!is_dir($path)) {
if (!mkdir($path, 0755, true)) { if (!mkdir($path, 0755, TRUE)) {
throw new Exception("Failed to create directory: $path"); throw new Exception("Failed to create directory: $path");
} }
} }
return true; return TRUE;
} }
/** /**
...@@ -164,7 +164,7 @@ class Extractor implements EndpointInterface ...@@ -164,7 +164,7 @@ class Extractor implements EndpointInterface
* @throws Exception * @throws Exception
* Note: Extract user attributes to a file. * Note: Extract user attributes to a file.
*/ */
private function extractToFile(array $userAttributes, string $filename, string $format): bool private function extractToFile (array $userAttributes, string $filename, string $format): bool
{ {
switch (strtolower($format)) { switch (strtolower($format)) {
case 'csv': case 'csv':
...@@ -185,58 +185,58 @@ class Extractor implements EndpointInterface ...@@ -185,58 +185,58 @@ class Extractor implements EndpointInterface
* @throws Exception * @throws Exception
* Note: Export user attributes to CSV, preventing duplicate UIDs and handling new attributes. * Note: Export user attributes to CSV, preventing duplicate UIDs and handling new attributes.
*/ */
private function exportToCsv(array $userAttributes, string $filename): bool private function exportToCsv (array $userAttributes, string $filename): bool
{ {
if (empty($userAttributes)) { if (empty($userAttributes)) {
return true; // No attributes to write return TRUE; // No attributes to write
} }
$user = $userAttributes[0]; $user = $userAttributes[0];
$userData = []; $userData = [];
$allColumns = []; $allColumns = [];
$existingData = []; $existingData = [];
$uidKey = 'uid'; // The attribute to check for duplicates $uidKey = 'uid'; // The attribute to check for duplicates
$newUserUid = ''; $newUserUid = '';
// Extract UID and prepare user data // Extract UID and prepare user data
foreach ($user as $attribute => $values) { foreach ($user as $attribute => $values) {
if (is_array($values)) { if (is_array($values)) {
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (is_numeric($key)) { if (is_numeric($key)) {
$userData[$attribute] = $value; $userData[$attribute] = $value;
$allColumns[$attribute] = true; // Use as associative array to avoid duplicates $allColumns[$attribute] = TRUE; // Use as associative array to avoid duplicates
if (strtolower($attribute) === $uidKey) { if (strtolower($attribute) === $uidKey) {
$newUserUid = $value; $newUserUid = $value;
} }
break; // Only take the first value for simplicity break; // Only take the first value for simplicity
} }
} }
} }
} }
// If no UID found, generate a random one // If no UID found, generate a random one
if (empty($newUserUid)) { if (empty($newUserUid)) {
$newUserUid = 'user_' . uniqid(); $newUserUid = 'user_' . uniqid();
$userData[$uidKey] = $newUserUid; $userData[$uidKey] = $newUserUid;
$allColumns[$uidKey] = true; $allColumns[$uidKey] = TRUE;
} }
// Read existing file if it exists // Read existing file if it exists
if (file_exists($filename)) { if (file_exists($filename)) {
$handle = fopen($filename, 'r'); $handle = fopen($filename, 'r');
if ($handle !== false) { if ($handle !== FALSE) {
// Read headers // Read headers
$headers = fgetcsv($handle); $headers = fgetcsv($handle);
if ($headers !== false) { if ($headers !== FALSE) {
// Add existing headers to all columns // Add existing headers to all columns
foreach ($headers as $header) { foreach ($headers as $header) {
$allColumns[$header] = true; $allColumns[$header] = TRUE;
} }
// Read existing data // Read existing data
while (($row = fgetcsv($handle)) !== false) { while (($row = fgetcsv($handle)) !== FALSE) {
$rowData = []; $rowData = [];
foreach ($headers as $index => $header) { foreach ($headers as $index => $header) {
$rowData[$header] = $row[$index] ?? ''; $rowData[$header] = $row[$index] ?? '';
...@@ -250,23 +250,23 @@ class Extractor implements EndpointInterface ...@@ -250,23 +250,23 @@ class Extractor implements EndpointInterface
fclose($handle); fclose($handle);
} }
} }
// Convert all columns associative array to indexed array // Convert all columns associative array to indexed array
$finalColumns = array_keys($allColumns); $finalColumns = array_keys($allColumns);
// Add new user data to existing data // Add new user data to existing data
$existingData[] = $userData; $existingData[] = $userData;
// Write to file // Write to file
$handle = fopen($filename, 'w'); // 'w' to overwrite with complete data $handle = fopen($filename, 'w'); // 'w' to overwrite with complete data
if ($handle === false) { if ($handle === FALSE) {
throw new Exception("Could not open file: $filename"); throw new Exception("Could not open file: $filename");
} }
try { try {
// Write headers // Write headers
fputcsv($handle, $finalColumns); fputcsv($handle, $finalColumns);
// Write data rows // Write data rows
foreach ($existingData as $row) { foreach ($existingData as $row) {
$outputRow = []; $outputRow = [];
...@@ -275,8 +275,8 @@ class Extractor implements EndpointInterface ...@@ -275,8 +275,8 @@ class Extractor implements EndpointInterface
} }
fputcsv($handle, $outputRow); fputcsv($handle, $outputRow);
} }
return true; return TRUE;
} finally { } finally {
fclose($handle); fclose($handle);
} }
...@@ -288,23 +288,23 @@ class Extractor implements EndpointInterface ...@@ -288,23 +288,23 @@ class Extractor implements EndpointInterface
* @return bool * @return bool
* Note: Export user attributes to JSON with duplicate UID handling. * Note: Export user attributes to JSON with duplicate UID handling.
*/ */
private function exportToJson(array $userAttributes, string $filename): bool private function exportToJson (array $userAttributes, string $filename): bool
{ {
$existingData = []; $existingData = [];
$uidKey = 'uid'; $uidKey = 'uid';
$newUserUid = ''; $newUserUid = '';
// Get UID of new user // Get UID of new user
if (!empty($userAttributes[0][$uidKey][0])) { if (!empty($userAttributes[0][$uidKey][0])) {
$newUserUid = $userAttributes[0][$uidKey][0]; $newUserUid = $userAttributes[0][$uidKey][0];
} }
// Read existing data if file exists // Read existing data if file exists
if (file_exists($filename)) { if (file_exists($filename)) {
$existingJson = file_get_contents($filename); $existingJson = file_get_contents($filename);
if (!empty($existingJson)) { if (!empty($existingJson)) {
$jsonData = json_decode($existingJson, true) ?? []; $jsonData = json_decode($existingJson, TRUE) ?? [];
// Filter out any entries with the same UID // Filter out any entries with the same UID
foreach ($jsonData as $entry) { foreach ($jsonData as $entry) {
if (!isset($entry[$uidKey][0]) || $entry[$uidKey][0] !== $newUserUid) { if (!isset($entry[$uidKey][0]) || $entry[$uidKey][0] !== $newUserUid) {
...@@ -313,12 +313,12 @@ class Extractor implements EndpointInterface ...@@ -313,12 +313,12 @@ class Extractor implements EndpointInterface
} }
} }
} }
// Add new data // Add new data
$existingData[] = $userAttributes[0]; $existingData[] = $userAttributes[0];
// Write back to file // Write back to file
return file_put_contents($filename, json_encode($existingData, JSON_PRETTY_PRINT)) !== false; return file_put_contents($filename, json_encode($existingData, JSON_PRETTY_PRINT)) !== FALSE;
} }
/** /**
...@@ -327,23 +327,23 @@ class Extractor implements EndpointInterface ...@@ -327,23 +327,23 @@ class Extractor implements EndpointInterface
* @return bool * @return bool
* Note: Export user attributes to XML with duplicate UID handling. * Note: Export user attributes to XML with duplicate UID handling.
*/ */
private function exportToXml(array $userAttributes, string $filename): bool private function exportToXml (array $userAttributes, string $filename): bool
{ {
$dom = new DOMDocument('1.0', 'UTF-8'); $dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true; $dom->formatOutput = TRUE;
$uidKey = 'uid'; $uidKey = 'uid';
$newUserUid = ''; $newUserUid = '';
// Get UID of new user // Get UID of new user
if (!empty($userAttributes[0][$uidKey][0])) { if (!empty($userAttributes[0][$uidKey][0])) {
$newUserUid = $userAttributes[0][$uidKey][0]; $newUserUid = $userAttributes[0][$uidKey][0];
} }
// Create or load XML document // Create or load XML document
if (file_exists($filename)) { if (file_exists($filename)) {
$dom->load($filename); $dom->load($filename);
$root = $dom->documentElement; $root = $dom->documentElement;
// Remove any existing user with the same UID // Remove any existing user with the same UID
if (!empty($newUserUid)) { if (!empty($newUserUid)) {
$xpath = new DOMXPath($dom); $xpath = new DOMXPath($dom);
...@@ -358,10 +358,10 @@ class Extractor implements EndpointInterface ...@@ -358,10 +358,10 @@ class Extractor implements EndpointInterface
$root = $dom->createElement('users'); $root = $dom->createElement('users');
$dom->appendChild($root); $dom->appendChild($root);
} }
// Add new user element // Add new user element
$user = $dom->createElement('user'); $user = $dom->createElement('user');
foreach ($userAttributes[0] as $attribute => $values) { foreach ($userAttributes[0] as $attribute => $values) {
if (is_array($values)) { if (is_array($values)) {
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
...@@ -372,20 +372,20 @@ class Extractor implements EndpointInterface ...@@ -372,20 +372,20 @@ class Extractor implements EndpointInterface
} }
} }
} }
$root->appendChild($user); $root->appendChild($user);
// Write to file // Write to file
return $dom->save($filename) !== false; return $dom->save($filename) !== FALSE;
} }
/** /**
* Get the CN (Common Name) of the main task * Get the CN (Common Name) of the main task
* *
* @param string $mainTaskDn * @param string $mainTaskDn
* @return string * @return string
*/ */
private function getMainTaskCn(string $mainTaskDn): string private function getMainTaskCn (string $mainTaskDn): string
{ {
$mainTask = $this->gateway->getLdapTasks( $mainTask = $this->gateway->getLdapTasks(
'(objectClass=*)', '(objectClass=*)',
...@@ -393,7 +393,7 @@ class Extractor implements EndpointInterface ...@@ -393,7 +393,7 @@ class Extractor implements EndpointInterface
'', '',
$mainTaskDn $mainTaskDn
); );
return $mainTask[0]['cn'][0] ?? 'extract'; return $mainTask[0]['cn'][0] ?? 'extract';
} }
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment