You are here

public function AssetRefresh::processItem in Media: Acquia DAM 8

Return value

bool TRUE if a media entity was updated successfully, FALSE - otherwise.

Overrides QueueWorkerInterface::processItem

File

src/Plugin/QueueWorker/AssetRefresh.php, line 85

Class

AssetRefresh
Updates Acquia DAM assets.

Namespace

Drupal\media_acquiadam\Plugin\QueueWorker

Code

public function processItem($data) {
  if (empty($data['media_id'])) {
    return FALSE;
  }

  /** @var \Drupal\media\Entity\Media $entity */
  $entity = $this->entityTypeManager
    ->getStorage('media')
    ->load($data['media_id']);
  if (empty($entity)) {
    $this->loggerChannel
      ->error('Unable to load media entity @media_id in order to refresh the associated asset. Was the media entity deleted within Drupal?', [
      '@media_id' => $data['media_id'],
    ]);
    return FALSE;
  }
  try {
    $assetID = $this->assetMediaFactory
      ->get($entity)
      ->getAssetId();
    if (empty($assetID)) {
      $this->loggerChannel
        ->error('Unable to load asset ID from media entity @media_id. This might mean that the DAM and Drupal relationship has been broken. Please check the media entity.', [
        '@media_id' => $data['media_id'],
      ]);
      return FALSE;
    }
    $asset = $this->assetMediaFactory
      ->get($entity)
      ->getAsset();
  } catch (\Exception $x) {
    $this->loggerChannel
      ->error('Error trying to check asset from media entity @media_id', [
      '@media_id' => $data['media_id'],
    ]);
    return FALSE;
  }
  $perform_delete = $this->configFactory
    ->get('media_acquiadam.settings')
    ->get('perform_sync_delete');
  if ((empty($asset) || $asset->status == 'inactive') && $perform_delete && !$entity
    ->isPublished()) {
    $entity
      ->delete();
    $this->loggerChannel
      ->warning('Deleted media entity @media_id with asset id @assetID.', [
      '@media_id' => $data['media_id'],
      '@assetID' => $assetID,
    ]);
    return TRUE;
  }
  if (empty($asset)) {
    $this->loggerChannel
      ->warning('Unable to update media entity @media_id with information from asset @assetID because the asset was missing. This warning will continue to appear until the media entity has been deleted.', [
      '@media_id' => $data['media_id'],
      '@assetID' => $assetID,
    ]);
    return FALSE;
  }
  try {

    // Re-save the entity, prompting the clearing and redownloading of
    // metadata and asset file.
    $entity
      ->save();
  } catch (\Exception $x) {

    // If we're hitting an exception after the above checks there might be
    // something impacting the overall system, so prevent further queue
    // processing.
    throw new SuspendQueueException($x
      ->getMessage());
  }
  return TRUE;
}