class CouponValidConstraintValidator in Commerce Cart API 8
Hierarchy
- class \Drupal\commerce_cart_api\Plugin\Validation\Constraint\CouponValidConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator
Expanded class hierarchy of CouponValidConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ CouponValidConstraintValidator.php, line 12
Namespace
Drupal\commerce_cart_api\Plugin\Validation\ConstraintView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CouponValidConstraintValidator:: |
public | function |