View source
<?php
namespace Drupal\acquia_contenthub\Commands;
use Acquia\ContentHubClient\ContentHubClient;
use Acquia\ContentHubClient\Settings;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\Core\Extension\ModuleExtensionList;
use Drush\Commands\DrushCommands;
class AcquiaContentHubPurgeCommands extends DrushCommands {
protected $clientFactory;
protected $moduleList;
public function __construct(ClientFactory $client_factory, ModuleExtensionList $module_list) {
$this->clientFactory = $client_factory;
$this->moduleList = $module_list;
}
public function contenthubPurge($api = NULL, $secret = NULL) {
$client = $this->clientFactory
->getClient();
if (!empty($api) && !empty($secret)) {
$client = $this
->resetConnection($client, $api, $secret);
}
if (!$client) {
throw new \Exception(dt('Error trying to connect to the Content Hub. Make sure this site is registered to Content hub.'));
}
$settings = $client
->getRemoteSettings();
$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 (!$this
->io()
->confirm($warning_message)) {
return;
}
$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 (!$this
->io()
->confirm($double_check_message)) {
return;
}
$response = $client
->purge();
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);
}
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);
}
public function contenthubRestore($api, $secret) {
$warning_message = "Are you sure you want to RESTORE the latest backup taken after purging your Content Hub Subscription?\n" . "*************************************************************************************\n" . "PROCEED WITH CAUTION. THIS ACTION WILL ELIMINATE ALL EXISTING ENTITIES IN YOUR CONTENT HUB SUBSCRIPTION.\n" . "This restore command should only be used after an accidental purge event has taken place *and* completed. This will attempt to restore\n" . "from the last purge-generated backup. In the event this fails, you will need to republish your content to Content Hub.\n For more information, check https://docs.acquia.com/content-hub.\n" . "*************************************************************************************\n" . "Are you sure you want to proceed?\n";
if ($this
->io()
->confirm($warning_message)) {
if (!empty($api) && !empty($secret)) {
$client = $this
->resetConnection($this->clientFactory
->getClient(), $api, $secret);
}
else {
$client = $this->clientFactory
->getClient();
}
if (!$client) {
throw new \Exception(dt('Error trying to connect to the Content Hub. Make sure this site is registered to Content hub.'));
}
$response = $client
->restore();
if (isset($response['success']) && $response['success'] === TRUE) {
$this
->output()
->writeln("Your Subscription is being restored. All clients who have registered to received webhooks will be notified with a reindex webhook when the restore process has been completed.\n");
}
else {
throw new \Exception(dt("Error trying to restore your subscription from a backup copy. You might require elevated keys to perform this operation."));
}
}
}
protected function resetConnection(ContentHubClient $client, $api_key, $secret_key) {
$settings = $client
->getSettings();
$new_settings = new Settings($settings
->getName(), $settings
->getUuid(), $api_key, $secret_key, $settings
->getUrl());
$module_info = $this->moduleList
->getExtensionInfo('acquia_contenthub');
$module_version = isset($module_info['version']) ? $module_info['version'] : '0.0.0';
$drupal_version = isset($module_info['core']) ? $module_info['core'] : '0.0.0';
$client_user_agent = 'AcquiaContentHub/' . $drupal_version . '-' . $module_version;
$config = [
'base_url' => $settings
->getUrl(),
'client-user-agent' => $client_user_agent,
];
$dispatcher = \Drupal::service('event_dispatcher');
return new ContentHubClient($config, $this
->logger(), $new_settings, $new_settings
->getMiddleware(), $dispatcher);
}
}