View source
<?php
namespace Drupal\bynder;
use Drupal\bynder\Plugin\Field\FieldType\BynderMetadataItem;
use Drupal\bynder\Plugin\media\Source\Bynder;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\State\StateInterface;
use GuzzleHttp\Exception\ClientException;
class BynderService implements BynderServiceInterface {
const METADATA_UPDATE_ID_KEY = 'bynder.metadata_update_id';
const METADATA_UPDATE_TIMESTAMP_KEY = 'bynder.metadata_update_timestamp';
const MAX_ITEMS = 100;
protected $bynderApi;
protected $entityTypeManager;
protected $logger;
protected $state;
protected $time;
protected $configFactory;
protected $mediaStorage;
protected $moduleHandler;
public function __construct(BynderApiInterface $bynder_api, EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory, StateInterface $state, TimeInterface $time, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler = NULL) {
$this->bynderApi = $bynder_api;
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger_factory
->get('bynder');
$this->state = $state;
$this->time = $time;
$this->configFactory = $config_factory;
$this->mediaStorage = $entity_type_manager
->getStorage('media');
$this->moduleHandler = $module_handler ?: \Drupal::service('module_handler');
}
public function getBynderMediaTypes() {
$bynder_media_types = [];
foreach ($this->entityTypeManager
->getStorage('media_type')
->loadMultiple() as $media_type_id => $media_type) {
if ($media_type
->getSource() instanceof Bynder) {
$bynder_media_types[$media_type_id] = $media_type;
}
}
return $bynder_media_types;
}
public function getTotalCountOfMediaEntities() {
$bynder_media_types = $this
->getBynderMediaTypes();
if (empty($bynder_media_types)) {
return 0;
}
$count = $this->mediaStorage
->getQuery()
->accessCheck(FALSE)
->condition($this->mediaStorage
->getEntityType()
->getKey('bundle'), array_keys($bynder_media_types), 'IN')
->count()
->execute();
return (int) $count;
}
public function updateLocalMetadataCron() {
$update_frequency = (int) $this->configFactory
->get('bynder.settings')
->get('update_frequency');
if ($update_frequency === 0) {
return;
}
$last_update = $this->state
->get(static::METADATA_UPDATE_TIMESTAMP_KEY);
$request_time = $this->time
->getRequestTime();
if ($last_update && $request_time - $last_update < $update_frequency) {
return;
}
$results = $this
->updateMetadataLastMediaEntities($this->state
->get(static::METADATA_UPDATE_ID_KEY));
if (empty($results) || $results['total'] < static::MAX_ITEMS) {
$this->state
->set(static::METADATA_UPDATE_TIMESTAMP_KEY, $request_time);
$this->state
->delete(static::METADATA_UPDATE_ID_KEY);
return;
}
$this->state
->set(static::METADATA_UPDATE_ID_KEY, $results['max_id']);
}
public function updateMetadataLastMediaEntities($minimum_id = NULL, $limit = BynderService::MAX_ITEMS) {
$bynder_media_types = $this
->getBynderMediaTypes();
if (empty($bynder_media_types)) {
return [];
}
$entity_id_key = $this->mediaStorage
->getEntityType()
->getKey('id');
$query = $this->mediaStorage
->getQuery()
->accessCheck(FALSE)
->condition($this->mediaStorage
->getEntityType()
->getKey('bundle'), array_keys($bynder_media_types), 'IN')
->sort($entity_id_key)
->range(0, $limit);
if ($minimum_id) {
$query
->condition($entity_id_key, $minimum_id, '>');
}
$media_ids = $query
->execute();
$media_entities = $this->mediaStorage
->loadMultiple($media_ids);
$bynder_media_entities = [];
foreach ($media_entities as $media_entity) {
if ($remote_uuid = $media_entity
->getSource()
->getSourceFieldValue($media_entity)) {
$bynder_media_entities[$remote_uuid] = $media_entity;
}
}
if (empty($bynder_media_entities)) {
return [];
}
$updated_entities = $this
->updateMediaEntities($bynder_media_entities);
$missing_remote_entities = [];
foreach ($bynder_media_entities as $missing_remote_entity) {
$missing_remote_entities[$missing_remote_entity
->id()] = $missing_remote_entity;
$this->logger
->warning('The media entity (ID: @id, Remote UUID: @remote_uuid) has been removed from the remote system.', [
'@id' => $missing_remote_entity
->id(),
'@remote_uuid' => $missing_remote_entity
->getSource()
->getSourceFieldValue($missing_remote_entity),
]);
}
return [
'updated' => $updated_entities,
'skipped' => $missing_remote_entities,
'total' => count($media_ids),
'max_id' => max($media_ids),
];
}
public function updateMediaEntities(array &$bynder_media_entities) {
$query = [
'ids' => implode(',', array_keys($bynder_media_entities)),
];
try {
$media_list = $this->bynderApi
->getMediaList($query);
} catch (ClientException $e) {
$this->logger
->error($e
->getMessage());
return [];
}
$updated_entities = [];
foreach ($media_list as $index => $item) {
$media_entity = $bynder_media_entities[$item['id']];
$source = $media_entity
->getSource();
$remote_metadata = $source
->filterRemoteMetadata($item);
$has_changed = FALSE;
if ($source
->hasMetadataChanged($media_entity, $remote_metadata)) {
$media_entity
->set(BynderMetadataItem::METADATA_FIELD_NAME, Json::encode($remote_metadata));
$has_changed = TRUE;
}
$this->moduleHandler
->alter('bynder_media_update', $media_entity, $item, $has_changed);
if ($has_changed) {
$media_entity
->save();
}
$updated_entities[$media_entity
->id()] = $media_entity;
unset($bynder_media_entities[$item['id']]);
}
return $updated_entities;
}
}