class NotificationSubscriber in Bynder 4.0.x
Subscribes to Amazon SNS notifications to update bynder metadata.
Hierarchy
- class \Drupal\bynder_sns\EventSubscriber\NotificationSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of NotificationSubscriber
1 string reference to 'NotificationSubscriber'
- bynder_sns.services.yml in modules/
bynder_sns/ bynder_sns.services.yml - modules/bynder_sns/bynder_sns.services.yml
1 service uses NotificationSubscriber
File
- modules/
bynder_sns/ src/ EventSubscriber/ NotificationSubscriber.php, line 18
Namespace
Drupal\bynder_sns\EventSubscriberView source
class NotificationSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The bynder service.
*
* @var \Drupal\bynder\BynderServiceInterface
*/
protected $bynder;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* NotifcationSubscriber constructor.
*
* @param \Drupal\bynder\BynderServiceInterface $bynder
* The bynder service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
*/
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;
}
/**
* Generates hash based on active Bynder config and saves it into state.
*
* @param \Drupal\amazon_sns\Event\SnsMessageEvent $event
* The Event to process.
*/
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;
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[SnsEvents::NOTIFICATION][] = [
'onNotification',
];
return $events;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NotificationSubscriber:: |
protected | property | The bynder service. | |
NotificationSubscriber:: |
protected | property | The config factory. | |
NotificationSubscriber:: |
protected | property | The entity type manager. | |
NotificationSubscriber:: |
protected | property | The logger factory. | |
NotificationSubscriber:: |
public static | function | ||
NotificationSubscriber:: |
public | function | Generates hash based on active Bynder config and saves it into state. | |
NotificationSubscriber:: |
public | function | NotifcationSubscriber constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |