Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entityqueue / src / Plugin / Validation / Constraint / QueueSizeConstraintValidator.php
1 <?php
2
3 namespace Drupal\entityqueue\Plugin\Validation\Constraint;
4
5 use Symfony\Component\Validator\Constraint;
6 use Symfony\Component\Validator\ConstraintValidator;
7
8 /**
9  * Validates the QueueSize constraint.
10  */
11 class QueueSizeConstraintValidator extends ConstraintValidator {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function validate($entity, Constraint $constraint) {
17     $number_of_items = count($entity->items);
18
19     /** @var \Drupal\entityqueue\EntityQueueInterface $queue */
20     $queue = $entity->getQueue();
21     $min_size = $queue->getMinimumSize();
22     $max_size = $queue->getMaximumSize();
23     $act_as_queue = $queue->getActAsQueue();
24
25     // Do not allow less items than the minimum size.
26     if ($min_size && $number_of_items < $min_size) {
27       $this->context->buildViolation($constraint->messageMinSize, ['%min_size' => $min_size])
28         ->addViolation();
29     }
30     // Do not allow more items than the maximum size if the queue is not
31     // configured to act a simple list.
32     elseif (!$act_as_queue && $max_size && $number_of_items > $max_size) {
33       $this->context->buildViolation($constraint->messageMaxSize, ['%max_size' => $max_size])
34         ->addViolation();
35     }
36   }
37
38 }