coupon(); if ( null === $coupon ) { return $running; } $result = $this->validator->validate( $coupon, $ctx, $running ); if ( $result->is_err() ) { throw new DomainException( htmlspecialchars( $result->error_code(), ENT_QUOTES, 'UTF-8' ), htmlspecialchars( $result->error_message(), ENT_QUOTES, 'UTF-8' ) ); } $discount = $this->validator->discount_amount( $coupon, $running ); return $running->subtract( $discount ); } /** * Discount amount for a validated coupon (or zero). * * @param PriceContext $ctx Context. * @param Money $running Pre-discount total. * * @throws DomainException When a coupon is present but invalid. * * @since 0.1.0 */ public function discount_amount( PriceContext $ctx, Money $running ): Money { $coupon = $ctx->coupon(); if ( null === $coupon ) { return Money::zero( $running->currency() ); } $result = $this->validator->validate( $coupon, $ctx, $running ); if ( $result->is_err() ) { throw new DomainException( htmlspecialchars( $result->error_code(), ENT_QUOTES, 'UTF-8' ), htmlspecialchars( $result->error_message(), ENT_QUOTES, 'UTF-8' ) ); } return $this->validator->discount_amount( $coupon, $running ); } }