1. <?php
  2. /**
  3. * Merges datasets of queues for staging environments with error handling.
  4. *
  5. * @param array $stagingQueues An array of staging queue datasets. Each element should be an associative array with a 'data' key containing the queue data.
  6. * @param string $outputFile The path to the output file where the merged data will be written.
  7. * @return bool True on success, false on failure.
  8. */
  9. function mergeStagingQueues(array $stagingQueues, string $outputFile): bool
  10. {
  11. if (empty($stagingQueues)) {
  12. error_log("Error: No staging queues provided.");
  13. return false;
  14. }
  15. $mergedData = [];
  16. foreach ($stagingQueues as $queue) {
  17. if (!isset($queue['data']) || !is_array($queue['data'])) {
  18. error_log("Error: Invalid queue data format. Skipping.");
  19. continue; // Skip to the next queue
  20. }
  21. $mergedData = array_merge($mergedData, $queue['data']);
  22. }
  23. if (empty($mergedData)) {
  24. error_log("Error: No data found in staging queues.");
  25. return false;
  26. }
  27. try {
  28. file_put_contents($outputFile, json_encode($mergedData));
  29. return true;
  30. } catch (Exception $e) {
  31. error_log("Error writing to file: " . $e->getMessage());
  32. return false;
  33. }
  34. }
  35. // Example Usage (replace with your actual data and file path)
  36. /*
  37. $stagingQueues = [
  38. [
  39. 'data' => [
  40. ['id' => 1, 'name' => 'Queue A'],
  41. ['id' => 2, 'name' => 'Queue B'],
  42. ],
  43. ],
  44. [
  45. 'data' => [
  46. ['id' => 3, 'name' => 'Queue C'],
  47. ['id' => 4, 'name' => 'Queue D'],
  48. ],
  49. ],
  50. ];
  51. $outputFile = 'merged_queues.json';
  52. if (mergeStagingQueues($stagingQueues, $outputFile)) {
  53. echo "Staging queues merged successfully to " . $outputFile . "\n";
  54. } else {
  55. echo "Staging queues merge failed.\n";
  56. }
  57. */
  58. ?>

Add your comment