Skip to content

Commit

Permalink
Convert message subscribe to DeliveryCandidate.
Browse files Browse the repository at this point in the history
- Converts all message_subscribe implementations to use the
  DeliveryCandidate object rather than arrays.
  • Loading branch information
jhedstrom committed Mar 16, 2017
1 parent 9ec6c7b commit 8446c20
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 333 deletions.
14 changes: 4 additions & 10 deletions message_subscribe.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@
* value. According to this context this function will retrieve the
* related subscribers.
*
* @return \Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface[]|array
* Array of delivery candidate objects keyed with the user ID, or an array of
* arrays with the value (array usage is deprecated and will be removed in
* 2.0):
* - "flags": Array with the flag names that resulted with including
* the user.
* - "notifiers": Array with the Message notifier name plugins.
* @return \Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface[]
* An array, keyed by recipeint user ID, of delivery candidate objects.
*/
function hook_message_subscribe_get_subscribers(MessageInterface $message, array $subscribe_options = [], array $context = []) {
return [
Expand All @@ -45,10 +40,9 @@ function hook_message_subscribe_get_subscribers(MessageInterface $message, array
/**
* Alter the subscribers list.
*
* @param \Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface[]|array &$uids
* @param \Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface[] &$uids
* The array of delivery candidates as defined by
* `hook_message_subscribe_get_subscribers()`. This can also be an array of
* arrays, but is deprecated.
* `hook_message_subscribe_get_subscribers()`.
* @param array $values
* A keyed array of values containing:
* - 'context' - The context array.
Expand Down
4 changes: 1 addition & 3 deletions message_subscribe.module
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ function message_subscribe_message_subscribe_get_subscribers(MessageInterface $m

$uids[$row->uid] = !empty($uids[$row->uid]) ? $uids[$row->uid] : new DeliveryCandidate([], [], $row->uid);
// Register the flag name.
$flags = $uids[$row->uid]->getFlags();
$flags[] = $row->flag_id;
$uids[$row->uid]->setFlags(array_unique($flags));
$uids[$row->uid]->addFlag($row->flag_id);

if ($range) {
--$range;
Expand Down
16 changes: 4 additions & 12 deletions src/Subscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Drupal\message\MessageInterface;
use Drupal\message_notify\MessageNotifier;
use Drupal\message_subscribe\Exception\MessageSubscribeException;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;
use Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface;
use Drupal\og\MembershipManagerInterface;
use Drupal\user\EntityOwnerInterface;

Expand Down Expand Up @@ -182,13 +180,6 @@ public function sendMessage(EntityInterface $entity, MessageInterface $message,
}

foreach ($uids as $uid => $delivery_candidate) {
// Array usage is deprecated, but supported until 2.0.
if (!$delivery_candidate instanceof DeliveryCandidateInterface) {
trigger_error('Usage of arrays in subscriber information is deprecated and will be removed in Message Subscribe 2.0. Use a DeliveryCandidate object instead.', E_USER_DEPRECATED);
$delivery_candidate += ['notifiers' => []];
$delivery_candidate = new DeliveryCandidate($delivery_candidate['flags'], $delivery_candidate['notifiers'], $uid);
}

$last_uid = $uid;
// Clone the message in case it will need to be saved, it won't
// overwrite the existing one.
Expand Down Expand Up @@ -419,7 +410,7 @@ public function getBasicContext(EntityInterface $entity, $skip_detailed_context
/**
* Get the default notifiers for a given set of users.
*
* @param array &$uids
* @param \Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface[] &$uids
* An array detailing notification info for users.
*/
protected function addDefaultNotifiers(array &$uids) {
Expand All @@ -429,9 +420,10 @@ protected function addDefaultNotifiers(array &$uids) {
}
// Use notifier names as keys to avoid potential duplication of notifiers
// by other modules' hooks.
$notifiers = array_combine($notifiers, $notifiers);
foreach (array_keys($uids) as $uid) {
$uids[$uid]['notifiers'] += $notifiers;
foreach ($notifiers as $notifier) {
$uids[$uid]->addNotifier($notifier);
}
}
}

Expand Down
44 changes: 38 additions & 6 deletions src/Subscribers/DeliveryCandidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class DeliveryCandidate implements DeliveryCandidateInterface {
*
* @var string[]
*/
protected $flags;
protected $flags = [];

/**
* An array of notifier IDs for delivery.
*
* @var string[]
*/
protected $notifiers;
protected $notifiers = [];

/**
* The delivery candidate account ID.
Expand All @@ -39,11 +39,43 @@ class DeliveryCandidate implements DeliveryCandidateInterface {
* The delivery candidate account ID.
*/
public function __construct(array $flags, array $notifiers, $uid) {
$this->flags = $flags;
$this->notifiers = $notifiers;
$this->flags = array_combine($flags, $flags);
$this->notifiers = array_combine($notifiers, $notifiers);
$this->uid = $uid;
}

/**
* {@inheritdoc}
*/
public function addFlag($flag_id) {
$this->flags[$flag_id] = $flag_id;
return $this;
}

/**
* {@inheritdoc}
*/
public function removeFlag($flag_id) {
unset($this->flags[$flag_id]);
return $this;
}

/**
* {@inheritdoc}
*/
public function addNotifier($notifier_id) {
$this->notifiers[$notifier_id] = $notifier_id;
return $this;
}

/**
* {@inheritdoc}
*/
public function removeNotifier($notifier_id) {
unset($this->notifiers[$notifier_id]);
return $this;
}

/**
* {@inheritdoc}
*/
Expand All @@ -55,7 +87,7 @@ public function getFlags() {
* {@inheritdoc}
*/
public function setFlags(array $flag_ids) {
$this->flags = $flag_ids;
$this->flags = array_combine($flag_ids, $flag_ids);
return $this;
}

Expand All @@ -70,7 +102,7 @@ public function getNotifiers() {
* {@inheritdoc}
*/
public function setNotifiers(array $notifier_ids) {
$this->notifiers = $notifier_ids;
$this->notifiers = array_combine($notifier_ids, $notifier_ids);
return $this;
}

Expand Down
44 changes: 44 additions & 0 deletions src/Subscribers/DeliveryCandidateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ public function getFlags();
*/
public function setFlags(array $flag_ids);

/**
* Adds a flag.
*
* @param string $flag_id
* The flag ID to add.
*
* @return static
* Return the object.
*/
public function addFlag($flag_id);

/**
* Remove a flag.
*
* @param string $flag_id
* The flag ID to remove.
*
* @return static
* Return the object.
*/
public function removeFlag($flag_id);

/**
* Get the notifier IDs.
*
Expand All @@ -45,6 +67,28 @@ public function getNotifiers();
*/
public function setNotifiers(array $notifier_ids);

/**
* Adds a notifier.
*
* @param string $notifier_id
* The notifier ID to add.
*
* @return static
* Return the object.
*/
public function addNotifier($notifier_id);

/**
* Remove a notifier.
*
* @param string $notifier_id
* The notifier ID to remove.
*
* @return static
* Return the object.
*/
public function removeNotifier($notifier_id);

/**
* Gets the account ID of the recipient.
*
Expand Down
15 changes: 3 additions & 12 deletions src/SubscribersInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,9 @@ public function sendMessage(EntityInterface $entity, MessageInterface $message,
* (optional) The context array, passed by reference. This has the same
* elements as the `$context` paramater for `self::sendMessage()`.
*
* @return array
* Array keyed with the user IDs to send notifications to, and an array with
* the flags used for the subscription, and the notifier names to use.
*
* @code
* array(
* 1 => array(
* 'flags' => array('subscribe_node', 'subscribe_og'),
* 'notifiers' => array('email', 'sms'),
* ),
* );
* @endcode
* @return \Drupal\message_subscribe\Subscribers\DeliveryCandidateInterface[]
* Array of delivery candidate objects keyed with the user IDs to send
* notifications to.
*/
public function getSubscribers(EntityInterface $entity, MessageInterface $message, array $options = [], array &$context = []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ function message_subscribe_test_message_subscribe_get_subscribers_alter(array &$
\Drupal::state('message_subscribe_test')->set('alter_hook_called', TRUE);

if (!\Drupal::state('message_subscribe_test')->get('disable_subscribers_alter', FALSE)) {
// Add non-existent user 10001.
if (\Drupal::state('message_subscribe_test')->get('use_array', FALSE)) {
// Test to ensure array usage still works (it will throw a deprecated
// notice).
$uids[10001] = [
'flags' => ['bar_flag'],
'notifiers' => ['email'],
];
}
else {
$uids[10001] = new DeliveryCandidate(['bar_flag'], ['email'], 10001);
}
$uids[10001] = new DeliveryCandidate(['bar_flag'], ['email'], 10001);

// Remove user 2.
unset($uids[2]);
Expand Down
6 changes: 5 additions & 1 deletion tests/src/Kernel/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\message\Entity\Message;
use Drupal\message\Entity\MessageTemplate;
use Drupal\message_subscribe\Exception\MessageSubscribeException;
use Drupal\message_subscribe\Subscribers\DeliveryCandidate;

/**
* Test queue integration.
Expand Down Expand Up @@ -77,7 +78,10 @@ public function testQueue() {
}

// Assert message was saved and added to queue.
$uids = array_fill(1, 10, ['flags' => [], 'notifiers' => []]);
$uids = array_fill(1, 10, new DeliveryCandidate([], [], 1));
foreach ($uids as $uid => $candidate) {
$candidate->setAccountId($uid);
}
$subscribe_options = [
'uids' => $uids,
'skip context' => TRUE,
Expand Down
Loading

0 comments on commit 8446c20

Please sign in to comment.