diff --git a/message_notify_sms/message_notify_sms.info.yml b/message_notify_sms/message_notify_sms.info.yml new file mode 100644 index 0000000..64c691c --- /dev/null +++ b/message_notify_sms/message_notify_sms.info.yml @@ -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 diff --git a/message_notify_sms/src/Plugin/Notifier/Sms.php b/message_notify_sms/src/Plugin/Notifier/Sms.php new file mode 100644 index 0000000..5e877ca --- /dev/null +++ b/message_notify_sms/src/Plugin/Notifier/Sms.php @@ -0,0 +1,147 @@ + 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()); + } + } + +} diff --git a/src/Plugin/Notifier/Email.php b/src/Plugin/Notifier/Email.php index d69493b..79bcb4c 100644 --- a/src/Plugin/Notifier/Email.php +++ b/src/Plugin/Notifier/Email.php @@ -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, @@ -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; } /** diff --git a/src/Plugin/Notifier/Sms.php b/src/Plugin/Notifier/Sms.php deleted file mode 100644 index 4d14ca1..0000000 --- a/src/Plugin/Notifier/Sms.php +++ /dev/null @@ -1,34 +0,0 @@ -message->smsNumber)) { - // Try to get the SMS number from the account. - $account = $this->message->uid->entitiy; - if (!empty($account->sms_user['number'])) { - $this->message->smsNumber = $account->sms_user['number']; - } - } - - if (empty($this->message->smsNumber)) { - throw new MessageNotifyException('Message cannot be sent using SMS as the "smsNumber" property is missing from the Message entity or user entity.'); - } - - return sms_send($this->message->smsNumber, strip_tags($output['message_notify_sms_body'])); - } - -}