Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions message_notify_sms/message_notify_sms.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: 'Message notify SMS'
description: 'Send message notifications via SMS.'
core: 8.x
package: Message
dependencies:
- message_notify:message_notify
- smsframework:sms
type: module
147 changes: 147 additions & 0 deletions message_notify_sms/src/Plugin/Notifier/Sms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Drupal\message_notify_sms\Plugin\Notifier;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\message\MessageInterface;
use Drupal\message_notify\Exception\MessageNotifyException;
use Drupal\message_notify\Plugin\Notifier\MessageNotifierBase;
use Drupal\sms\Direction;
use Drupal\sms\Exception\NoPhoneNumberException;
use Drupal\sms\Exception\RecipientRouteException;
use Drupal\sms\Message\SmsMessage;
use Drupal\sms\Message\SmsMessageInterface;
use Drupal\sms\Provider\PhoneNumberProviderInterface;
use Drupal\sms\Provider\SmsProviderInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* SMS notifier.
*
* @Notifier(
* id = "sms",
* title = @Translation("SMS"),
* descriptions = @Translation("Send messages via SMS."),
* viewModes = {
* "sms_body"
* }
* )
*/
class Sms extends MessageNotifierBase {

/**
* The SMS phone number provider service.
*
* @var \Drupal\sms\Provider\PhoneNumberProviderInterface
*/
protected $phoneNumberProvider;

/**
* The SMS provider service.
*
* @var \Drupal\sms\Provider\SmsProviderInterface
*/
protected $smsProvider;

/**
* Constructs the SMS notifier plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* The message_notify logger channel.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Render\RendererInterface $render
* The rendering service.
* @param \Drupal\message\MessageInterface $message
* (optional) The message entity. This is required when sending or
* delivering a notification. If not passed to the constructor, use
* ::setMessage().
* @param \Drupal\sms\Provider\PhoneNumberProviderInterface $phone_number_provider
* The SMS phone number provider.
* @param \Drupal\sms\Provider\SmsProviderInterface $sms_provider
* The SMS provider service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, MessageInterface $message = NULL, PhoneNumberProviderInterface $phone_number_provider, SmsProviderInterface $sms_provider) {
// Set configuration defaults.
$configuration += [
'mail' => FALSE,
'language override' => FALSE,
];

parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $entity_type_manager, $render, $message);

$this->phoneNumberProvider = $phone_number_provider;
$this->smsProvider = $sms_provider;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MessageInterface $message = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('logger.channel.message_notify'),
$container->get('entity_type.manager'),
$container->get('renderer'),
$message,
$container->get('sms.phone_number'),
$container->get('sms.provider')
);
}

/**
* {@inheritdoc}
*/
public function deliver(array $output = []) {
$sms = new SmsMessage();
$sms->setMessage(strip_tags($output['sms_body']));

if (!empty($this->configuration['sms_number'])) {
// Phone number is directly attached to the message entity.
return $this->sendDirect($sms);
}

if (!$this->message->uid->entity instanceof UserInterface) {
throw new MessageNotifyException('No account passed to the SMS notifier plugin.');
}

try {
return (bool) $this->phoneNumberProvider->sendMessage($this->message->uid->entity, $sms);
}
catch (NoPhoneNumberException $e) {
$this->logger->error($e->getMessage());
}
}

/**
* Send directly to a provided SMS number.
*
* @param \Drupal\sms\Message\SmsMessageInterface $sms
* The SMS message. Should already contain the body/payload.
*
* @return bool
* Returns TRUE if the message was succesfully added to the SMS queue.
*/
protected function sendDirect(SmsMessageInterface $sms) {
try {
$sms->addRecipient($this->configuration['sms_number'])
->setDirection(Direction::OUTGOING);
return (bool) $this->smsProvider->queue($sms);
}
catch (RecipientRouteException $e) {
$this->logger->error($e->getMessage());
}
}

}
6 changes: 3 additions & 3 deletions src/Plugin/Notifier/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class Email extends MessageNotifierBase {
* (optional) The message entity. This is required when sending or
* delivering a notification. If not passed to the constructor, use
* ::setMessage().
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* @param \Drupal\Core\Mail\MailManagerInterface $phone_number_provider
* The mail manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, MessageInterface $message = NULL, MailManagerInterface $mail_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager, RendererInterface $render, MessageInterface $message = NULL, MailManagerInterface $phone_number_provider) {
// Set configuration defaults.
$configuration += [
'mail' => FALSE,
Expand All @@ -63,7 +63,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition

parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $entity_type_manager, $render, $message);

$this->mailManager = $mail_manager;
$this->mailManager = $phone_number_provider;
}

/**
Expand Down
34 changes: 0 additions & 34 deletions src/Plugin/Notifier/Sms.php

This file was deleted.