You are here

public function EmailConfirmerManager::getConfirmations in Email confirmer 8

Search for existent confirmation entities for a given email address.

Parameters

string $email: The email address.

string $status: Filter by confirmation status.

int $limit: Max results count. Defaults to no limit.

string $realm: A realm to filter by.

Return value

\Drupal\email_confirmer\EmailConfirmationInterface[] Array of confirmations found, most recent first. Empty array if none found.

Overrides EmailConfirmerManagerInterface::getConfirmations

1 call to EmailConfirmerManager::getConfirmations()
EmailConfirmerManager::getConfirmation in src/EmailConfirmerManager.php
Search for the recent confirmation for a given email address.

File

src/EmailConfirmerManager.php, line 79

Class

EmailConfirmerManager
Email confirmation service.

Namespace

Drupal\email_confirmer

Code

public function getConfirmations($email, $status = FALSE, $limit = 0, $realm = '') {
  $results = [];
  $confirmation_query = $this->entityTypeManager
    ->getStorage('email_confirmer_confirmation')
    ->getQuery();

  // Add realm filter condition.
  if (!empty($realm)) {
    $confirmation_query
      ->condition('realm', $realm);
  }

  // Add limit condition.
  if ($limit) {
    $confirmation_query
      ->range(0, $limit);
  }

  // Filter by status.
  switch ($status) {
    case 'expired':
      $confirmation_query
        ->condition('created', $this->time
        ->getRequestTime() - $this->config
        ->get('hash_expiration'), '<');
      break;
    case 'cancelled':
      $confirmation_query
        ->condition('status', EmailConfirmationInterface::CANCELLED);
      break;
    case 'confirmed':
      $confirmation_query
        ->condition('confirmed', EmailConfirmationInterface::CONFIRMED);
      break;
    case 'pending':

      // Non cancelled, confirmed and expired.
      $confirmation_query
        ->condition('status', EmailConfirmationInterface::ACTIVE)
        ->condition('confirmed', EmailConfirmationInterface::UNCONFIRMED)
        ->condition('created', $this->time
        ->getRequestTime() - $this->config
        ->get('hash_expiration'), '>');
      break;
  }
  $old_confirmations = $confirmation_query
    ->condition('email', $email)
    ->sort('created', 'DESC')
    ->execute();
  $confirmation_storage = $this->entityTypeManager
    ->getStorage('email_confirmer_confirmation');
  if (is_array($old_confirmations) && count($old_confirmations)) {

    // Return existent email confirmation.
    $results = array_values($confirmation_storage
      ->loadMultiple($old_confirmations));
  }
  return $results;
}