You are here

class EmailConfirmerManager in Email confirmer 8

Email confirmation service.

Hierarchy

Expanded class hierarchy of EmailConfirmerManager

1 string reference to 'EmailConfirmerManager'
email_confirmer.services.yml in ./email_confirmer.services.yml
email_confirmer.services.yml
1 service uses EmailConfirmerManager
email_confirmer in ./email_confirmer.services.yml
Drupal\email_confirmer\EmailConfirmerManager

File

src/EmailConfirmerManager.php, line 12

Namespace

Drupal\email_confirmer
View source
class EmailConfirmerManager implements EmailConfirmerManagerInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The email_confirmer config.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Construct an EmailConfirmerManager.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TimeInterface $time) {
    $this->entityTypeManager = $entity_type_manager;
    $this->config = $config_factory
      ->get('email_confirmer.settings');
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public function confirm($email, array $data = [], $realm = '') {
    $confirmation = $this
      ->getConfirmation($email, 'pending', $realm);
    if (!$confirmation) {
      $confirmation = $this
        ->createConfirmation($email);
    }

    // Set the realm.
    if (!empty($realm)) {
      $confirmation
        ->setRealm($realm);
    }

    // Store properties.
    foreach ($data as $key => $value) {
      $confirmation
        ->setProperty($key, $value);
    }
    $confirmation
      ->sendRequest();
    $confirmation
      ->save();
    return $confirmation;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getConfirmation($email, $status = FALSE, $realm = '') {
    $confirmation = NULL;
    $confirmations = $this
      ->getConfirmations($email, $status, 1, $realm);
    if (count($confirmations)) {
      $confirmation = $confirmations[0];
    }
    return $confirmation;
  }

  /**
   * {@inheritdoc}
   */
  public function createConfirmation($email) {
    return $this->entityTypeManager
      ->getStorage('email_confirmer_confirmation')
      ->create([
      'email' => $email,
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmailConfirmerManager::$config protected property The email_confirmer config.
EmailConfirmerManager::$entityTypeManager protected property The entity type manager.
EmailConfirmerManager::$time protected property The time service.
EmailConfirmerManager::confirm public function Starts an email confirmation process. Overrides EmailConfirmerManagerInterface::confirm
EmailConfirmerManager::createConfirmation public function Creates a new email confirmation entity. Overrides EmailConfirmerManagerInterface::createConfirmation
EmailConfirmerManager::getConfirmation public function Search for the recent confirmation for a given email address. Overrides EmailConfirmerManagerInterface::getConfirmation
EmailConfirmerManager::getConfirmations public function Search for existent confirmation entities for a given email address. Overrides EmailConfirmerManagerInterface::getConfirmations
EmailConfirmerManager::__construct public function Construct an EmailConfirmerManager.