You are here

public function ImportUpdateAssets::onHandleWebhook in Acquia Content Hub 8.2

Handles webhook events.

Parameters

\Drupal\acquia_contenthub\Event\HandleWebhookEvent $event: The HandleWebhookEvent object.

Throws

\Exception

File

modules/acquia_contenthub_subscriber/src/EventSubscriber/HandleWebhook/ImportUpdateAssets.php, line 108

Class

ImportUpdateAssets
Imports and updates assets.

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook

Code

public function onHandleWebhook(HandleWebhookEvent $event) {

  // @todo Would be nice to have one place with statuses list - $payload['status'].
  // @todo The same regarding $payload['crud'] and supported types ($asset['type']).
  $payload = $event
    ->getPayload();
  $client = $event
    ->getClient();

  // Nothing to do or log here.
  if ($payload['crud'] !== 'update') {
    return;
  }
  if ($payload['status'] !== 'successful' || !isset($payload['assets']) || !count($payload['assets'])) {
    $this->channel
      ->info('Payload will not be processed because it is not successful or it does not have assets.
        Payload data: @payload', [
      '@payload' => print_r($payload, TRUE),
    ]);
    return;
  }
  if ($payload['initiator'] === $client
    ->getSettings()
    ->getUuid()) {

    // Only log if we're trying to update something other than client objects.
    if ($payload['assets'][0]['type'] !== 'client') {
      $this->channel
        ->info('Payload will not be processed because its initiator is the existing client.
        Payload data: @payload', [
        '@payload' => print_r($payload, TRUE),
      ]);
    }
    return;
  }
  $uuids = [];
  $types = [
    'drupal8_content_entity',
    'drupal8_config_entity',
  ];
  foreach ($payload['assets'] as $asset) {
    $uuid = $asset['uuid'];
    $type = $asset['type'];
    if (!in_array($type, $types)) {
      $this->channel
        ->info('Entity with UUID @uuid was not added to the import queue because it has an unsupported type: @type', [
        '@uuid' => $uuid,
        '@type' => $type,
      ]);
      continue;
    }
    if ($this->tracker
      ->isTracked($uuid)) {
      $status = $this->tracker
        ->getStatusByUuid($uuid);
      if ($status === SubscriberTracker::AUTO_UPDATE_DISABLED) {
        $this->channel
          ->info('Entity with UUID @uuid was not added to the import queue because it has auto update disabled.', [
          '@uuid' => $uuid,
        ]);
        continue;
      }
    }
    $uuids[] = $uuid;
    $this->tracker
      ->queue($uuid);
    $this->channel
      ->info('Attempting to add entity with UUID @uuid to the import queue.', [
      '@uuid' => $uuid,
    ]);
  }
  if ($uuids) {
    $item = new \stdClass();
    $item->uuids = implode(', ', $uuids);
    $queue_id = $this->queue
      ->createItem($item);
    if (empty($queue_id)) {
      return;
    }
    $this->tracker
      ->setQueueItemByUuids($uuids, $queue_id);
    $this->channel
      ->info('Entities with UUIDs @uuids added to the import queue and to the tracking table.', [
      '@uuids' => print_r($uuids, TRUE),
    ]);
    if (!($this->config
      ->get('send_contenthub_updates') ?? TRUE)) {
      return;
    }

    // Add entities to interest list.
    $client
      ->addEntitiesToInterestList($client
      ->getSettings()
      ->getWebhook('uuid'), $uuids);

    // Update Client CDF metrics.
    $settings = $client
      ->getSettings();
    $event = new BuildClientCdfEvent(ClientCDFObject::create($settings
      ->getUuid(), [
      'settings' => $settings
        ->toArray(),
    ]));
    $this->dispatcher
      ->dispatch(AcquiaContentHubEvents::BUILD_CLIENT_CDF, $event);
    $this->clientCDFObject = $event
      ->getCdf();
    $client
      ->putEntities($this->clientCDFObject);
  }
}