Skip to content

Commit

Permalink
Merge pull request #121 from digirati-co-uk/feature/ELPP-2427
Browse files Browse the repository at this point in the history
ELPP-2427 Updated the notifications system to deal with taxonomy terms.
  • Loading branch information
nlisgo committed Mar 17, 2017
2 parents c44bb0e + af5777e commit a79adc4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
22 changes: 14 additions & 8 deletions src/modules/jcms_notifications/drush/jcms_notifications.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

use GuzzleHttp\Exception\RequestException;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;

/**
* Implements hook_drush_command().
Expand Down Expand Up @@ -43,13 +45,15 @@ function jcms_notifications_drush_command() {
'options' => [
'iterations' => 'Limit on the number of iterations made then terminate.',
'sleep' => 'The amount of time in seconds to sleep before iterating over the notifications table again (defaults to 30 seconds).',
'delay' => 'The amount of time in seconds to sleep before sending the message after receiving and loading the entities (defaults to 2 seconds).',
],
'drupal dependencies' => ['jcms_notifications'],
'aliases' => ['sendn'],
'examples' => [
'drush send-notifications' => 'Long running process that iterates infinitely over the notifications table .',
'drush send-notifications --iterations=20' => 'Iterate over the notifications table 20 times then stop.',
'drush send-notifications --sleep=10' => 'Sleep for 10 seconds after each iteration.',
'drush send-notifications --delay=2' => 'Sleep for 2 seconds before sending.',
],
];
return $items;
Expand Down Expand Up @@ -156,17 +160,19 @@ function drush_jcms_notifications_send_notifications() {
$i++;
}
// Read the table, get the IDs.
$notifications = $storage->getNotificationNids();
$nodes = \Drupal\node\Entity\Node::loadMultiple($notifications);
$notifications = $storage->getNotificationEntityIds();
$nodes = Node::loadMultiple($notifications);
$terms = Term::loadMultiple($notifications);
$entities = array_merge($nodes, $terms);
sleep($delay);
// Iterate through them.
$nids = [];
foreach ($nodes as $node) {
$nids[] = $node->id();
$busMessage = $sns_crud->sendMessage($node);
drush_print(dt('[!date] Sent notification !message (nid: !nid)', ['!date' => date('c'), '!message' => $busMessage->getMessageJson(), '!nid' => $node->id()]));
$entity_ids = [];
foreach ($entities as $entity) {
$entity_ids[] = $entity->id();
$busMessage = $sns_crud->sendMessage($entity);
drush_print(dt('[!date] Sent notification !message (entity id: !eid)', ['!date' => date('c'), '!message' => $busMessage->getMessageJson(), '!eid' => $entity->id()]));
}
$storage->deleteNotificationNids($nids);
$storage->deleteNotificationIds($entity_ids);
sleep($sleep);
}
drush_print(dt('Exiting because of limits reached.'));
Expand Down
24 changes: 20 additions & 4 deletions src/modules/jcms_notifications/jcms_notifications.install
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
<?php

use Drupal\jcms_notifications\MysqlNotificationStorage;
use Drupal\Core\Database\Database;

function jcms_notifications_schema() {
$schema['jcms_notifications'] = [
'description' => 'The base table for nodes.',
'description' => 'The base table for notifications.',
'fields' => [
'notification_id' => [
'description' => 'The primary identifier for a notification.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'node_id' => [
'description' => 'The primary identifier for a node.',
MysqlNotificationStorage::ID_FIELD => [
'description' => 'The primary identifier for a Drupal entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
],
'indexes' => [
'nid' => ['node_id'],
'entity_id' => [MysqlNotificationStorage::ID_FIELD],
],
'primary key' => ['notification_id'],
];
return $schema;
}

/**
* Implements hook_update_N().
*
* Updates the node_id field to entity_id.
*/
function jcms_notifications_update_8101() {
$old_schema = jcms_notifications_schema();
$spec = $old_schema['jcms_notifications'][MysqlNotificationStorage::ID_FIELD];
$schema = Database::getConnection()->schema();
$schema->changeField(MysqlNotificationStorage::TABLE, 'node_id',
MysqlNotificationStorage::ID_FIELD, $spec);
}
18 changes: 9 additions & 9 deletions src/modules/jcms_notifications/jcms_notifications.module
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ function jcms_notifications_help($route_name, RouteMatchInterface $route_match)
}

/**
* Implements hook_node_insert().
* Implements hook_entity_insert().
*/
function jcms_notifications_node_insert(EntityInterface $entity) {
function jcms_notifications_entity_insert(EntityInterface $entity) {
$notification_service = \Drupal::service('jcms_notifications.notification_storage');
$notification_service->saveNotificationNid($entity);
$notification_service->saveNotificationEntityId($entity);
}

/**
* Implements hook_node_update().
* Implements hook_entity_update().
*/
function jcms_notifications_node_update(EntityInterface $entity) {
function jcms_notifications_entity_update(EntityInterface $entity) {
$notification_service = \Drupal::service('jcms_notifications.notification_storage');
$notification_service->saveNotificationNid($entity);
$notification_service->saveNotificationEntityId($entity);
}

/**
* Implements hook_node_delete().
* Implements hook_entity_delete().
*/
function jcms_notifications_node_delete(EntityInterface $entity) {
function jcms_notifications_entity_delete(EntityInterface $entity) {
$notification_service = \Drupal::service('jcms_notifications.notification_storage');
$notification_service->saveNotificationNid($entity);
$notification_service->saveNotificationEntityId($entity);
}
25 changes: 15 additions & 10 deletions src/modules/jcms_notifications/src/MysqlNotificationStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ final class MysqlNotificationStorage implements NotificationStorageInterface {
*/
const TABLE = 'jcms_notifications';

/**
* Notification ID field name.
*/
const ID_FIELD = 'entity_id';

/**
* Drupal\Core\Database\Driver\mysql\Connection definition.
*
Expand All @@ -36,25 +41,25 @@ public function __construct(Connection $connection) {
/**
* @inheritdoc
*/
public function saveNotificationNid(EntityInterface $entity) {
public function saveNotificationEntityId(EntityInterface $entity) {
$whitelist_bundles = array_keys(NodeCrudNotificationService::ENTITY_TYPE_MAP);
if (!in_array($entity->bundle(), $whitelist_bundles)) {
return NULL;
}
$id = $entity->id();
return $this->connection->insert(self::TABLE)->fields(['node_id'], [$id])->execute();
return $this->connection->insert(self::TABLE)->fields([self::ID_FIELD], [$id])->execute();
}

/**
* @inheritdoc
*/
public function getNotificationNids(): array {
public function getNotificationEntityIds(): array {
$ids = [];
$query = $this->connection->select(self::TABLE);
$query->addField(self::TABLE, 'node_id');
$query->addField(self::TABLE, self::ID_FIELD);
$result = $query->execute();
foreach ($result->fetchAll() as $row) {
$id = $row->node_id;
$id = $row->{self::ID_FIELD};
$ids[$id] = $id;
}
return $ids;
Expand All @@ -63,18 +68,18 @@ public function getNotificationNids(): array {
/**
* @inheritdoc
*/
public function deleteNotificationNid(int $nodeId) {
public function deleteNotificationEntityId(int $entityId) {
$this->connection->delete(self::TABLE)
->condition('node_id', $nodeId)
->condition(self::ID_FIELD, $entityId)
->execute();
}

/**
* @inheritdoc
*/
public function deleteNotificationNids(array $nodeIds) {
foreach ($nodeIds as $nid) {
$this->deleteNotificationNid($nid);
public function deleteNotificationEntityIds(array $entityIds) {
foreach ($entityIds as $id) {
$this->deleteNotificationEntityId($id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function __construct(NotificationService $notification_service) {
* @return \Drupal\jcms_notifications\Notification\BusOutgoingMessage
*/
public function sendMessage(EntityInterface $entity): BusOutgoingMessage {
$sns_message = $this->getMessageFromNode($entity);
$sns_message = $this->getMessageFromEntity($entity);
$this->notificationService->sendNotification($sns_message);
return $sns_message;
}
Expand All @@ -114,7 +114,7 @@ public function sendMessage(EntityInterface $entity): BusOutgoingMessage {
*
* @return \Drupal\jcms_notifications\Notification\BusOutgoingMessage
*/
public function getMessageFromNode(EntityInterface $entity): BusOutgoingMessage {
public function getMessageFromEntity(EntityInterface $entity): BusOutgoingMessage {
$bundle = $entity->bundle();
$data = self::ENTITY_TYPE_MAP[$bundle];
$field_name = $data['field'];
Expand Down
20 changes: 10 additions & 10 deletions src/modules/jcms_notifications/src/NotificationStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
interface NotificationStorageInterface {

/**
* Saves a node ID for notifications.
* Saves a entity ID for notifications.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
*
* @return int|null
*/
public function saveNotificationNid(EntityInterface $entity);
public function saveNotificationEntityId(EntityInterface $entity);

/**
* Gets the node IDs from the notifications table then deletes them.
* Gets the entity IDs from the notifications table then deletes them.
*
* @return array
*/
public function getNotificationNids(): array;
public function getNotificationEntityIds(): array;

/**
* Deletes a notification node ID.
* Deletes a notification entity ID.
*
* @param int $nodeId
* @param int $entityId
*
* @return null
*/
public function deleteNotificationNid(int $nodeId);
public function deleteNotificationEntityId(int $entityId);

/**
* Takes an array of node IDs and deletes them.
* Takes an array of entity IDs and deletes them.
*
* @param array $nodeIds
* @param array $entityIds
*
* @return null
*/
public function deleteNotificationNids(array $nodeIds);
public function deleteNotificationEntityIds(array $entityIds);

}

0 comments on commit a79adc4

Please sign in to comment.