diff --git a/config/services/form.php b/config/services/form.php index 6b4e375a..83c9dbe7 100644 --- a/config/services/form.php +++ b/config/services/form.php @@ -6,6 +6,7 @@ use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\CardTypeDataTransformer; use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyClientSecretListener; +use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\RemoveUnnecessaryPaymentDetailsFieldsListener; use CommerceWeavers\SyliusTpayPlugin\Form\Extension\CompleteTypeExtension; use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayCardType; use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayGatewayConfigurationType; @@ -15,15 +16,13 @@ return function(ContainerConfigurator $container): void { $services = $container->services(); - $services->set('commerce_weavers_tpay.form.data_transformer.card_type', CardTypeDataTransformer::class); - $services->set(CompleteTypeExtension::class) ->tag('form.type_extension') ; $services->set(TpayGatewayConfigurationType::class) ->args([ - service(PreventSavingEmptyClientSecretListener::class), + service('commerce_weavers_tpay.form.event_listener.prevent_saving_empty_client_secret'), ]) ->tag( 'sylius.gateway_configuration_type', @@ -39,7 +38,16 @@ ->tag('form.type') ; - $services->set(TpayPaymentDetailsType::class)->tag('form.type'); + $services->set(TpayPaymentDetailsType::class) + ->args([ + service('commerce_weavers_tpay.form.event_listener.remove_unnecessary_payment_details_fields'), + ]) + ->tag('form.type') + ; + + $services->set('commerce_weavers_tpay.form.data_transformer.card_type', CardTypeDataTransformer::class); + + $services->set('commerce_weavers_tpay.form.event_listener.prevent_saving_empty_client_secret', PreventSavingEmptyClientSecretListener::class); - $services->set(PreventSavingEmptyClientSecretListener::class); + $services->set('commerce_weavers_tpay.form.event_listener.remove_unnecessary_payment_details_fields', RemoveUnnecessaryPaymentDetailsFieldsListener::class); }; diff --git a/phpstan.neon b/phpstan.neon index 07368faa..aff7be05 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -17,3 +17,4 @@ parameters: - '/Call to method getSubject\(\) on an unknown class Symfony\\Component\\Workflow\\Event\\TransitionEvent\./' - '#Class CommerceWeavers\\SyliusTpayPlugin\\Form\\DataTransformer\\CardTypeDataTransformer implements generic interface Symfony\\Component\\Form\\DataTransformerInterface but does not specify its types: TValue, TTransformedValue#' - '#Method CommerceWeavers\\SyliusTpayPlugin\\Form\\Type\\TpayCardType::__construct\(\) has parameter \$cardTypeDataTransformer with generic interface Symfony\\Component\\Form\\DataTransformerInterface but does not specify its types: TValue, TTransformedValue#' + - '#Parameter \#2 \$listener of method .+::addEventListener\(\) expects callable\(\): mixed, array.+ given\.#' diff --git a/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php b/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php new file mode 100644 index 00000000..a4416d20 --- /dev/null +++ b/src/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListener.php @@ -0,0 +1,25 @@ +getData() ?? []; + $form = $event->getForm(); + + if (!isset($data['card'])) { + $form->remove('card'); + } + + if (!isset($data['blik_token'])) { + $form->remove('blik_token'); + } + } +} diff --git a/src/Form/Type/TpayPaymentDetailsType.php b/src/Form/Type/TpayPaymentDetailsType.php index 0d648297..b1ce76f7 100644 --- a/src/Form/Type/TpayPaymentDetailsType.php +++ b/src/Form/Type/TpayPaymentDetailsType.php @@ -7,11 +7,15 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; final class TpayPaymentDetailsType extends AbstractType { + public function __construct( + private object $removeUnnecessaryPaymentDetailsFieldsListener, + ) { + } + /** * @param array $options */ @@ -35,19 +39,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ) ; - /** TODO: Extract into a separate class */ - $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { - /** @var array{card?: string, blik_token?: string} $data */ - $data = $event->getData() ?? []; - $form = $event->getForm(); - - if (!isset($data['card'])) { - $form->remove('card'); - } - - if (!isset($data['blik_token'])) { - $form->remove('blik_token'); - } - }); + $builder->addEventListener( + FormEvents::PRE_SUBMIT, + [$this->removeUnnecessaryPaymentDetailsFieldsListener, '__invoke'], + ); } } diff --git a/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php b/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php new file mode 100644 index 00000000..3b8c68c3 --- /dev/null +++ b/tests/Unit/Form/EventListener/RemoveUnnecessaryPaymentDetailsFieldsListenerTest.php @@ -0,0 +1,54 @@ +prophesize(FormInterface::class); + $form->remove('card')->shouldBeCalled()->willReturn($form); + $form->remove('blik_token')->shouldNotBeCalled(); + + $event = new FormEvent($form->reveal(), ['blik_token' => '123456']); + + $this->createTestSubject()->__invoke($event); + } + + public function test_it_removes_blik_token_field_once_blik_token_is_not_set(): void + { + $form = $this->prophesize(FormInterface::class); + $form->remove('card')->shouldNotBeCalled(); + $form->remove('blik_token')->shouldBeCalled()->willReturn($form); + + $event = new FormEvent($form->reveal(), ['card' => 'h45h']); + + $this->createTestSubject()->__invoke($event); + } + + public function test_it_removes_both_card_and_blik_token_if_none_of_them_is_passed(): void + { + $form = $this->prophesize(FormInterface::class); + $form->remove('card')->shouldBeCalled()->willReturn($form); + $form->remove('blik_token')->shouldBeCalled()->willReturn($form); + + $event = new FormEvent($form->reveal(), []); + + $this->createTestSubject()->__invoke($event); + } + + public function createTestSubject(): object + { + return new RemoveUnnecessaryPaymentDetailsFieldsListener(); + } +}