You are here

class SpoolList in Simplenews 8

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

List of mail spool entries.

Hierarchy

Expanded class hierarchy of SpoolList

File

src/Spool/SpoolList.php, line 10

Namespace

Drupal\simplenews\Spool
View source
class SpoolList implements SpoolListInterface {

  /**
   * Array with mail spool rows being processed.
   *
   * @var array
   */
  protected $mails;

  /**
   * Array of the processed mail spool rows.
   */
  protected $processed = array();

  /**
   * Creates a spool list.
   *
   * @param array $mails
   *   List of mail spool rows.
   */
  public function __construct(array $mails) {
    $this->mails = $mails;
  }

  /**
   * {@inheritdoc}
   */
  public function count() {
    return count($this->mails);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  function getProcessed() {
    $processed = $this->processed;
    $this->processed = array();
    return $processed;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SpoolList::$mails protected property Array with mail spool rows being processed.
SpoolList::$processed protected property Array of the processed mail spool rows.
SpoolList::count public function
SpoolList::getProcessed function Returns the processed mail spool rows, keyed by the msid. Overrides SpoolListInterface::getProcessed
SpoolList::nextMail public function Returns a Simplenews source to be sent. Overrides SpoolListInterface::nextMail
SpoolList::__construct public function Creates a spool list.