You are here

class AssetRefresh in Media: Acquia DAM 8

Updates Acquia DAM assets.

@QueueWorker ( id = "media_acquiadam_asset_refresh", title =

Plugin annotation


@Translation("Acquia DAM Asset Refresh"),
  cron = {"time" = 30}
)

Hierarchy

Expanded class hierarchy of AssetRefresh

1 file declares its use of AssetRefresh
AcquiadamConfigFormTest.php in tests/src/Unit/AcquiadamConfigFormTest.php

File

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

Namespace

Drupal\media_acquiadam\Plugin\QueueWorker
View source
class AssetRefresh extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * Drupal logger channel service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $loggerChannel;

  /**
   * Drupal entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Media: Acquia DAM Asset Media Factory service.
   *
   * @var \Drupal\media_acquiadam\Service\AssetMediaFactory
   */
  protected $assetMediaFactory;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $loggerChannel, EntityTypeManagerInterface $entityTypeManager, AssetMediaFactory $assetMediaFactory, ConfigFactory $config_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->loggerChannel = $loggerChannel;
    $this->entityTypeManager = $entityTypeManager;
    $this->assetMediaFactory = $assetMediaFactory;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('logger.factory')
      ->get('media_acquiadam'), $container
      ->get('entity_type.manager'), $container
      ->get('media_acquiadam.asset_media.factory'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   *
   * @return bool
   *   TRUE if a media entity was updated successfully, FALSE - otherwise.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssetRefresh::$assetMediaFactory protected property Media: Acquia DAM Asset Media Factory service.
AssetRefresh::$configFactory protected property The config factory service.
AssetRefresh::$entityTypeManager protected property Drupal entity type manager service.
AssetRefresh::$loggerChannel protected property Drupal logger channel service.
AssetRefresh::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AssetRefresh::processItem public function Overrides QueueWorkerInterface::processItem
AssetRefresh::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.