-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pagolight payment method unique constraint
- Loading branch information
Showing
10 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Webgriffe\SyliusPagolightPlugin\Validator\PagolightPaymentMethodUniqueValidator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator) { | ||
$services = $containerConfigurator->services(); | ||
|
||
$services->set('webgriffe_sylius_pagolight.validator.pagolight_payment_method_unique', PagolightPaymentMethodUniqueValidator::class) | ||
->args([ | ||
service('sylius.repository.payment_method'), | ||
]) | ||
->tag('validator.constraint_validator') | ||
; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping | ||
https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
<class name="Sylius\Component\Core\Model\PaymentMethod"> | ||
<constraint name="Webgriffe\SyliusPagolightPlugin\Validator\PagolightPaymentMethodUnique"> | ||
<option name="groups">sylius</option> | ||
</constraint> | ||
</class> | ||
</constraint-mapping> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusPagolightPlugin\Validator; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
*/ | ||
final class PagolightPaymentMethodUnique extends Constraint | ||
{ | ||
public string $message = 'webgriffe_sylius_pagolight.payment_method.unique'; | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusPagolightPlugin\Validator; | ||
|
||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedValueException; | ||
use Webgriffe\SyliusPagolightPlugin\Payum\PagolightApi; | ||
|
||
/** | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
*/ | ||
final class PagolightPaymentMethodUniqueValidator extends ConstraintValidator | ||
{ | ||
public function __construct( | ||
private readonly PaymentMethodRepositoryInterface $paymentMethodRepository, | ||
) { | ||
} | ||
|
||
/** | ||
* @param mixed|PaymentMethodInterface $value | ||
*/ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$value instanceof PaymentMethodInterface) { | ||
throw new UnexpectedValueException($value, PaymentMethodInterface::class); | ||
} | ||
|
||
if (!$constraint instanceof PagolightPaymentMethodUnique) { | ||
throw new UnexpectedValueException($constraint, PagolightPaymentMethodUnique::class); | ||
} | ||
|
||
$gatewayConfig = $value->getGatewayConfig(); | ||
if ($gatewayConfig === null || | ||
!in_array($gatewayConfig->getFactoryName(), [ | ||
PagolightApi::PAGOLIGHT_GATEWAY_CODE, | ||
PagolightApi::PAGOLIGHT_PRO_GATEWAY_CODE, | ||
], true) | ||
) { | ||
return; | ||
} | ||
|
||
$paymentMethods = $this->paymentMethodRepository->findAll(); | ||
$paymentMethodsWithSameGatewayConfig = array_filter( | ||
$paymentMethods, | ||
static fn (PaymentMethodInterface $paymentMethod) => $paymentMethod->getGatewayConfig()?->getFactoryName() === $gatewayConfig->getFactoryName() | ||
); | ||
if (count($paymentMethodsWithSameGatewayConfig) > 1 || | ||
(count($paymentMethodsWithSameGatewayConfig) === 1 && reset($paymentMethodsWithSameGatewayConfig) !== $value) | ||
) { | ||
$this->context | ||
->buildViolation($constraint->message) | ||
->atPath('gatewayConfig') | ||
->addViolation(); | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
webgriffe_sylius_pagolight: | ||
payment_method: | ||
unique: 'There should be only one payment method for this gateway.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
webgriffe_sylius_pagolight: | ||
payment_method: | ||
unique: 'Ci deve essere un solo metodo di pagamento per questo gateway.' |