You are here

public function AcquiaContentHubSiteCommands::contenthubDisconnectSite in Acquia Content Hub 8.2

Disconnects a site with contenthub.

@option delete Flag to delete all the entities from Content Hub. @default delete

@command acquia:contenthub-disconnect-site @aliases ach-disconnect,acquia-contenthub-disconnect-site

File

src/Commands/AcquiaContentHubSiteCommands.php, line 152

Class

AcquiaContentHubSiteCommands
Drush commands for interacting with Acquia Content Hub client site.

Namespace

Drupal\acquia_contenthub\Commands

Code

public function contenthubDisconnectSite() {
  $client = $this->clientFactory
    ->getClient();
  if (!$client instanceof ContentHubClient) {
    $message = "Couldn't instantiate client. Please check connection settings.";
    $this->logger
      ->log(LogLevel::CANCEL, $message);
    return;
  }
  $provider = $this->clientFactory
    ->getProvider();
  if ($provider != 'core_config') {
    $message = dt('Settings are being provided by %provider and cannot be disconnected manually.', [
      '%provider' => $provider,
    ]);
    $this->logger
      ->log(LogLevel::CANCEL, $message);
    return;
  }
  $client = $this->clientFactory
    ->getClient();
  $settings = $client
    ->getSettings();
  $remote_settings = $client
    ->getRemoteSettings();
  foreach ($remote_settings['webhooks'] as $webhook) {

    // Checks that webhook from settings and url from options are matching.
    $uri_option = $this->input
      ->getOption('uri');
    if ($uri_option && $settings
      ->getWebhook() !== $uri_option) {
      continue;
    }
    if ($webhook['client_name'] === $settings
      ->getName()) {
      $webhook_uuid = $webhook['uuid'];
      break;
    }
  }
  if (empty($webhook_uuid)) {
    $this->logger
      ->log(LogLevel::ERROR, 'Cannot find webhook UUID.');
    return;
  }
  $event = new AcquiaContentHubUnregisterEvent($webhook_uuid);
  $this->eventDispatcher
    ->dispatch(AcquiaContentHubEvents::ACH_UNREGISTER, $event);
  try {
    $delete = $this->input
      ->getOption('delete');
    if ($delete === 'all') {
      $warning_message = dt('This command will delete ALL the entities published by this origin before unregistering the client. There is no way back from this action. It might take a while depending how many entities originated from this site. Are you sure you want to proceed (Y/n)?');
      if ($this
        ->io()
        ->confirm($warning_message) === FALSE) {
        $this->logger
          ->log(LogLevel::ERROR, 'Cancelled.');
        return;
      }
      foreach ($event
        ->getOrphanedEntities() as $entity) {
        if ($entity['type'] === 'client') {
          continue;
        }
        $client
          ->deleteEntity($entity['uuid']);
      }
    }
    if ($event
      ->getOrphanedEntitiesAmount() > 0 && $delete !== 'all') {
      $message = $this->output
        ->writeln(dt('There are @count entities published from this origin. You have to delete/reoriginate those entities before proceeding with the unregistration. If you want to delete those entities and unregister the client, use the following drush command "drush ach-disconnect --delete=all".', [
        '@count' => $event
          ->getOrphanedEntitiesAmount(),
      ]));
      $this->logger
        ->log(LogLevel::CANCEL, $message);
      return;
    }
    $this->achConnectionManager
      ->unregister($event);
  } catch (\Exception $exception) {
    $this->logger
      ->log(LogLevel::ERROR, $exception
      ->getMessage());
  }
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('acquia_contenthub.admin_settings');
  $client_name = $config
    ->get('client_name');
  $config
    ->delete();
  $message = dt('Successfully disconnected site %site from contenthub', [
    '%site' => $client_name,
  ]);
  $this->logger
    ->log(LogLevel::SUCCESS, $message);
}