public function SmsMessageProcessor::ensureGateways in SMS Framework 8
Same name and namespace in other branches
- 2.x src/EventSubscriber/SmsMessageProcessor.php \Drupal\sms\EventSubscriber\SmsMessageProcessor::ensureGateways()
- 2.1.x src/EventSubscriber/SmsMessageProcessor.php \Drupal\sms\EventSubscriber\SmsMessageProcessor::ensureGateways()
Ensure all recipients are routed to a gateway.
Messages will be split into multiple if recipients need to be routed to different gateways.
Parameters
\Drupal\sms\Event\SmsMessageEvent $event: The SMS message preprocess event.
Throws
\Drupal\sms\Exception\RecipientRouteException Guarantees a gateway is set on the message, otherwise this exception is thrown.
File
- src/
EventSubscriber/ SmsMessageProcessor.php, line 169
Class
- SmsMessageProcessor
- Handles messages before they are processed by queue(), send(), or incoming().
Namespace
Drupal\sms\EventSubscriberCode
public function ensureGateways(SmsMessageEvent $event) {
$sms_messages = $event
->getMessages();
$result = [];
// Ignore messages if they already have a gateway.
foreach ($sms_messages as $k => $sms_message) {
if ($sms_message
->getGateway() instanceof SmsGatewayInterface) {
unset($sms_messages[$k]);
$result[] = $sms_message;
}
}
// Ensure all recipients in this message can be routed to a gateway.
foreach ($sms_messages as $sms_message) {
$gateways = [];
$recipients_all = $sms_message
->getRecipients();
foreach ($recipients_all as $recipient) {
$gateway = $this
->getGatewayForPhoneNumber($recipient);
if ($gateway instanceof SmsGatewayInterface) {
$gateways[$gateway
->id()][] = $recipient;
}
else {
$event
->stopPropagation();
throw new RecipientRouteException(sprintf('Unable to determine gateway for recipient %s.', $recipient));
}
}
// Recreate SMS messages depending on the gateway.
$base = $sms_message instanceof EntityInterface ? $sms_message
->createDuplicate() : clone $sms_message;
$base
->removeRecipients($recipients_all);
foreach ($gateways as $gateway_id => $recipients) {
$new = $base instanceof EntityInterface ? $base
->createDuplicate() : clone $base;
$result[] = $new
->addRecipients($recipients)
->setGateway(SmsGateway::load($gateway_id));
}
}
$event
->setMessages($result);
}