You are here

public function AcquiaContentHubPurgeCommands::contenthubPurge in Acquia Content Hub 8.2

Purges all entities from Acquia Content Hub.

WARNING! Be VERY careful when using this command. This destructive command requires elevated keys. Every subsequent execution of this command will override the backup created by the previous call.

@command acquia:contenthub-purge

@aliases ach-purge,acquia-contenthub-purge

Parameters

string $api: The API key.

string $secret: The secret key.

Throws

\Exception

File

src/Commands/AcquiaContentHubPurgeCommands.php, line 64

Class

AcquiaContentHubPurgeCommands
Drush commands for purging Acquia Content Hub.

Namespace

Drupal\acquia_contenthub\Commands

Code

public function contenthubPurge($api = NULL, $secret = NULL) {
  $client = $this->clientFactory
    ->getClient();

  // Use the keys associated with Drupal config explicitly entered.
  if (!empty($api) && !empty($secret)) {
    $client = $this
      ->resetConnection($client, $api, $secret);
  }

  // Without a client, we cannot purge.
  if (!$client) {
    throw new \Exception(dt('Error trying to connect to the Content Hub. Make sure this site is registered to Content hub.'));
  }

  // Get the remote settings for the UUID (name) of the client.
  $settings = $client
    ->getRemoteSettings();

  // Warning prompt initially.
  $warning_message = "Are you sure you want to PURGE your Content Hub Subscription?\n" . "*************************************************************************************\n" . "PROCEED WITH CAUTION. THIS ACTION WILL PURGE ALL EXISTING ENTITIES IN YOUR CONTENT HUB SUBSCRIPTION.\n" . "While a backup is created for use by the restore command, restoration may not be timely and is not guaranteed. Concurrent or frequent\n" . "use of this command may result in an inability to restore. You can always republish your content as a means of 'recovery'.\n    For more information, check https://docs.acquia.com/content-hub.\n" . "*************************************************************************************\n" . "Are you sure you want to proceed?\n";

  // If user aborts, stop the purge.
  if (!$this
    ->io()
    ->confirm($warning_message)) {
    return;
  }

  // Make sure this is the correct account before purging.
  $double_check_message = dt("Are you ABSOLUTELY sure? Purging the subscription !sub will remove all entities from Content Hub. Backups are created but not guaranteed. Please confirm one last time that you would like to continue.", [
    '!sub' => $settings['uuid'],
  ]);

  // If user aborts, stop the purge.
  if (!$this
    ->io()
    ->confirm($double_check_message)) {
    return;
  }

  // Execute the 'purge' command.
  $response = $client
    ->purge();

  // Success but not really.
  if (!isset($response['success']) || $response['success'] !== TRUE) {
    $message = dt("Error trying to purge your subscription. You might require elevated keys to perform this operation.");
    throw new \Exception($message);
  }

  // Error occurred.
  if (!empty($response['error']['code']) && !empty($response['error']['message'])) {
    $message = dt('Error trying to purge your subscription. Status code !code. !message', [
      '!code' => $response['error']['code'],
      '!message' => $response['error']['message'],
    ]);
    throw new \Exception($message);
  }
  $confirmation_message = dt("Your !sub subscription is being purged. All clients who have registered to received webhooks will be notified with purge and reindex webhooks when the purge process has been completed.\n", [
    '!sub' => $settings['uuid'] ?? '',
  ]);
  $this
    ->output()
    ->writeln($confirmation_message);
}