You are here

public function SpoolList::nextMail in Simplenews 8.2

Same name and namespace in other branches
  1. 8 src/Spool/SpoolList.php \Drupal\simplenews\Spool\SpoolList::nextMail()
  2. 3.x src/Spool/SpoolList.php \Drupal\simplenews\Spool\SpoolList::nextMail()

Returns a Simplenews mail to be sent.

Return value

\Drupal\simplenews\Mail\MailInterface Next mail to be sent.

Overrides SpoolListInterface::nextMail

File

src/Spool/SpoolList.php, line 81

Class

SpoolList
List of mail spool entries.

Namespace

Drupal\simplenews\Spool

Code

public function nextMail() {

  // Get the current mail spool row.
  $spool_data = current($this->spoolRows);

  // If we're done, return false.
  if (!$spool_data) {
    return FALSE;
  }
  $issue = \Drupal::entityTypeManager()
    ->getStorage($spool_data->entity_type)
    ->load($spool_data->entity_id);
  if (!$issue) {

    // Skip if the entity load failed.
    $this
      ->setLastMailResult(SpoolStorageInterface::STATUS_SKIPPED);
    return $this
      ->nextMail();
  }
  if (!empty($spool_data->data)) {
    $subscriber = Subscriber::create(unserialize($spool_data->data));
  }
  else {
    $subscriber = Subscriber::load($spool_data->snid);
  }
  if (!$subscriber || !$subscriber
    ->getMail()) {

    // Skip if loading the subscriber failed or no email is available.
    $this
      ->setLastMailResult(SpoolStorageInterface::STATUS_SKIPPED);
    return $this
      ->nextMail();
  }
  $mail = new MailEntity($issue, $subscriber, \Drupal::service('simplenews.mail_cache'));

  // Set the langcode.
  $spool_data->langcode = $mail
    ->getLanguage();
  return $mail;
}