From c7b4ab0feebeb860de86ec4b514fa5fc8c9bb4ef Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Wed, 23 Oct 2024 14:31:50 +0200 Subject: [PATCH] Validate the charge types support in a checkout with any given gateway --- src/Entity/GatewayCheckout.php | 2 + src/Validator/SupportedChargeTypes.php | 14 ++++++ .../SupportedChargeTypesValidator.php | 44 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/Validator/SupportedChargeTypes.php create mode 100644 src/Validator/SupportedChargeTypesValidator.php diff --git a/src/Entity/GatewayCheckout.php b/src/Entity/GatewayCheckout.php index 8ca05d4..b861cd0 100644 --- a/src/Entity/GatewayCheckout.php +++ b/src/Entity/GatewayCheckout.php @@ -9,6 +9,7 @@ use App\Entity\Trait\TimestampableUpdationEntity; use App\Repository\GatewayCheckoutRepository; use App\State\GatewayCheckoutProcessor; use App\Validator\GatewayName; +use App\Validator\SupportedChargeTypes; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; @@ -63,6 +64,7 @@ class GatewayCheckout #[API\ApiProperty(readableLink: true, writableLink: true)] #[Assert\NotBlank()] #[Assert\Count(min: 1)] + #[SupportedChargeTypes()] #[ORM\OneToMany(mappedBy: 'checkout', targetEntity: GatewayCharge::class, cascade: ['persist'])] private Collection $charges; diff --git a/src/Validator/SupportedChargeTypes.php b/src/Validator/SupportedChargeTypes.php new file mode 100644 index 0000000..b803659 --- /dev/null +++ b/src/Validator/SupportedChargeTypes.php @@ -0,0 +1,14 @@ + */ + $charges = $value; + + $checkout = $charges->toArray()[0]->getCheckout(); + $gateway = $this->gatewayLocator->getGatewayOf($checkout); + + foreach ($charges as $charge) { + if (!\in_array( + $charge->getType(), + $gateway->getSupportedChargeTypes() + )) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $charge->getType()->value) + ->setParameter('{{ gateway }}', $gateway->getName()) + ->addViolation(); + } + } + } +}