You are here

public function AcquiaContenthubCommands::contenthubWebhooks in Acquia Content Hub 8

Perform a webhook management operation.

@option webhook_url The webhook URL to register or unregister.

@command acquia:contenthub-webhooks @aliases ach-wh,acquia-contenthub-webhooks

Parameters

$op: The operation to use. Options are: register, unregister, list.

array $options: An associative array of options whose values come from cli, aliases, config, etc.

Throws

\Exception

File

src/Commands/AcquiaContenthubCommands.php, line 626

Class

AcquiaContenthubCommands
A Drush commandfile.

Namespace

Drupal\acquia_contenthub\Commands

Code

public function contenthubWebhooks($op, array $options = [
  'webhook_url' => NULL,
]) {
  $config_factory = \Drupal::configFactory();

  /** @var \Drupal\acquia_contenthub\Client\ClientManager $client_manager */
  $client_manager = \Drupal::service('acquia_contenthub.client_manager');

  /** @var \Drupal\acquia_contenthub\ContentHubSubscription $subscription */
  $subscription = \Drupal::service('acquia_contenthub.acquia_contenthub_subscription');
  $webhook_url = $options['webhook_url'];
  if (empty($webhook_url)) {
    $webhook_url = Url::fromUri('internal:/acquia-contenthub/webhook', [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  if ($client_manager
    ->isConnected()) {
    $config = $config_factory
      ->getEditable('acquia_contenthub.admin_settings');
    switch ($op) {
      case 'register':
        $success = $subscription
          ->registerWebhook($webhook_url);
        if (!$success) {
          $this
            ->logger()
            ->log(LogLevel::CANCEL, dt('Registering webhooks encountered an error.'));
        }
        else {
          $webhook_uuid = $config
            ->get('webhook_uuid');
          $this
            ->logger()
            ->log(LogLevel::SUCCESS, dt('Registered Content Hub Webhook: @uuid', [
            '@uuid' => $webhook_uuid,
          ]));
        }
        break;
      case 'unregister':
        $webhook_url = isset($options['webhook_url']) ? $options['webhook_url'] : $config
          ->get('webhook_url');
        $success = $subscription
          ->unregisterWebhook($webhook_url);
        if (!$success) {
          $this
            ->logger()
            ->log(LogLevel::CANCEL, dt('There was an error unregistering the URL: @url', [
            '@url' => $webhook_url,
          ]));
        }
        break;
      case 'list':
        $settings = $subscription
          ->getSettings();
        if (!$settings) {
          break;
        }
        $webhooks = $settings
          ->getWebhooks();
        foreach ($webhooks as $index => $webhook) {
          $this
            ->output()
            ->writeln($index + 1 . '. ' . $webhook['url'] . ' - ' . $webhook['uuid']);
        }
        break;
      default:

        // Invalid operation.
        throw new \Exception(dt('The op "@op" is invalid', [
          '@op' => $op,
        ]));
    }
  }
  else {
    throw new \Exception(dt('The Content Hub client is not connected so the webhook operations could not be performed.'));
  }
}