You are here

public function SubscriptionManager::tidy in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/Subscription/SubscriptionManager.php \Drupal\simplenews\Subscription\SubscriptionManager::tidy()

File

src/Subscription/SubscriptionManager.php, line 225

Class

SubscriptionManager
Default subscription manager.

Namespace

Drupal\simplenews\Subscription

Code

public function tidy() {
  $days = $this->config
    ->get('subscription.tidy_unconfirmed');
  if (!$days) {
    return;
  }

  // Query subscribers with unconfirmed subscriptions due to be tidied.
  $max_age = strtotime("-{$days} days");
  $unconfirmed = \Drupal::entityQuery('simplenews_subscriber')
    ->condition('subscriptions.status', SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED)
    ->condition('subscriptions.timestamp', $max_age, '<')
    ->execute();

  // Exclude any subscribers with confirmed subscriptions.
  $confirmed = \Drupal::entityQuery('simplenews_subscriber')
    ->condition('subscriptions.status', SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED, '<>')
    ->execute();
  $delete = array_diff($unconfirmed, $confirmed);
  $this->subscriberStorage
    ->delete($this->subscriberStorage
    ->loadMultiple($delete));
}