You are here

public function SpoolStorage::addIssue in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/Spool/SpoolStorage.php \Drupal\simplenews\Spool\SpoolStorage::addIssue()

Adds a newsletter issue to the mail spool.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $issue: The newsletter issue to be sent.

Overrides SpoolStorageInterface::addIssue

File

src/Spool/SpoolStorage.php, line 231

Class

SpoolStorage
Default database spool storage.

Namespace

Drupal\simplenews\Spool

Code

public function addIssue(ContentEntityInterface $issue) {
  if (!in_array($issue->simplenews_issue->status, [
    SIMPLENEWS_STATUS_SEND_NOT,
    SIMPLENEWS_STATUS_SEND_PUBLISH,
  ])) {
    return;
  }
  if (!$issue
    ->isPublished()) {
    $issue->simplenews_issue->status = SIMPLENEWS_STATUS_SEND_PUBLISH;
    $issue
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Newsletter issue %title will be sent when published.', [
      '%title' => $issue
        ->getTitle(),
    ]));
    return;
  }
  $recipient_handler = $this
    ->getRecipientHandler($issue);
  $issue->simplenews_issue->subscribers = $recipient_handler
    ->addToSpool();
  $issue->simplenews_issue->sent_count = 0;
  $issue->simplenews_issue->error_count = 0;
  $issue->simplenews_issue->status = SIMPLENEWS_STATUS_SEND_PENDING;

  // Save except if already saving.
  if (!isset($issue->original)) {
    $issue
      ->save();
  }

  // Notify other modules that a newsletter was just spooled.
  $this->moduleHandler
    ->invokeAll('simplenews_spooled', [
    $issue,
  ]);

  // Attempt to send immediately, if configured to do so.
  if (\Drupal::service('simplenews.mailer')
    ->attemptImmediateSend([
    'entity_type' => $issue
      ->getEntityTypeId(),
    'entity_id' => $issue
      ->id(),
  ])) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Newsletter issue %title sent.', [
      '%title' => $issue
        ->getTitle(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Newsletter issue %title pending.', [
      '%title' => $issue
        ->getTitle(),
    ]));
  }
}