View source
<?php
namespace Drupal\acquia_contenthub\Form;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\AcquiaContentHubUnregisterHelperTrait;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub\ContentHubConnectionManager;
use Drupal\acquia_contenthub\Event\AcquiaContentHubUnregisterEvent;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ContentHubDeleteClientConfirmForm extends FormBase {
use AcquiaContentHubUnregisterHelperTrait;
protected $chConnectionManager;
protected $eventDispatcher;
protected $event;
protected $clientFactory;
public function __construct(ContentHubConnectionManager $ch_connection_manager, ClientFactory $client_factory, EventDispatcherInterface $eventDispatcher) {
$this->chConnectionManager = $ch_connection_manager;
$this->clientFactory = $client_factory;
$this->eventDispatcher = $eventDispatcher;
}
public static function create(ContainerInterface $container) {
$ch_connection_manager = $container
->get('acquia_contenthub.connection_manager');
$client_factory = $container
->get('acquia_contenthub.client.factory');
$event_dispatcher = $container
->get('event_dispatcher');
return new static($ch_connection_manager, $client_factory, $event_dispatcher);
}
public function buildForm(array $form, FormStateInterface $form_state) : array {
$webhook_uuid = $this
->getWebhookUuid();
if (empty($webhook_uuid)) {
$this
->messenger()
->addError($this
->t('Cannot find webhook uuid.'));
return $form;
}
$this
->dispatchEvent($webhook_uuid);
$orphaned_entities_amount = $this->event
->getOrphanedEntitiesAmount();
if ($orphaned_entities_amount !== 0) {
$form['delete_entities']['orphaned_entites'] = [
'#type' => 'details',
'#open' => TRUE,
'#description' => $this
->t('There are @count entities published from this client: @client. You have to delete/reoriginate those entities before proceeding with the unregistration. @blank
If you want to delete those entities and unregister the client, use the following drush command on the given client "drush ach-disconnect --delete=all".', [
'@count' => $orphaned_entities_amount,
'@client' => $this->event
->getClientName(),
'@blank' => new FormattableMarkup('<br>', []),
]),
'#title' => $this
->t('Un-register Acquia Content Hub'),
];
}
if ($this->event
->getOrphanedFilters()) {
$form['delete_filters'] = [
'#type' => 'details',
'#title' => $this
->t('After un-registration the following filters will be deleted:'),
'#open' => TRUE,
];
$form['delete_filters']['orphaned_filters'] = [
'#type' => 'table',
'#title' => $this
->t('Orphaned filters'),
'#header' => [
'Filter name',
'Filter UUID',
],
'#rows' => $this
->formatOrphanedFiltersTable($this->event
->getOrphanedFilters()),
];
if ($this
->checkDiscoveryRoute()) {
$form['actions']['redirect'] = [
'#type' => 'link',
'#title' => $this
->t('Go to Discovery Interface'),
'#url' => Url::fromRoute('acquia_contenthub_curation.discovery'),
'#attributes' => [
'class' => [
'button',
],
],
];
}
}
if (empty($this->event
->getOrphanedFilters()) && !$orphaned_entities_amount) {
$form['safe_message'] = [
'#markup' => $this
->t('Everything is in order, safe to proceed.'),
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Unregister'),
'#button_type' => 'primary',
'#attributes' => [
'disabled' => (bool) $orphaned_entities_amount,
],
];
$form['settings'] = [
'#type' => 'submit',
'#value' => $this
->t('Content Hub Settings'),
'#button_type' => 'primary',
'#name' => 'settings',
];
$form['subscription'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#button_type' => 'primary',
'#name' => 'subscription',
];
return $form;
}
public function dispatchEvent(string $webhook_uuid) {
$this->event = new AcquiaContentHubUnregisterEvent($webhook_uuid);
$this->eventDispatcher
->dispatch(AcquiaContentHubEvents::ACH_UNREGISTER, $this->event);
}
public function getWebhookUuid() : string {
$client = $this->clientFactory
->getClient();
$settings = $client
->getSettings();
$remote_settings = $client
->getRemoteSettings();
foreach ($remote_settings['webhooks'] as $webhook) {
if ($webhook['client_name'] === $settings
->getName()) {
return $webhook['uuid'];
}
}
return '';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement()['#name'] === 'subscription') {
$form_state
->setRedirect('acquia_contenthub.subscription_settings');
return;
}
if ($form_state
->getTriggeringElement()['#name'] === 'settings') {
$form_state
->setRedirect('acquia_contenthub.admin_settings');
return;
}
$client = $this->clientFactory
->getClient();
if (!$client) {
$this
->messenger()
->addError("Couldn't instantiate client. Please check connection settings.");
$form_state
->setRedirect('acquia_contenthub.admin_settings');
return;
}
try {
$this->chConnectionManager
->unregister($this->event);
} catch (\Exception $e) {
$this
->messenger()
->addError($this
->t('Error during unregistration: @error_message', [
'@error_message' => $e
->getMessage(),
]));
return;
}
$form_state
->setRedirect('acquia_contenthub.admin_settings');
}
public function getFormId() {
return "contenthub_delete_client_confirmation";
}
}