public function NotificationSubscriber::onNotification in Bynder 4.0.x
Generates hash based on active Bynder config and saves it into state.
Parameters
\Drupal\amazon_sns\Event\SnsMessageEvent $event: The Event to process.
File
- modules/
bynder_sns/ src/ EventSubscriber/ NotificationSubscriber.php, line 74
Class
- NotificationSubscriber
- Subscribes to Amazon SNS notifications to update bynder metadata.
Namespace
Drupal\bynder_sns\EventSubscriberCode
public function onNotification(SnsMessageEvent $event) {
if ($sns_message = $event
->getMessage()) {
// If the topic is not configured or not matching, do not process the
// notification.
$config = $this->configFactory
->get('bynder_sns.settings');
if (!$config
->get('topic') || $config
->get('topic') != $sns_message['TopicArn']) {
return;
}
$message = Json::decode($sns_message['Message']);
if (empty($message['media_id'])) {
$this->loggerFactory
->get('bynder_sns')
->error('Notification not processed, no media ID found: @message', [
'@message' => print_r($event
->getMessage()
->toArray(), TRUE),
]);
return;
}
$source_fields = [];
foreach ($this->bynder
->getBynderMediaTypes() as $media_type) {
$source_fields[] = $media_type
->getSource()
->getConfiguration()['source_field'];
}
$source_fields = array_filter(array_unique($source_fields));
if (empty($source_fields)) {
$this->loggerFactory
->get('bynder_sns')
->error('Notification not processed, no Bynder source fields found.');
return;
}
$storage = $this->entityTypeManager
->getStorage('media');
$query = $storage
->getQuery();
$source_field_condition = $query
->orConditionGroup();
foreach (array_unique($source_fields) as $source_field) {
$source_field_condition
->condition($source_field, $message['media_id']);
}
$mid = $query
->condition($source_field_condition)
->range(0, 1)
->execute();
if ($mid) {
$media = $storage
->load(reset($mid));
$bynder_media_entities = [
$message['media_id'] => $media,
];
$updated = $this->bynder
->updateMediaEntities($bynder_media_entities);
if ($updated) {
$this->loggerFactory
->get('bynder_sns')
->notice('Updated bynder asset @label (@media_id)', [
'link' => $media
->toLink($this
->t('View'))
->toString(),
'@media_id' => $message['media_id'],
'@label' => $media
->label(),
]);
}
}
else {
$this->loggerFactory
->get('bynder_sns')
->notice('Notification not processed, no media entity for ID @id found', [
'@id' => $message['media_id'],
]);
return;
}
}
}