You are here

public function SpoolStorage::countMails in Simplenews 8.2

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

Count data in mail spool table.

Parameters

array $conditions: (Optional) Array of conditions which are applied to the query. If not set, status defaults to SpoolStorageInterface::STATUS_PENDING, SpoolStorageInterface::STATUS_IN_PROGRESS.

Return value

int Count of mail spool elements of the passed in arguments.

Overrides SpoolStorageInterface::countMails

File

src/Spool/SpoolStorage.php, line 165

Class

SpoolStorage
Default database spool storage.

Namespace

Drupal\simplenews\Spool

Code

public function countMails(array $conditions = []) {

  // Continue to support 'nid' as a condition.
  if (!empty($conditions['nid'])) {
    $conditions['entity_type'] = 'node';
    $conditions['entity_id'] = $conditions['nid'];
    unset($conditions['nid']);
  }

  // Add default status condition if not set.
  if (!isset($conditions['status'])) {
    $conditions['status'] = [
      SpoolStorageInterface::STATUS_PENDING,
      SpoolStorageInterface::STATUS_IN_PROGRESS,
    ];
  }
  $query = $this->connection
    ->select('simplenews_mail_spool');

  // Add conditions.
  foreach ($conditions as $field => $value) {
    if ($field == 'status') {
      if (!is_array($value)) {
        $value = [
          $value,
        ];
      }
      $status_or = new Condition('OR');
      foreach ($value as $status) {
        $status_or
          ->condition('status', $status);
      }
      $query
        ->condition($status_or);
    }
    else {
      $query
        ->condition($field, $value);
    }
  }
  $query
    ->addExpression('COUNT(*)', 'count');
  return (int) $query
    ->execute()
    ->fetchField();
}