You are here

class WebhookDeleteConfirmForm in Acquia Content Hub 8.2

Confirmation form for webhook deletion.

@package Drupal\acquia_contenthub_publisher\Form\Webhook

Hierarchy

Expanded class hierarchy of WebhookDeleteConfirmForm

1 string reference to 'WebhookDeleteConfirmForm'
acquia_contenthub_publisher.routing.yml in modules/acquia_contenthub_publisher/acquia_contenthub_publisher.routing.yml
modules/acquia_contenthub_publisher/acquia_contenthub_publisher.routing.yml

File

modules/acquia_contenthub_publisher/src/Form/Webhook/WebhookDeleteConfirmForm.php, line 21

Namespace

Drupal\acquia_contenthub_publisher\Form\Webhook
View source
class WebhookDeleteConfirmForm extends FormBase {
  use SubscriptionManagerFormTrait;
  use AcquiaContentHubUnregisterHelperTrait;

  /**
   * The Acquia ContentHub Client object.
   *
   * @var \Acquia\ContentHubClient\ContentHubClient
   */
  protected $client;

  /**
   * The Acquia ContentHub Unregister event.
   *
   * @var \Drupal\acquia_contenthub\Event\AcquiaContentHubUnregisterEvent
   */
  protected $event;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $dispatcher;

  /**
   * The UUID of a webhook delete.
   *
   * @var string
   */
  protected $uuid;

  /**
   * WebhookDeleteConfirmForm constructor.
   *
   * @param \Drupal\acquia_contenthub\Client\ClientFactory $client_factory
   *   ACH client factory.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
   *   Symfony event dispatcher.
   */
  public function __construct(ClientFactory $client_factory, EventDispatcherInterface $dispatcher) {
    $this->client = $client_factory
      ->getClient();
    $this->dispatcher = $dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('acquia_contenthub.client.factory'), $container
      ->get('event_dispatcher'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $uuid = NULL) {
    $this->uuid = $uuid;
    $this->event = new AcquiaContentHubUnregisterEvent($this->uuid, '', TRUE);
    $this->dispatcher
      ->dispatch(AcquiaContentHubEvents::ACH_UNREGISTER, $this->event);
    $form['cancel'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Cancel'),
      '#button_type' => 'primary',
      '#name' => 'cancel',
      '#weight' => 101,
    ];
    $orphaned_filters = $this->event
      ->getOrphanedFilters();
    if (!empty($orphaned_filters)) {
      $form['filters'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('With webhook deletion the following filters will be deleted as well:'),
        '#open' => TRUE,
        '#weight' => -1,
      ];
      $form['filters']['orphaned_filters'] = [
        '#type' => 'table',
        '#title' => $this
          ->t('Filters'),
        '#header' => [
          'Filter name',
          'Filter UUID',
        ],
        '#rows' => $this
          ->formatOrphanedFiltersTable($orphaned_filters),
      ];
      $form['actions']['delete_webhook_and_filters'] = [
        '#type' => 'submit',
        '#submit' => [
          [
            $this,
            'deleteFilters',
          ],
          [
            $this,
            'deleteWebhook',
          ],
        ],
        '#value' => $this
          ->t('Delete webhook and filters'),
        '#button_type' => 'primary',
        '#weight' => 100,
        '#limit_validation_errors' => [],
      ];
      if ($this
        ->checkDiscoveryRoute()) {
        $form['actions']['redirect'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Go to Discovery Interface'),
          '#url' => Url::fromRoute('acquia_contenthub_curation.discovery'),
          '#weight' => 99,
          '#attributes' => [
            'class' => [
              'button',
            ],
          ],
        ];
      }
      return $form;
    }
    $this
      ->messenger()
      ->addStatus('Everything is in order, safe to proceed!');
    $form['actions']['delete_webhook'] = [
      '#type' => 'submit',
      '#submit' => [
        [
          $this,
          'deleteFilters',
        ],
        [
          $this,
          'deleteWebhook',
        ],
      ],
      '#value' => $this
        ->t('Delete webhook'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getTriggeringElement()['#name'] === 'cancel') {
      $form_state
        ->setRedirect('acquia_contenthub.subscription_settings');
    }
  }

  /**
   * Delete orphaned and default filters from event.
   */
  public function deleteFilters() : void {
    foreach ($this->event
      ->getOrphanedFilters() as $filter_id) {
      $response = $this->client
        ->deleteFilter($filter_id);
      if (!$this
        ->isResponseSuccessful($response, $this
        ->t('delete'), $this
        ->t('filter'), $filter_id, $this
        ->messenger())) {
        return;
      }
    }
    $default_filter_response = $this->client
      ->deleteFilter($this->event
      ->getDefaultFilter());
    $this
      ->isResponseSuccessful($default_filter_response, $this
      ->t('delete'), $this
      ->t('default filter'), $this->event
      ->getDefaultFilter(), $this
      ->messenger());
  }

  /**
   * Delete webhook.
   *
   * @param array $form
   *   Form object.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state interface object.
   */
  public function deleteWebhook(array &$form, FormStateInterface $form_state) : void {
    $response = $this->client
      ->deleteWebhook($this->uuid);
    if (!$this
      ->isResponseSuccessful($response, $this
      ->t('delete'), $this
      ->t('webhook'), $this->uuid, $this
      ->messenger())) {
      return;
    }
    $this
      ->messenger()
      ->addStatus($this
      ->t('Webhook %uuid has been deleted successfully.', [
      '%uuid' => $this->uuid,
    ]));
    $form_state
      ->setRedirect('acquia_contenthub.subscription_settings');
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'acquia_contenthub_webhook_delete_confirm_form';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaContentHubUnregisterHelperTrait::checkDiscoveryRoute public function Checks if Discovery Interface route exists.
AcquiaContentHubUnregisterHelperTrait::formatOrphanedFiltersTable protected function Format rows for render array.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SubscriptionManagerFormTrait::isResponseSuccessful protected function Returns the success status of the response.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
WebhookDeleteConfirmForm::$client protected property The Acquia ContentHub Client object.
WebhookDeleteConfirmForm::$dispatcher protected property The event dispatcher.
WebhookDeleteConfirmForm::$event protected property The Acquia ContentHub Unregister event.
WebhookDeleteConfirmForm::$uuid protected property The UUID of a webhook delete.
WebhookDeleteConfirmForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
WebhookDeleteConfirmForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
WebhookDeleteConfirmForm::deleteFilters public function Delete orphaned and default filters from event.
WebhookDeleteConfirmForm::deleteWebhook public function Delete webhook.
WebhookDeleteConfirmForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
WebhookDeleteConfirmForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
WebhookDeleteConfirmForm::__construct public function WebhookDeleteConfirmForm constructor.