You are here

class MessageNotifier in Message Notify 8

Prepare and send notifications.

Hierarchy

Expanded class hierarchy of MessageNotifier

1 string reference to 'MessageNotifier'
message_notify.services.yml in ./message_notify.services.yml
message_notify.services.yml
1 service uses MessageNotifier
message_notify.sender in ./message_notify.services.yml
Drupal\message_notify\MessageNotifier

File

src/MessageNotifier.php, line 12

Namespace

Drupal\message_notify
View source
class MessageNotifier {

  /**
   * The notifier plugin manager.
   *
   * @var \Drupal\message_notify\Plugin\Notifier\Manager
   */
  protected $notifierManager;

  /**
   * Constructs the message notifier.
   *
   * @param \Drupal\message_notify\Plugin\Notifier\Manager $notifier_manager
   *   The notifier plugin manager.
   */
  public function __construct(Manager $notifier_manager) {
    $this->notifierManager = $notifier_manager;
  }

  /**
   * Process and send a message.
   *
   * @param \Drupal\message\MessageInterface $message
   *   The message entity being used for the notification.
   * @param array $options
   *   Array of options to override the plugin's default ones.
   * @param string $notifier_name
   *   Optional; The name of the notifier to use. Defaults to "email"
   *   sending method.
   *
   * @return bool
   *   Boolean value denoting success or failure of the notification.
   *
   * @throws \Drupal\message_notify\Exception\MessageNotifyException
   *   If no matching notifier plugin exists.
   */
  public function send(MessageInterface $message, array $options = [], $notifier_name = 'email') {
    if (!$this->notifierManager
      ->hasDefinition($notifier_name, FALSE)) {
      throw new MessageNotifyException('Could not send notification using the "' . $notifier_name . '" notifier.');
    }

    /** @var \Drupal\message_notify\Plugin\Notifier\MessageNotifierInterface $notifier */
    $notifier = $this->notifierManager
      ->createInstance($notifier_name, $options, $message);
    if ($notifier
      ->access()) {
      return $notifier
        ->send();
    }

    // @todo Throw exception instead?
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessageNotifier::$notifierManager protected property The notifier plugin manager.
MessageNotifier::send public function Process and send a message.
MessageNotifier::__construct public function Constructs the message notifier.