You are here

public function SubscriberAudit::executeManifest in Acquia Content Hub 8

Executes a Manifest file.

Parameters

string $file_path: The path to Manifest file.

string $output_file_path: Resulting Manifest file after execution.

Throws

\Exception

File

acquia_contenthub_audit/src/SubscriberAudit.php, line 267

Class

SubscriberAudit
Audits Subscribers.

Namespace

Drupal\acquia_contenthub_audit

Code

public function executeManifest(string $file_path, string $output_file_path) {
  $manifest = (include $file_path);
  $origin = $this->configFactory
    ->get('acquia_contenthub.admin_settings')
    ->get('origin');
  if (empty($manifest[$origin])) {
    print sprintf("There are no entities found in the manifest for this site's origin: %s\n", $origin);
    return;
  }

  // Missing entities.
  if (!empty($manifest[$origin]['missing'])) {
    print sprintf("There are entities found in the site that do not exist in Content Hub. Review those entities and delete them from the Manifest file before proceeding. Below a list of those entities:\n");
    foreach ($manifest[$origin]['missing'] as $entity_origin => $entities_list) {
      foreach ($entities_list as $type => $entities) {
        foreach ($entities as $uuid => $entity) {
          print sprintf("- [%s, %s, %s] origin: %s\n", $entity['type'], $entity['id'], $uuid, $entity_origin);
        }
      }
    }
    return;
  }

  // Deleting entities.
  $deleted_entities = 0;
  if (is_array($manifest[$origin]['delete'])) {
    foreach ($manifest[$origin]['delete'] as $type => $entities) {
      foreach ($entities as $uuid => $entity) {
        $this
          ->deleteEntity($uuid, $type);
        $deleted_entities++;
        unset($manifest[$origin]['delete'][$type][$uuid]);
      }
      if (empty($manifest[$origin]['delete'][$type])) {
        unset($manifest[$origin]['delete'][$type]);
      }
    }
    if (empty($manifest[$origin]['delete'])) {
      unset($manifest[$origin]['delete']);
    }
  }

  // Deleting tracking records.
  $deleted_tracking_record = 0;
  if (is_array($manifest[$origin]['delete_tracking'])) {
    foreach ($manifest[$origin]['delete_tracking'] as $type => $entities) {
      foreach ($entities as $uuid => $entity) {
        $this
          ->deleteTrackingEntity($uuid, $type);
        $deleted_tracking_record++;
        unset($manifest[$origin]['delete_tracking'][$type][$uuid]);
      }
      if (empty($manifest[$origin]['delete_tracking'][$type])) {
        unset($manifest[$origin]['delete_tracking'][$type]);
      }
    }
    if (empty($manifest[$origin]['delete_tracking'])) {
      unset($manifest[$origin]['delete_tracking']);
    }
  }

  // Importing entities.
  $imported_entities = 0;
  foreach ($manifest[$origin]['import'] as $entity_origin => $entities) {
    foreach ($entities as $uuid => $entity) {
      if ($this
        ->importEntity($uuid, $entity['filter'])) {
        $imported_entities++;
        unset($manifest[$origin]['import'][$entity_origin][$uuid]);
      }
    }
    if (empty($manifest[$origin]['import'][$entity_origin])) {
      unset($manifest[$origin]['import'][$entity_origin]);
    }
  }
  if (empty($manifest[$origin]['import'])) {
    unset($manifest[$origin]['import']);
  }
  if (empty($manifest[$origin])) {
    unset($manifest[$origin]);
  }
  if (file_put_contents($output_file_path, "<?php\n\nreturn " . var_export($manifest, TRUE) . ";")) {
    print sprintf("The resulting manifest file after execution was successfully written to %s.\n", $output_file_path);
    print sprintf("Deleted Entities: %s\n", $deleted_entities);
    print sprintf("Deleted Wrongly Tracking Records: %s\n", $deleted_tracking_record);
    print sprintf("Imported Entities: %s\n", $imported_entities);
  }
}