You are here

public function BuildHooksDeploymentValidator::validate 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::validate()

File

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

Class

BuildHooksDeploymentValidator
Defines a validator for the BuildHooksEnvironment constraint.

Namespace

Drupal\build_hooks\Plugin\Validation\Constraint

Code

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();
  }
}