You are here

class Acquiadam in Media: Acquia DAM 8

Same name in this branch
  1. 8 src/Acquiadam.php \Drupal\media_acquiadam\Acquiadam
  2. 8 src/Plugin/EntityBrowser/Widget/Acquiadam.php \Drupal\media_acquiadam\Plugin\EntityBrowser\Widget\Acquiadam

Class Acquiadam.

Abstracts away details of the REST API.

Hierarchy

Expanded class hierarchy of Acquiadam

6 files declare their use of Acquiadam
AcquiadamMediaTest.php in tests/src/Kernel/AcquiadamMediaTest.php
AcquiadamServiceTest.php in tests/src/Unit/AcquiadamServiceTest.php
AssetFileEntityHelperTest.php in tests/src/Unit/AssetFileEntityHelperTest.php
AssetMetadataHelperTest.php in tests/src/Unit/AssetMetadataHelperTest.php
AssetRefreshManagerTest.php in tests/src/Unit/AssetRefreshManagerTest.php

... See full list

1 string reference to 'Acquiadam'
media_acquiadam.services.yml in ./media_acquiadam.services.yml
media_acquiadam.services.yml
2 services use Acquiadam
media_acquiadam.acquiadam in ./media_acquiadam.services.yml
Drupal\media_acquiadam\Acquiadam
media_acquiadam.acquiadam_user_creds in ./media_acquiadam.services.yml
Drupal\media_acquiadam\Acquiadam

File

src/Acquiadam.php, line 15

Namespace

Drupal\media_acquiadam
View source
class Acquiadam implements AcquiadamInterface, ContainerInjectionInterface {

  /**
   * Temporary asset data storage.
   *
   * @var array
   */
  protected static $cachedAssets = [];

  /**
   * The Acquia DAM client service.
   *
   * @var \Drupal\media_acquiadam\Client
   */
  protected $acquiaDamClient;

  /**
   * Media: Acquia DAM logging service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $loggerChannel;

  /**
   * Acquiadam constructor.
   *
   * @param \Drupal\media_acquiadam\ClientFactory $client_factory
   *   An instance of ClientFactory that we can get a webdam client from.
   * @param string $credential_type
   *   The type of credentials to use.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerChannelFactory
   *   The Drupal LoggerChannelFactory service.
   */
  public function __construct(ClientFactory $client_factory, $credential_type, LoggerChannelFactoryInterface $loggerChannelFactory) {
    $this->acquiaDamClient = $client_factory
      ->get($credential_type);
    $this->loggerChannel = $loggerChannelFactory
      ->get('media_acquiadam');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('media_acquiadam.client_factory'), 'background', $container
      ->get('logger.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function __call($name, array $arguments) {
    $method_variable = [
      $this->acquiaDamClient,
      $name,
    ];
    return is_callable($method_variable) ? call_user_func_array($method_variable, $arguments) : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getFlattenedFolderList($folder_id = NULL) {
    $folder_data = [];
    if (is_null($folder_id)) {
      $folders = $this->acquiaDamClient
        ->getTopLevelFolders();
    }
    else {
      $folder = $this->acquiaDamClient
        ->getFolder($folder_id);
      $folders = !empty($folder->folders) ? $folder->folders : [];
    }
    foreach ($folders as $folder) {
      $folder_data[$folder->id] = $folder->name;
      $folder_list = $this
        ->getFlattenedFolderList($folder->id);
      foreach ($folder_list as $folder_id => $folder_name) {
        $folder_data[$folder_id] = $folder_name;
      }
    }
    return $folder_data;
  }

  /**
   * {@inheritdoc}
   */
  public function getAsset($assetId, $include_xmp = FALSE) {
    $asset = $this
      ->staticAssetCache('get', $assetId);

    // @BUG: XMP-less assets may bypass static caching.
    // Technically if the asset doesn't have xmp_metadata (and always returns
    // an empty value) this will bypass the cache version each call.
    $needs_xmp_get = $include_xmp && empty($asset->xmp_metadata);
    try {
      if (is_null($asset) || $needs_xmp_get) {
        $this
          ->staticAssetCache('set', $assetId, $this->acquiaDamClient
          ->getAsset($assetId, $include_xmp) ?? FALSE);
      }
    } catch (ClientException $x) {

      // We want specific handling for 404 errors so we can provide a more
      // relateable error message.
      if (404 != $x
        ->getCode()) {
        throw $x;
      }
      $this->loggerChannel
        ->warning('Received a missing asset response when trying to load asset @assetID. Was the asset deleted in Acquia DAM? DAM API client returned a @code exception code with the following message: %message', [
        '@assetID' => $assetId,
        '@code' => $x
          ->getCode(),
        '@message' => $x
          ->getMessage(),
      ]);
    } catch (\Exception $x) {
      $this
        ->staticAssetCache('set', $assetId, FALSE);
      $this->loggerChannel
        ->debug($x
        ->getMessage());
    }
    return $this
      ->staticAssetCache('get', $assetId);
  }

  /**
   * Static asset cache helper.
   *
   * This is a public standalone method to enable unit testing the behavior.
   *
   * @param string $op
   *   The operation to perform. One of get, set, or clear.
   * @param int $assetId
   *   The asset ID when using get or set.
   * @param \cweagans\webdam\Entity\Asset|false|null $asset
   *   The data to store under the given asset ID.
   *
   * @return mixed|null
   *   The static cache or NULL if unset.
   */
  public function staticAssetCache($op, $assetId = NULL, $asset = NULL) {
    if ('set' == $op) {
      return static::$cachedAssets[$assetId] = $asset;
    }
    elseif ('clear' == $op) {
      static::$cachedAssets = [];
    }
    return static::$cachedAssets[$assetId] ?? NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Acquiadam::$acquiaDamClient protected property The Acquia DAM client service.
Acquiadam::$cachedAssets protected static property Temporary asset data storage.
Acquiadam::$loggerChannel protected property Media: Acquia DAM logging service.
Acquiadam::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
Acquiadam::getAsset public function
Acquiadam::getFlattenedFolderList public function Get a list of folders keyed by ID. Overrides AcquiadamInterface::getFlattenedFolderList
Acquiadam::staticAssetCache public function Static asset cache helper.
Acquiadam::__call public function Passes method calls through to the DAM client object. Overrides AcquiadamInterface::__call
Acquiadam::__construct public function Acquiadam constructor.