NotificationSubscriber.php in Bynder 4.0.x
File
modules/bynder_sns/src/EventSubscriber/NotificationSubscriber.php
View source
<?php
namespace Drupal\bynder_sns\EventSubscriber;
use Drupal\amazon_sns\Event\SnsEvents;
use Drupal\amazon_sns\Event\SnsMessageEvent;
use Drupal\bynder\BynderServiceInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NotificationSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $bynder;
protected $entityTypeManager;
protected $loggerFactory;
protected $configFactory;
public function __construct(BynderServiceInterface $bynder, EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory) {
$this->bynder = $bynder;
$this->entityTypeManager = $entity_type_manager;
$this->loggerFactory = $logger_factory;
$this->configFactory = $config_factory;
}
public function onNotification(SnsMessageEvent $event) {
if ($sns_message = $event
->getMessage()) {
$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;
}
}
}
public static function getSubscribedEvents() {
$events[SnsEvents::NOTIFICATION][] = [
'onNotification',
];
return $events;
}
}