-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_sns.module
73 lines (64 loc) · 1.84 KB
/
aws_sns.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @file
* Module file for the aws_sns module.
*/
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_insert().
*/
function aws_sns_entity_insert(EntityInterface $entity) {
aws_sns_send_message($entity, 'insert');
}
/**
* Implements hook_entity_update().
*/
function aws_sns_entity_update(EntityInterface $entity) {
aws_sns_send_message($entity, 'update');
}
/**
* Implements hook_entity_delete().
*/
function aws_sns_entity_delete(EntityInterface $entity) {
aws_sns_send_message($entity, 'delete');
}
/**
* Sends a message to SNS.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
* @param string $op
* The operation the entity is undergoing.
*/
function aws_sns_send_message(EntityInterface $entity, $op) {
/** @var \Drupal\Core\Config\ImmutableConfig $config */
$config = \Drupal::config('aws_sns.settings');
$topics = $config->get('topics') ?? [];
$entity_settings = $config->get('enabled_sns_entities');
$bundle = $entity->bundle();
foreach ($topics as $topic => $arn) {
$topic_settings = $entity_settings[$topic];
foreach ($topic_settings as $entity_type => $settings) {
if (!array_key_exists($bundle, $settings)) {
continue;
}
if (!array_key_exists($op, $settings[$bundle]['ops'])) {
continue;
}
if ($settings[$bundle]['ops'][$op]) {
$message = [
'entity_id' => $entity->id(),
'entity_type' => $entity->getEntityTypeId(),
'bundle' => $bundle,
'op' => $op,
'changed' => time(),
];
\Drupal::service('aws_sns')->sendMessage($arn, $message);
\Drupal::logger('aws_sns.module')->debug('Sent messagefor entity @entity_id for topic @topic', [
'@entity_id' => $entity->id(),
'@topic' => $topic,
]);
}
}
}
}