You are here

public function DeleteAssets::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/DeleteAssets.php, line 61

Class

DeleteAssets
Deletes assets based on a webhook.

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook

Code

public function onHandleWebhook(HandleWebhookEvent $event) : void {
  $payload = $event
    ->getPayload();
  $assets = $payload['assets'] ?? [];
  $client = $event
    ->getClient();
  $settings = $client
    ->getSettings();
  $client_uuid = $settings
    ->getUuid();
  if ('successful' !== $payload['status'] || 'delete' !== $payload['crud'] || $payload['initiator'] === $client_uuid || empty($assets)) {
    return;
  }
  $send_update = $this->config
    ->get('send_contenthub_updates') ?? TRUE;
  foreach ($assets as $asset) {
    if (!$this
      ->isSupportedType($asset['type'])) {
      continue;
    }
    $entity = $this->tracker
      ->getEntityByRemoteIdAndHash($asset['uuid']);
    if (!$entity) {

      // Clean up the tracker. The entity was deleted before import.
      $this->tracker
        ->delete($asset['uuid']);

      // Clean up the interest list. The entity was deleted before import.
      if ($settings && $send_update) {
        $webhook_uuid = $settings
          ->getWebhook('uuid');
        $client
          ->deleteInterest($asset['uuid'], $webhook_uuid);
      }
      continue;
    }
    $status = $this->tracker
      ->getStatusByUuid($asset['uuid']);

    // If entity updating is disabled, delete tracking but not the entity.
    if ($status === SubscriberTracker::AUTO_UPDATE_DISABLED) {
      $this->tracker
        ->delete($asset['uuid']);
      continue;
    }
    $entity
      ->delete();
  }
}