You are here

public function ContentHubConnectionManager::registerWebhook in Acquia Content Hub 8.2

Registers a webhook if it has not been registered already.

Parameters

string $webhook_url: The webhook url with to register. Provide the full route (/acquia-contenthub/webhook).

Return value

array The response of the attempt.

Throws

\Exception

1 call to ContentHubConnectionManager::registerWebhook()
ContentHubConnectionManager::updateWebhook in src/ContentHubConnectionManager.php
Updates the specified webhook on Content Hub.

File

src/ContentHubConnectionManager.php, line 135

Class

ContentHubConnectionManager
Responsible for connection management actions.

Namespace

Drupal\acquia_contenthub

Code

public function registerWebhook(string $webhook_url) : array {
  $this
    ->initialize();
  $response = $this->client
    ->addWebhook($webhook_url);
  if (isset($response['success']) && $response['success'] === FALSE) {
    if (isset($response['error']['code']) && $response['error']['code'] === self::WEBHOOK_ALREADY_EXISTS) {
      $wh = $this->client
        ->getWebHook($webhook_url);
      $response['uuid'] = $wh
        ->getUuid();
    }
    else {
      $this->logger
        ->error('Unable to register Webhook URL = @url, Error @e_code: "@e_message".', [
        '@url' => $webhook_url,
        '@e_code' => $response['error']['code'],
        '@e_message' => $response['error']['message'],
      ]);
      return [];
    }
  }
  $this
    ->addDefaultFilterToWebhook($response['uuid']);

  // Save Webhook Configuration.
  $this
    ->saveWebhookConfig($response['uuid'], $webhook_url);
  return $response;
}