View source
<?php
namespace Drupal\acquia_contenthub\Commands;
use Acquia\ContentHubClient\ContentHubClient;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub\ContentHubConnectionManager;
use Drupal\acquia_contenthub\Event\AcquiaContentHubUnregisterEvent;
use Drupal\acquia_contenthub\Form\ContentHubSettingsForm;
use Drupal\Core\Form\FormState;
use Drush\Commands\DrushCommands;
use Drush\Log\LogLevel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class AcquiaContentHubSiteCommands extends DrushCommands {
protected $clientFactory;
protected $eventDispatcher;
protected $achConnectionManager;
public function __construct(ClientFactory $client_factory, EventDispatcherInterface $eventDispatcher, ContentHubConnectionManager $achConnectionManager) {
$this->clientFactory = $client_factory;
$this->eventDispatcher = $eventDispatcher;
$this->achConnectionManager = $achConnectionManager;
}
public function contenthubConnectSite() {
$options = $this
->input()
->getOptions();
$settings = $this->clientFactory
->getSettings();
$config_origin = $settings
->getUuid();
$provider = $this->clientFactory
->getProvider();
if ($provider != 'core_config') {
$message = dt('Settings are being provided by @provider, and already connected.', [
'@provider' => $provider,
]);
$this
->logger()
->log(LogLevel::CANCEL, $message);
return;
}
if (!empty($config_origin)) {
$message = dt('Site is already connected to Content Hub. Skipping.');
$this
->logger()
->log(LogLevel::CANCEL, $message);
return;
}
$io = $this
->io();
$hostname = $options['hostname'] ?? $io
->ask(dt('What is the Content Hub API URL?'), 'https://us-east-1.content-hub.acquia.com');
$api_key = $options['api_key'] ?? $io
->ask(dt('What is your Content Hub API Key?'));
$secret_key = $options['secret_key'] ?? $io
->ask(dt('What is your Content Hub API Secret?'));
$client_uuid = \Drupal::service('uuid')
->generate();
$client_name = $options['client_name'] ?? $io
->ask(dt('What is the client name for this site?'), $client_uuid);
$form_state = (new FormState())
->setValues([
'hostname' => $hostname,
'api_key' => $api_key,
'secret_key' => $secret_key,
'client_name' => sprintf("%s_%s", $client_name, $client_uuid),
'op' => t('Save configuration'),
]);
$form = \Drupal::formBuilder()
->buildForm(ContentHubSettingsForm::class, new FormState());
$form_state
->setTriggeringElement($form['actions']['submit']);
\Drupal::formBuilder()
->submitForm(ContentHubSettingsForm::class, $form_state);
}
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) {
$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);
}
}