You are here

public function WebhookEnableForm::submitForm in Acquia Content Hub 8.2

Throws

\Exception

Overrides FormInterface::submitForm

File

modules/acquia_contenthub_publisher/src/Form/Webhook/WebhookEnableForm.php, line 68

Class

WebhookEnableForm
Class WebhookEnableForm.

Namespace

Drupal\acquia_contenthub_publisher\Form\Webhook

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $webhooks = $this->client
    ->getWebHooks();
  $uuid = $this->uuid;

  /** @var \Acquia\ContentHubClient\Webhook $webhook */
  $webhook = current(array_filter($webhooks, function (Webhook $webhook) use ($uuid) {
    return $webhook
      ->getUuid() === $uuid;
  }));
  if ($webhook) {

    // Re-enable the webhook by performing a POST request.
    $response = $this->client
      ->addWebhook($webhook
      ->getUrl());
    if (empty($response) || empty($response['success'])) {
      $this
        ->messenger()
        ->addError($this
        ->t('Unable to re-enable webhook %uuid (%url). Error code: %code. Error message: %message', [
        '%uuid' => $this->uuid,
        '%url' => $webhook
          ->getUrl(),
        '%code' => $response['error']['code'] ?? $this
          ->t('n/a'),
        '%message' => $response['error']['message'] ?? $this
          ->t('n/a'),
      ]));
      return;
    }
    $form_state
      ->setRedirect('acquia_contenthub.subscription_settings');
    $this
      ->messenger()
      ->addStatus($this
      ->t('Webhook %uuid (%url) has been re-enabled.', [
      '%uuid' => $this->uuid,
      '%url' => $webhook
        ->getUrl(),
    ]));
    return;
  }
  $this
    ->messenger()
    ->addError($this
    ->t('Failed to re-enable webhook %uuid. The webhook is not found.', [
    '%uuid' => $this->uuid,
  ]));
}