You are here

class BuildHooksDeploymentValidator in Build Hooks 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/Validation/Constraint/BuildHooksDeploymentValidator.php \Drupal\build_hooks\Plugin\Validation\Constraint\BuildHooksDeploymentValidator

Defines a validator for the BuildHooksEnvironment constraint.

Hierarchy

Expanded class hierarchy of BuildHooksDeploymentValidator

File

src/Plugin/Validation/Constraint/BuildHooksDeploymentValidator.php, line 14

Namespace

Drupal\build_hooks\Plugin\Validation\Constraint
View source
class BuildHooksDeploymentValidator extends ConstraintValidator implements ContainerInjectionInterface {

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

  /**
   * Creates a new BuildHooksEnvironmentConstraintValidator instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function validate($entity, Constraint $constraint) {

    /** @var \Drupal\build_hooks\Entity\DeploymentInterface $entity */
    $deployment_storage = $this->entityTypeManager
      ->getStorage($entity
      ->getEntityTypeId());
    if ($entity
      ->isDeployed()) {

      // Deployed entities don't matter.
      return;
    }
    $query = $deployment_storage
      ->getQuery()
      ->condition('status', 0)
      ->condition('environment', $entity
      ->bundle())
      ->range(0, 1)
      ->accessCheck(FALSE);
    if (!$entity
      ->isNew()) {
      $query
        ->condition('did', $entity
        ->id(), '<>');
    }
    $undeployed = $query
      ->execute();
    if ($undeployed) {
      $existing = $deployment_storage
        ->load(reset($undeployed));
      $this->context
        ->buildViolation($constraint->message, [
        '@label' => $existing
          ->label(),
        ':url' => $existing
          ->toUrl()
          ->toString(),
      ])
        ->atPath('status')
        ->addViolation();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BuildHooksDeploymentValidator::$entityTypeManager private property The entity type manager.
BuildHooksDeploymentValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
BuildHooksDeploymentValidator::validate public function
BuildHooksDeploymentValidator::__construct public function Creates a new BuildHooksEnvironmentConstraintValidator instance.