You are here

public function QueueSizeConstraintValidator::validate in Entityqueue 8

File

src/Plugin/Validation/Constraint/QueueSizeConstraintValidator.php, line 17

Class

QueueSizeConstraintValidator
Validates the QueueSize constraint.

Namespace

Drupal\entityqueue\Plugin\Validation\Constraint

Code

public function validate($entity, Constraint $constraint) {
  assert($entity instanceof EntitySubqueueInterface);
  assert($constraint instanceof QueueSizeConstraint);
  $queue = $entity
    ->getQueue();
  $min_size = $queue
    ->getMinimumSize();
  $max_size = $queue
    ->getMaximumSize();
  $act_as_queue = $queue
    ->getActAsQueue();
  $number_of_items = count($entity->items);

  // Do not allow less items than the minimum size.
  if ($min_size && $number_of_items < $min_size) {
    $this->context
      ->buildViolation($constraint->messageMinSize, [
      '%min_size' => $min_size,
    ])
      ->addViolation();
  }
  elseif (!$act_as_queue && $max_size && $number_of_items > $max_size) {
    $this->context
      ->buildViolation($constraint->messageMaxSize, [
      '%max_size' => $max_size,
    ])
      ->addViolation();
  }
}