You are here

class DeveloperEmailUniqueValidator in Apigee Edge 8

Checks if an email address already belongs to a developer on Edge.

Hierarchy

Expanded class hierarchy of DeveloperEmailUniqueValidator

2 files declare their use of DeveloperEmailUniqueValidator
apigee_edge.module in ./apigee_edge.module
Copyright 2018 Google Inc.
UserDeveloperConverter.php in src/UserDeveloperConverter.php

File

src/Plugin/Validation/Constraint/DeveloperEmailUniqueValidator.php, line 33

Namespace

Drupal\apigee_edge\Plugin\Validation\Constraint
View source
class DeveloperEmailUniqueValidator extends ConstraintValidator implements ContainerInjectionInterface {

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

  /**
   * Stores email addresses that should not be validated.
   *
   * @var array
   */
  private static $whitelist = [];

  /**
   * DeveloperEmailUniqueValidator constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {
    if (in_array($items->value, static::$whitelist)) {
      return;
    }

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $items
      ->getEntity();

    // If field's value has not changed do not validate it.
    if (!$entity
      ->isNew()) {
      $original = $this->entityTypeManager
        ->getStorage($entity
        ->getEntityType()
        ->id())
        ->load($entity
        ->id());
      if ($original->{$items
        ->getName()}->value === $items->value) {
        return;
      }
    }
    try {
      $developer = Developer::load($items->value);
      if ($developer) {
        $this->context
          ->addViolation($constraint->message, [
          '%email' => $items->value,
        ]);
      }
    } catch (\Exception $exception) {

      // Nothing to do here, if there is no connection to Apigee Edge interrupt
      // the registration in the
      // apigee_edge_form_user_form_api_connection_validate() function.
    }
  }

  /**
   * Whitelist email address for validation.
   *
   * @param string $email
   *   Email address to whitelist.
   */
  public static function whitelist(string $email) {
    static::$whitelist[] = $email;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeveloperEmailUniqueValidator::$entityTypeManager private property The entity type manager.
DeveloperEmailUniqueValidator::$whitelist private static property Stores email addresses that should not be validated.
DeveloperEmailUniqueValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
DeveloperEmailUniqueValidator::validate public function Checks if the passed value is valid.
DeveloperEmailUniqueValidator::whitelist public static function Whitelist email address for validation.
DeveloperEmailUniqueValidator::__construct public function DeveloperEmailUniqueValidator constructor.