media_acquiadam-8.x-1.46/src/MediaEntityHelper.php
src/MediaEntityHelper.php
<?php
namespace Drupal\media_acquiadam;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\media\MediaInterface;
use Drupal\media_acquiadam\Service\AssetFileEntityHelper;
use GuzzleHttp\Exception\GuzzleException;
/**
* Class MediaEntityHelper.
*
* Functionality related to working with the Media entity that assets are tied
* to. The intent is to make it easier to test and rework the behavior without
* having everything in a singular class.
*/
class MediaEntityHelper {
use StringTranslationTrait;
/**
* Entity Type Manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Acquia DAM asset data service.
*
* @var \Drupal\media_acquiadam\AssetData
*/
protected $assetData;
/**
* Acquia DAM client.
*
* @var \Drupal\media_acquiadam\Acquiadam
*/
protected $acquiaDamClient;
/**
* Acquia DAM asset file helper service.
*
* @var \Drupal\media_acquiadam\Service\AssetFileEntityHelper
*/
protected $assetFileHelper;
/**
* The media entity that is being wrapped.
*
* @var \Drupal\media\MediaInterface
*/
protected $mediaEntity;
/**
* The logger service object.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $logger;
/**
* MediaEntityHelper constructor.
*
* @param \Drupal\media\MediaInterface $media
* The media entity to wrap.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity Type Manager service.
* @param \Drupal\media_acquiadam\AssetDataInterface $assetData
* Acquia DAM asset data service.
* @param \Drupal\media_acquiadam\AcquiadamInterface $acquiaDamClient
* Acquia DAM client.
* @param \Drupal\media_acquiadam\Service\AssetFileEntityHelper $assetFileHelper
* Acquia DAM file entity helper service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger service.
*/
public function __construct(MediaInterface $media, EntityTypeManagerInterface $entityTypeManager, AssetDataInterface $assetData, AcquiadamInterface $acquiaDamClient, AssetFileEntityHelper $assetFileHelper, LoggerChannelFactoryInterface $logger_factory) {
$this->entityTypeManager = $entityTypeManager;
$this->assetData = $assetData;
$this->acquiaDamClient = $acquiaDamClient;
$this->assetFileHelper = $assetFileHelper;
$this->mediaEntity = $media;
$this->logger = $logger_factory;
}
/**
* Returns an associated file or creates a new one.
*
* @return false|\Drupal\file\FileInterface
* A file entity or FALSE on failure.
*/
public function getFile() {
// If there is already a file on the media entity then we should use that.
$file = $this->getExistingFile();
// If there is an error fetching the asset, rely on existing file.
try {
$asset = $this->getAsset();
}
catch (GuzzleException $exception) {
return $file;
}
// If we're getting an updated version of the asset we need to grab a new
// version of the file.
if ($asset !== NULL) {
$is_different_version = $this->assetData->isUpdatedAsset($asset);
if (empty($file) || $is_different_version) {
$destination_folder = $this
->assetFileHelper
->getDestinationFromEntity(
$this->mediaEntity,
$this->getAssetFileField(),
$asset->file_upload_date
);
$file = $this->assetFileHelper->createNewFile($asset, $destination_folder);
if ($file) {
$this->assetData->set($asset->id, 'file_upload_date', strtotime($asset->file_upload_date));
}
}
}
return $file;
}
/**
* Attempts to load an existing file entity from the given media entity.
*
* @return \Drupal\file\FileInterface|false
* A loaded file entity or FALSE if none could be found.
*/
public function getExistingFile() {
try {
if ($fid = $this->getExistingFileId()) {
/** @var \Drupal\file\FileInterface $file */
$file = $this->entityTypeManager->getStorage('file')->load($fid);
}
}
catch (\Exception $x) {
$file = FALSE;
}
return !empty($file) ? $file : FALSE;
}
/**
* Gets the existing file ID from the given Media entity.
*
* @return int|false
* The existing file ID or FALSE if one was not found.
*/
public function getExistingFileId() {
return $this->getFieldPropertyValue($this->getAssetFileField()) ?? FALSE;
}
/**
* Gets the file field being used to store the asset.
*
* @return false|string
* The name of the file field on the media bundle or FALSE on failure.
*/
public function getAssetFileField() {
try {
/** @var \Drupal\media\Entity\MediaType $bundle */
$bundle = $this->entityTypeManager->getStorage('media_type')
->load($this->mediaEntity->bundle());
$field_map = !empty($bundle) ? $bundle->getFieldMap() : FALSE;
}
catch (\Exception $x) {
return FALSE;
}
return empty($field_map['file']) ? FALSE : $field_map['file'];
}
/**
* Get the asset from a media entity.
*
* @return \Drupal\media_acquiadam\Entity\Asset|null
* The asset or NULL on failure.
*/
public function getAsset() {
$assetId = $this->getAssetId();
if (empty($assetId)) {
return NULL;
}
try {
return $this->acquiaDamClient->getAsset($assetId);
}
catch (\Exception $e) {
$this->logger->get('media_acquiadam')->error($e->getMessage());
return NULL;
}
}
/**
* Get the asset ID for the given media entity.
*
* @return string|false
* The asset ID or FALSE on failure.
*/
public function getAssetId() {
$sourceField = $this->mediaEntity->getSource()
->getSourceFieldDefinition($this->mediaEntity->get('bundle')->entity)
->getName();
return $this->getFieldPropertyValue($sourceField) ?? FALSE;
}
/**
* Gets the value of a field without knowing the key to use.
*
* @param string $fieldName
* The field name.
*
* @return null|mixed
* The field value or NULL.
*/
protected function getFieldPropertyValue($fieldName) {
if ($this->mediaEntity->hasField($fieldName)) {
/** @var \Drupal\Core\Field\FieldItemInterface $item */
$item = $this->mediaEntity->{$fieldName}->first();
if (!empty($item)) {
$property_name = $item::mainPropertyName();
if (isset($item->{$property_name})) {
return $item->{$property_name};
}
}
}
return NULL;
}
}
