View source
<?php
namespace Drupal\media_acquiadam;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Acquiadam implements AcquiadamInterface, ContainerInjectionInterface {
protected static $cachedAssets = [];
protected $acquiaDamClient;
protected $loggerChannel;
public function __construct(ClientFactory $client_factory, $credential_type, LoggerChannelFactoryInterface $loggerChannelFactory) {
$this->acquiaDamClient = $client_factory
->get($credential_type);
$this->loggerChannel = $loggerChannelFactory
->get('media_acquiadam');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('media_acquiadam.client_factory'), 'background', $container
->get('logger.factory'));
}
public function __call($name, array $arguments) {
$method_variable = [
$this->acquiaDamClient,
$name,
];
return is_callable($method_variable) ? call_user_func_array($method_variable, $arguments) : NULL;
}
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;
}
public function getAsset($assetId, $include_xmp = FALSE) {
$asset = $this
->staticAssetCache('get', $assetId);
$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) {
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);
}
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;
}
}