You are here

private function ActivitySendEmailJobType::sendToFrequencyManager in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.1.x modules/custom/activity_send/modules/activity_send_email/src/Plugin/AdvancedQueue/JobType/ActivitySendEmailJobType.php \Drupal\activity_send_email\Plugin\AdvancedQueue\JobType\ActivitySendEmailJobType::sendToFrequencyManager()
  2. 10.2.x modules/custom/activity_send/modules/activity_send_email/src/Plugin/AdvancedQueue/JobType/ActivitySendEmailJobType.php \Drupal\activity_send_email\Plugin\AdvancedQueue\JobType\ActivitySendEmailJobType::sendToFrequencyManager()

Send the queue items for further processing by frequency managers.

Parameters

array $parameters: The array of message_tempalte_id, current_message_frequency, target_account, activity entity, email body text.

Throws

\Drupal\Component\Plugin\Exception\PluginException

1 call to ActivitySendEmailJobType::sendToFrequencyManager()
ActivitySendEmailJobType::process in modules/custom/activity_send/modules/activity_send_email/src/Plugin/AdvancedQueue/JobType/ActivitySendEmailJobType.php

File

modules/custom/activity_send/modules/activity_send_email/src/Plugin/AdvancedQueue/JobType/ActivitySendEmailJobType.php, line 268

Class

ActivitySendEmailJobType
Advanced Queue Job to process email workers and send them to SendGrid.

Namespace

Drupal\activity_send_email\Plugin\AdvancedQueue\JobType

Code

private function sendToFrequencyManager(array $parameters) {
  if (empty($parameters['target_recipients'])) {
    $this
      ->getLogger('activity_send_email_worker')
      ->error('We expected some recipients. None were provided.');
    return JobResult::failure('We expected some recipients. None were provided.');
  }
  $user_storage = $this->entityTypeManager
    ->getStorage('user');
  if (!empty($parameters['langcode'])) {

    // Get the message text according to language.
    $body_text = EmailActivityDestination::getSendEmailOutputText($parameters['message'], $parameters['langcode']);
  }
  else {

    // We get the default body text.
    $body_text = EmailActivityDestination::getSendEmailOutputText($parameters['message']);
  }

  // We load all the target accounts.
  $target_accounts = $user_storage
    ->loadMultiple($parameters['target_recipients']);
  if (!empty($target_accounts)) {

    /** @var \Drupal\user\Entity\User $target_account */
    foreach ($target_accounts as $target_account) {
      if ($target_account instanceof User && !$target_account
        ->isBlocked()) {

        // If a site manager decides emails should not be sent to users
        // who have never logged in. We need to verify last accessed time,
        // so those users are not processed.
        if ($this->swiftmailSettings
          ->get('do_not_send_emails_new_users') && (int) $target_account
          ->getLastAccessedTime() === 0) {
          continue;
        }

        // Only for users that have access to related content.
        if ($parameters['activity']
          ->getRelatedEntity()
          ->access('view', $target_account)) {

          // If the website is multilingual, get the body text in
          // users preferred language. This will happen when the queue item
          // is not processed in a batch and thus we can't be sure if all
          // users in the queue have the same language.
          if (empty($parameters['langcode']) && $this->languageManager
            ->isMultilingual()) {
            $body_text = EmailActivityDestination::getSendEmailOutputText($parameters['message'], $target_account
              ->getPreferredLangcode());
          }

          // Send item to EmailFrequency instance.
          $instance = $this->frequencyManager
            ->createInstance($parameters['frequency']);
          $instance
            ->processItem($parameters['activity'], $parameters['message'], $target_account, $body_text);
        }
      }
    }
    return JobResult::success('We have successfully scheduled items to be processed by the Frequency Manager.');
  }
}