You are here

public function SpoolList::nextMail in Simplenews 8

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

Returns a Simplenews source to be sent.

A single source may represent any number of mail spool rows, e.g. by addressing them as BCC.

Return value

\Drupal\simplenews\Mail\MailInterface

Overrides SpoolListInterface::nextMail

File

src/Spool/SpoolList.php, line 44

Class

SpoolList
List of mail spool entries.

Namespace

Drupal\simplenews\Spool

Code

public function nextMail() {

  // Get the current mail spool row and update the internal pointer to the
  // next row.
  $spool_data = current($this->mails);
  next($this->mails);

  // If we're done, return false.
  if (!$spool_data) {
    return FALSE;
  }

  // Store this spool row as processed.
  $this->processed[$spool_data->msid] = $spool_data;
  $entity = \Drupal::entityTypeManager()
    ->getStorage($spool_data->entity_type)
    ->load($spool_data->entity_id);
  if (!$entity) {

    // If the entity load failed, set the processed status done and proceed with
    // the next mail.
    $this->processed[$spool_data->msid]->result = array(
      'status' => SpoolStorageInterface::STATUS_DONE,
      'error' => TRUE,
    );
    return $this
      ->nextMail();
  }
  if ($spool_data->data) {
    $subscriber = $spool_data->data;
  }
  else {
    $subscriber = simplenews_subscriber_load_by_mail($spool_data->mail);
  }
  if (!$subscriber) {

    // If loading the subscriber failed, set the processed status done and
    // proceed with the next mail.
    $this->processed[$spool_data->msid]->result = array(
      'status' => SpoolStorageInterface::STATUS_DONE,
      'error' => TRUE,
    );
    return $this
      ->nextMail();
  }
  $mail = new MailEntity($entity, $subscriber, \Drupal::service('simplenews.mail_cache'));

  // Set the langcode langcode.
  $this->processed[$spool_data->msid]->langcode = $mail
    ->getEntity()
    ->language()
    ->getId();
  return $mail;
}