View source
<?php
namespace Drupal\media_acquiadam\Plugin\QueueWorker;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Queue\SuspendQueueException;
use Drupal\media_acquiadam\Service\AssetMediaFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AssetRefresh extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $loggerChannel;
protected $entityTypeManager;
protected $assetMediaFactory;
protected $configFactory;
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;
}
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'));
}
public function processItem($data) {
if (empty($data['media_id'])) {
return FALSE;
}
$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 {
$entity
->save();
} catch (\Exception $x) {
throw new SuspendQueueException($x
->getMessage());
}
return TRUE;
}
}