You are here

class CouponValidConstraintValidator in Commerce Cart API 8

Hierarchy

Expanded class hierarchy of CouponValidConstraintValidator

File

src/Plugin/Validation/Constraint/CouponValidConstraintValidator.php, line 12

Namespace

Drupal\commerce_cart_api\Plugin\Validation\Constraint
View source
class CouponValidConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\Core\TypedData\Exception\MissingDataException
   */
  public function validate($value, Constraint $constraint) {
    assert($value instanceof EntityReferenceFieldItemListInterface);
    $order = $value
      ->getEntity();
    assert($order instanceof OrderInterface);

    // Only draft orders should be processed.
    if ($order
      ->getState()
      ->getId() !== 'draft') {
      return;
    }
    $coupons = $value
      ->referencedEntities();
    foreach ($coupons as $delta => $coupon) {
      assert($coupon instanceof CouponInterface);
      if (!$coupon
        ->available($order) || !$coupon
        ->getPromotion()
        ->applies($order)) {
        $this->context
          ->buildViolation($constraint->message, [
          '%code' => $coupon
            ->getCode(),
        ])
          ->atPath((string) $delta)
          ->addViolation();
      }
    }
  }

}

Members