Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
54d0772
implemented a way to split up a message based on gateway. only done f…
dpi May 9, 2016
b80538a
event subscriber experiments
dpi May 12, 2016
80a4e3c
Merge branch '8.x-1.x' into process-eventsubscriber
dpi May 14, 2016
df777ea
Bug fixes
dpi May 16, 2016
c780c47
Removed debug methods from processor. (almaudohTest, testAddGateway)
dpi May 16, 2016
7356917
inject services into SmsMessageProcessor
dpi May 16, 2016
2b13800
Add docs for events.
dpi May 16, 2016
3e42c29
fixed clone from incorrect message.
dpi May 16, 2016
94ca239
inject event dispatcher
dpi May 16, 2016
6a55cf6
doc nits
dpi May 17, 2016
901b6b9
Merge branch '8.x-1.x' into process-eventsubscriber
dpi May 17, 2016
668963b
Addressed dispatch shortcut nits
dpi May 17, 2016
5337f70
Ensure gateways property is not modified by uasort
dpi May 17, 2016
76bc9ba
Added documentation for events.
dpi May 17, 2016
b85974e
oops
dpi May 19, 2016
7d2df7b
Merge branch '8.x-1.x' into process-eventsubscriber
dpi May 23, 2016
1c18a6d
Merge branch '8.x-1.x' into process-eventsubscriber
dpi May 29, 2016
eab24a6
Merge branch '8.x-1.x' into process-eventsubscriber
dpi Jun 4, 2016
0a2a822
temporarily remove deprecation notices until a consensus is reached.
dpi Jun 4, 2016
5881c27
fixed message chunking reducing from one to zero because no recipient…
dpi Jun 4, 2016
99ee36d
Add a preprocess to detect if messages have no recipients.
dpi Jun 4, 2016
31560d0
Fixed some failing tests popping up as a result of new no-recipient e…
dpi Jun 4, 2016
0e1e3a6
Merge branch '8.x-1.x' into process-eventsubscriber
dpi Aug 17, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,13 @@ public function testDelaySmsMessage() {
$user = $this->createUser();
$sms_message = SmsMessage::create()
->setMessage($this->randomString())
->addRecipients($this->randomPhoneNumbers())
->setDirection(Direction::OUTGOING)
->setRecipientEntity($user)
->setAutomated(TRUE);
$this->smsProvider->queue($sms_message);
$return = $this->smsProvider->queue($sms_message);

$this->assertEquals($timestamp, $sms_message->getSendTime());
$this->assertEquals($timestamp, $return[0]->getSendTime());
}

/**
Expand All @@ -249,6 +250,7 @@ public function testDelaySmsMessageNotAutomated() {

$user = $this->createUser();
$sms_message = SmsMessage::create()
->addRecipients($this->randomPhoneNumbers(1))
->setMessage($this->randomString())
->setDirection(Direction::OUTGOING)
->setRecipientEntity($user)
Expand Down
91 changes: 90 additions & 1 deletion sms.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,95 @@ function hook_sms_incoming_postprocess(\Drupal\sms\Message\SmsMessageInterface $
* @param \Symfony\Component\HttpFoundation\Response $response
* The HTTP response that will be sent back to the server.
*/
function hook_sms_delivery_report(array $reports, Response $response) {
function hook_sms_delivery_report(array $reports, \Symfony\Component\HttpFoundation\Response $response) {
$response->setContent('OK');
}

/**
* Event subscribers for SMS Framework.
*
* Service definition:
* <code>
* ```yaml
* my_module.my_event_subscriber:
* class: Drupal\my_module\EventSubscriber\MySmsEventSubscriber
* tags:
* - { name: event_subscriber }
* ```
* </code>
*
* <code>
* <?php
* namespace Drupal\my_module\EventSubscriber;
* ?>
* </code>
*/
class MySmsEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface {

/**
* Process and chunk a SMS message before it is queued, sent, or received.
*
* @param \Drupal\sms\Event\SmsMessageEvent $event
* The SmsMessageEvent event.
*/
public function mySmsMessagePreprocess(\Drupal\sms\Event\SmsMessageEvent $event) {
$result = [];
foreach ($event->getMessages() as $message) {
// Modify or chunk messages.
$result[] = $message;
}
$event->setMessages($result);
}

/**
* Process and chunk a SMS message after it is queued, sent, or received.
*
* @param \Drupal\sms\Event\SmsMessageEvent $event
* The SmsMessageEvent event.
*/
public function mySmsMessagePostProcess(\Drupal\sms\Event\SmsMessageEvent $event) {
$result = [];
foreach ($event->getMessages() as $message) {
// Modify or chunk messages.
$result[] = $message;
}
$event->setMessages($result);
}

/**
* Determines valid gateways for a recipient phone number.
*
* This event is not always dispatched. It is only dispatch if no other
* preprocessors have added a gateway to a message.
*
* If you don't know whether you should add a gateway for a recipient, then
* it is best to not do anything at all. Let the rest of the framework
* continue to try to find a gateway.
*
* Only one gateway will be applied to the message for this recipient. The
* gateway with the largest priority wins.
*
* @param \Drupal\sms\Event\RecipientGatewayEvent $event
* The RecipientGatewayEvent event.
*/
public function mySmsMessageGateway(\Drupal\sms\Event\RecipientGatewayEvent $event) {
// The recipient phone number.
$event->getRecipient();
// Add a gateway for a phone number.
$event->addGateway($a_gateway);
// Add a gateway with a priority.
$event->addGateway($a_gateway, 333);
$event->addGateway($a_gateway, -333);
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events['sms.message.preprocess'][] = ['mySmsMessagePreprocess'];
$events['sms.message.postprocess'][] = ['mySmsMessagePostprocess'];
$events['sms.message.gateway'][] = ['mySmsMessageGateway'];
return $events;
}

}
7 changes: 6 additions & 1 deletion sms.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
alias: sms_provider.default
sms_provider.default:
class: Drupal\sms\Provider\DefaultSmsProvider
arguments: ['@config.factory', '@module_handler']
arguments: ['@event_dispatcher', '@config.factory', '@module_handler']
plugin.manager.sms_gateway:
class: Drupal\sms\Plugin\SmsGatewayPluginManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler']
Expand All @@ -13,3 +13,8 @@ services:
sms.queue:
class: Drupal\sms\Provider\SmsQueueProcessor
arguments: ['@entity_type.manager', '@queue', '@sms_provider']
sms.sms_message_processor:
class: Drupal\sms\EventSubscriber\SmsMessageProcessor
arguments: ['@event_dispatcher', '@config.factory']
tags:
- { name: event_subscriber }
140 changes: 140 additions & 0 deletions src/Event/RecipientGatewayEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Drupal\sms\Event;

use Symfony\Component\EventDispatcher\Event;
use Drupal\sms\Entity\SmsGatewayInterface;

/**
* Event fired to determine valid gateways for a recipient.
*/
class RecipientGatewayEvent extends Event {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a second event?


/**
* The recipient phone number.
*
* @var string
*/
protected $recipient;

/**
* An array of gateway doubles.
*
* @var array
* The array of gateway/priority doubles where:
* - Key 0: SmsGatewayInterface $gateway
* - Key 1: integer $priority
*/
protected $gateways = [];

/**
* Constructs the object.
*
* @param string $recipient
* The recipient phone number.
*/
public function __construct($recipient) {
$this->setRecipient($recipient);
}

/**
* Get the phone number for this event.
*
* @return string
* The phone number for this event.
*/
public function getRecipient() {
return $this->recipient;
}

/**
* Set the phone number for this event.
*
* @param string $recipient
* The phone number for this event.
*
* @return $this
* Return this event for chaining.
*/
public function setRecipient($recipient) {
$this->recipient = $recipient;
return $this;
}

/**
* Get the gateways for this event.
*
* @return array
* An array of doubles gateway/priority doubles.
*/
public function getGateways() {
return $this->gateways;
}

/**
* Return gateways ordered by priority from highest to lowest.
*
* @return \Drupal\sms\Entity\SmsGatewayInterface[]
*/
public function getGatewaysSorted() {
$gateways = $this->gateways;
uasort($gateways, function($a, $b) {
list(, $priority_a) = $a;
list(, $priority_b) = $b;
if ($priority_a == $priority_b) {
return 0;
}
return ($priority_a > $priority_b) ? -1 : 1;
});

$gateways = [];
foreach ($gateways as $tuple) {
list($gateway, ) = $tuple;
$gateways[] = $gateway;
}

return $gateways;
}

/**
* Add a gateway for the recipient on this event.
*
* @param \Drupal\sms\Entity\SmsGatewayInterface $gateway
* The gateway for the recipient
* @param int $priority
* The priority for this gateway.
*
* @return $this
* Return this event for chaining.
*/
public function addGateway(SmsGatewayInterface $gateway, $priority = 0) {
$this->gateways[] = [$gateway, $priority];
return $this;
}

/**
* Remove a gateway from this event.
*
* @param $gateway_id
* A gateway plugin ID.
*
* @param integer|NULL $priority
* The priority of the gateway to remove, or NULL to remove all gateways
* with the identifier.
*
* @return $this
* Return this event for chaining.
*/
public function removeGateway($gateway_id, $priority = NULL) {
foreach ($this->gateways as $k => $tuple) {
list($gateway, $gateway_priority) = $tuple;
if ($gateway_id == $gateway->id()) {
if (!isset($priority) || ($priority == $gateway_priority)) {
unset($this->gateways[$k]);
}
}
}
return $this;
}

}
54 changes: 54 additions & 0 deletions src/Event/SmsMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Drupal\sms\Event;

use Symfony\Component\EventDispatcher\Event;

/**
* Event fired when a new SMS message is being processed before or after send or
* queue.
*/
class SmsMessageEvent extends Event {

/**
* The SMS messages.
*
* @var \Drupal\sms\Message\SmsMessageInterface[]
*/
protected $messages;

/**
* Constructs the object.
*
* @param \Drupal\sms\Message\SmsMessageInterface[] $messages
* The SMS message.
*/
public function __construct(array $messages) {
$this->setMessages($messages);
}

/**
* Get all messages on this event.
*
* @return \Drupal\sms\Message\SmsMessageInterface[]
* The messages on this event.
*/
public function getMessages() {
return $this->messages;
}

/**
* Set the messages on this event.
*
* @param $messages
* The messages to set on this event.
*
* @return $this
* Returns this event for chaining.
*/
public function setMessages($messages) {
$this->messages = $messages;
return $this;
}

}
Loading