You are here

private function EntityManager::deleteRemoteEntity in Acquia Content Hub 8

Delete an entity from Content Hub.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The Content Hub Entity.

1 call to EntityManager::deleteRemoteEntity()
EntityManager::unexportCandidateEntities in src/EntityManager.php
Delete entities from Content Hub that are explicitly un-exported.

File

src/EntityManager.php, line 362

Class

EntityManager
Provides a service for managing entity actions for Content Hub.

Namespace

Drupal\acquia_contenthub

Code

private function deleteRemoteEntity(EntityInterface $entity) {

  // Check if the entity was never exported first, to avoid sending a DELETE
  // request to the Content Hub service.
  if ($this->contentHubEntitiesTracking
    ->loadExportedByUuid($entity
    ->uuid()) === FALSE) {
    return;
  }

  /** @var \Drupal\acquia_contenthub\Client\ClientManagerInterface $client_manager */
  try {
    $client = $this->clientManager
      ->getConnection();
  } catch (ContentHubException $e) {
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->error($e
      ->getMessage());
    return;
  }
  $resource_url = $this
    ->getResourceUrl($entity);
  $args = [
    '@type' => $entity
      ->getEntityTypeId(),
    '@uuid' => $entity
      ->uuid(),
    '@id' => $entity
      ->id(),
  ];
  if (!$resource_url) {
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->error('Error trying to form a unique resource Url for @type with uuid @uuid and id @id', $args);
    return;
  }
  try {
    $uuid = $entity
      ->uuid();
    $response = $client
      ->deleteEntity($uuid);
    $args = [
      '@uuid' => $uuid,
      '@type' => $entity
        ->getEntityTypeId(),
      '@id' => $entity
        ->id(),
    ];
    $log_msg = 'Deleting remote entity with UUID = @uuid (@type, @id)';
    $contents = Json::decode($response
      ->getBody()
      ->getContents());
    if (isset($contents['request_id'])) {
      $log_msg .= ' (Request ID: @request_id.)';
      $args += [
        '@request_id' => $contents['request_id'],
      ];
    }
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->debug($log_msg, $args);
    $exported_entity = $this->contentHubEntitiesTracking
      ->loadExportedByUuid($uuid);
    if ($exported_entity) {
      $exported_entity
        ->delete();
    }
  } catch (RequestException $e) {
    $args['@error'] = $e
      ->getMessage();
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->error('Error trying to post the resource url for @type with uuid @uuid and id @id with a response from the API: @error', $args);
    return;
  } catch (\RuntimeException $e) {
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->error('Error trying to read response contents: @error', $e
      ->getMessage() . $e
      ->getTraceAsString());
    return;
  }

  // Make sure it is within the 2XX range. Expected response is a 202.
  $status_code = $response
    ->getStatusCode();
  if (substr($status_code, 0, 2) !== '20') {
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->error('Error trying to post the resource url for @type with uuid @uuid and id @id: Response status code was not 20X as expected.', $args);
  }
}