You are here

class PrivateMessageMailer in Private Message 8

A service class for sending notification emails for private messages.

Hierarchy

Expanded class hierarchy of PrivateMessageMailer

1 string reference to 'PrivateMessageMailer'
private_message.services.yml in ./private_message.services.yml
private_message.services.yml

File

src/Service/PrivateMessageMailer.php, line 16

Namespace

Drupal\private_message\Service
View source
class PrivateMessageMailer implements PrivateMessageMailerInterface {

  /**
   * The private message service.
   *
   * @var \Drupal\private_message\Service\PrivateMessageServiceInterface
   */
  private $privateMessageService;

  /**
   * The mail manager service.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  private $mailManager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  private $currentUser;

  /**
   * The user data service.
   *
   * @var \Drupal\user\UserDataInterface
   */
  private $userData;

  /**
   * The configuration factory service.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  private $config;

  /**
   * Constructs a new PrivateMessageMailer object.
   *
   * @param \Drupal\private_message\Service\PrivateMessageServiceInterface $privateMessageService
   *   The private message service.
   * @param \Drupal\Core\Mail\MailManagerInterface $mailManager
   *   The mail manager service.
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
   *   The current user.
   * @param \Drupal\user\UserDataInterface $userData
   *   The user data service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The configuration factory service.
   */
  public function __construct(PrivateMessageServiceInterface $privateMessageService, MailManagerInterface $mailManager, AccountProxyInterface $currentUser, UserDataInterface $userData, ConfigFactoryInterface $configFactory) {
    $this->privateMessageService = $privateMessageService;
    $this->mailManager = $mailManager;
    $this->currentUser = $currentUser;
    $this->userData = $userData;
    $this->config = $configFactory
      ->get('private_message.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function send(PrivateMessageInterface $message, PrivateMessageThreadInterface $thread, array $members = []) {
    $params = [
      'private_message' => $message,
      'private_message_thread' => $thread,
    ];

    // Remove the current user from the members array.
    $members = array_filter($members, function (AccountInterface $member) {
      return $member
        ->id() !== $this->currentUser
        ->id();
    });
    foreach ($members as $member) {
      $params['member'] = $member;
      if ($this
        ->shouldSend($member)) {
        $this->mailManager
          ->mail('private_message', 'message_notification', $member
          ->getEmail(), $member
          ->getPreferredLangcode(), $params);
      }
    }
  }

  /**
   * Determines if the message should be sent.
   *
   * Checks individual user preferences as well as system defaults.
   *
   * @param \Drupal\Core\Session\AccountInterface $recipient
   *   The potential recipient.
   *
   * @return bool
   *   A boolean indicating whether or not the message should be sent.
   */
  private function shouldSend(AccountInterface $recipient) {

    // If the recipient email address is not populated, return FALSE.
    if (!$recipient
      ->getEmail()) {
      return FALSE;
    }

    // If the user data value is set, return it as a boolean.
    if (($value = $this->userData
      ->get('private_message', $recipient
      ->id(), 'email_notification')) !== NULL) {
      return (bool) $value;
    }
    return $this->config
      ->get('enable_email_notifications') && $this->config
      ->get('send_by_default');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrivateMessageMailer::$config private property The configuration factory service.
PrivateMessageMailer::$currentUser private property The current user.
PrivateMessageMailer::$mailManager private property The mail manager service.
PrivateMessageMailer::$privateMessageService private property The private message service.
PrivateMessageMailer::$userData private property The user data service.
PrivateMessageMailer::send public function Send a private message notification email. Overrides PrivateMessageMailerInterface::send
PrivateMessageMailer::shouldSend private function Determines if the message should be sent.
PrivateMessageMailer::__construct public function Constructs a new PrivateMessageMailer object.