You are here

public function UserMailQueueJob::process in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_user/src/Plugin/AdvancedQueue/JobType/UserMailQueueJob.php \Drupal\social_user\Plugin\AdvancedQueue\JobType\UserMailQueueJob::process()
  2. 10.1.x modules/social_features/social_user/src/Plugin/AdvancedQueue/JobType/UserMailQueueJob.php \Drupal\social_user\Plugin\AdvancedQueue\JobType\UserMailQueueJob::process()

File

modules/social_features/social_user/src/Plugin/AdvancedQueue/JobType/UserMailQueueJob.php, line 101

Class

UserMailQueueJob
Advanced Queue Job to process email to users.

Namespace

Drupal\social_user\Plugin\AdvancedQueue\JobType

Code

public function process(Job $job) {
  try {

    // Get the Job data.
    $data = $job
      ->getPayload();

    // 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, $user
                ->getDisplayName());
            }
          }
        }

        // When there are email addresses configured.
        if (!empty($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']);
            }
          }
        }

        // Saving the entity and setting it to finished should send
        // a message about the batch completion.
        $queue_storage
          ->setFinished(TRUE);
        $queue_storage
          ->save();
        return JobResult::success('The user mail job was successfully finished.');
      }
    }

    // By default mark the Job as failed.
    $this
      ->getLogger('user_email_queue')
      ->error('The Job was not finished correctly, no error thrown.');
    return JobResult::failure('The Job was not finished correctly, no error thrown.');
  } catch (\Exception $e) {
    $this
      ->getLogger('user_email_queue')
      ->error($e
      ->getMessage());
    return JobResult::failure($e
      ->getMessage());
  }
}