You are here

class AssetData in Media: Acquia DAM 8

Acquia DAM Asset Data service implementation.

Hierarchy

Expanded class hierarchy of AssetData

2 files declare their use of AssetData
AssetDataTest.php in tests/src/Unit/AssetDataTest.php
MediaEntityHelperTest.php in tests/src/Unit/MediaEntityHelperTest.php
1 string reference to 'AssetData'
media_acquiadam.services.yml in ./media_acquiadam.services.yml
media_acquiadam.services.yml
1 service uses AssetData
media_acquiadam.asset_data in ./media_acquiadam.services.yml
Drupal\media_acquiadam\AssetData

File

src/AssetData.php, line 13

Namespace

Drupal\media_acquiadam
View source
class AssetData implements AssetDataInterface, ContainerInjectionInterface {

  /**
   * The database connection to use.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a new asset data service.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to use.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'));
  }

  /**
   * {@inheritdoc}
   */
  public function delete($assetID = NULL, $name = NULL) {
    $query = $this->connection
      ->delete('acquiadam_assets_data');

    // Cast scalars to array so we can consistently use an IN condition.
    if (isset($assetID)) {
      $query
        ->condition('asset_id', (array) $assetID, 'IN');
    }
    if (isset($name)) {
      $query
        ->condition('name', (array) $name, 'IN');
    }
    $query
      ->execute();
  }

  /**
   * Check if the given asset is different than what is stored.
   *
   * @param \cweagans\webdam\Entity\Asset $asset
   *   The current version of the asset.
   * @param bool $saveUpdatedVersion
   *   TRUE to save the new version (if newer than the existing).
   *
   * @return bool
   *   TRUE if the given asset is a different version than what has been stored.
   */
  public function isUpdatedAsset(Asset $asset, $saveUpdatedVersion = TRUE) {
    $current_version = intval($this
      ->get($asset->id, 'version'));
    $new_version = intval($asset->version);
    return $new_version !== $current_version;
  }

  /**
   * {@inheritdoc}
   */
  public function get($assetID = NULL, $name = NULL) {
    $query = $this->connection
      ->select('acquiadam_assets_data', 'ad')
      ->fields('ad');
    if (isset($assetID)) {
      $query
        ->condition('asset_id', $assetID);
    }
    if (isset($name)) {
      $query
        ->condition('name', $name);
    }
    $result = $query
      ->execute();

    // A specific value for a specific asset ID was requested.
    if (isset($assetID) && isset($name)) {
      $result = $result
        ->fetchAllAssoc('asset_id');
      if (isset($result[$assetID])) {
        return $result[$assetID]->serialized ? unserialize($result[$assetID]->value) : $result[$assetID]->value;
      }
      return NULL;
    }
    $return = [];

    // All values for a given asset ID were requested.
    if (isset($assetID)) {
      foreach ($result as $record) {
        $return[$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
      }
      return $return;
    }

    // All asset IDs for a given value were requested.
    if (isset($name)) {
      foreach ($result as $record) {
        $return[$record->asset_id] = $record->serialized ? unserialize($record->value) : $record->value;
      }
      return $return;
    }

    // Everything was requested.
    foreach ($result as $record) {
      $return[$record->asset_id][$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
    }
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function set($assetID, $name, $value) {
    $serialized = (int) (!is_scalar($value));
    if ($serialized) {
      $value = serialize($value);
    }
    $this->connection
      ->merge('acquiadam_assets_data')
      ->keys([
      'asset_id' => $assetID,
      'name' => $name,
    ])
      ->fields([
      'value' => $value,
      'serialized' => $serialized,
    ])
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssetData::$connection protected property The database connection to use.
AssetData::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
AssetData::delete public function Deletes data stored for an asset. Overrides AssetDataInterface::delete
AssetData::get public function Returns data stored for an asset. Overrides AssetDataInterface::get
AssetData::isUpdatedAsset public function Check if the given asset is different than what is stored.
AssetData::set public function Stores data for an asset. Overrides AssetDataInterface::set
AssetData::__construct public function Constructs a new asset data service.