You are here

public function UserMailQueueProcessor::processItem in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/src/Plugin/QueueWorker/UserMailQueueProcessor.php \Drupal\social_user\Plugin\QueueWorker\UserMailQueueProcessor::processItem()
  2. 10.3.x modules/social_features/social_user/src/Plugin/QueueWorker/UserMailQueueProcessor.php \Drupal\social_user\Plugin\QueueWorker\UserMailQueueProcessor::processItem()
  3. 10.0.x modules/social_features/social_user/src/Plugin/QueueWorker/UserMailQueueProcessor.php \Drupal\social_user\Plugin\QueueWorker\UserMailQueueProcessor::processItem()
  4. 10.1.x modules/social_features/social_user/src/Plugin/QueueWorker/UserMailQueueProcessor.php \Drupal\social_user\Plugin\QueueWorker\UserMailQueueProcessor::processItem()
  5. 10.2.x modules/social_features/social_user/src/Plugin/QueueWorker/UserMailQueueProcessor.php \Drupal\social_user\Plugin\QueueWorker\UserMailQueueProcessor::processItem()

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

modules/social_features/social_user/src/Plugin/QueueWorker/UserMailQueueProcessor.php, line 101

Class

UserMailQueueProcessor
Queue worker to process email to users.

Namespace

Drupal\social_user\Plugin\QueueWorker

Code

public function processItem($data) {

  // Validate if the queue data is complete before processing.
  if (self::validateQueueItem($data)) {

    // Get the email content that needs to be sent.

    /** @var \Drupal\social_queue_storage\Entity\QueueStorageEntity $queue_storage */
    $queue_storage = $this->storage
      ->getStorage('queue_storage_entity')
      ->load($data['mail']);

    // Check if it's from the configured email bundle type.
    if ($queue_storage
      ->bundle() === 'email') {

      // When there are user ID's configured.
      if ($data['users']) {

        // Load the users that are in the batch.
        $users = $this->storage
          ->getStorage('user')
          ->loadMultiple($data['users']);

        /** @var \Drupal\user\UserInterface $user */
        foreach ($users as $user) {

          // Attempt sending mail.
          if ($user
            ->getEmail()) {
            $this
              ->sendMail($user
              ->getEmail(), $user
              ->language()
              ->getId(), $queue_storage);
          }
        }
      }

      // When there are email addresses configured.
      if ($data['user_mail_addresses']) {
        foreach ($data['user_mail_addresses'] as $mail_address) {
          if ($this->emailValidator
            ->isValid($mail_address['email_address'])) {

            // Attempt sending mail.
            $this
              ->sendMail($mail_address['email_address'], $this->languageManager
              ->getDefaultLanguage()
              ->getId(), $queue_storage, $mail_address['display_name']);
          }
        }
      }

      // Check if this is the last item.
      if ($this
        ->lastItem($data['mail'])) {
        $queue_storage
          ->setFinished(TRUE);

        // Try to save a the storage entity to update the finished status.
        try {

          // Saving the entity and setting it to finished should send
          // a message template.
          $queue_storage
            ->save();
        } catch (EntityStorageException $e) {
          $this
            ->getLogger('user_email_queue')
            ->error($e
            ->getMessage());
        }
      }
    }
  }
}