You are here

function drush_acquia_contenthub_webhooks in Acquia Content Hub 8

Content Hub webhook operations.

File

./acquia_contenthub.drush.inc, line 791
ContentHub Drush Commands.

Code

function drush_acquia_contenthub_webhooks($op, $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');
  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) {
          drush_log(dt('Registering webhooks encountered an error.'), LogLevel::CANCEL);
        }
        else {
          $webhook_uuid = $config
            ->get('webhook_uuid');
          drush_log(dt('Registered Content Hub Webhook: @uuid', [
            '@uuid' => $webhook_uuid,
          ]), LogLevel::SUCCESS);
        }
        break;
      case 'unregister':
        $webhook_url = $config
          ->get('webhook_url');
        $success = $subscription
          ->unregisterWebhook($webhook_url);
        if (!$success) {
          drush_log(dt('There was an error unregistering the URL: @url', [
            '@url' => $webhook_url,
          ]), LogLevel::CANCEL);
        }
        break;
      case 'list':
        $settings = $subscription
          ->getSettings();
        if (!$settings) {
          break;
        }
        $webhooks = $settings
          ->getWebhooks();
        foreach ($webhooks as $index => $webhook) {
          drush_print($index + 1 . '. ' . $webhook['url'] . ' - ' . $webhook['uuid']);
        }
        break;
      default:

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