From 7ff57baa76f2ff93d0493208c98033be3b0cd275 Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Tue, 5 Nov 2024 11:24:38 +0100 Subject: [PATCH] Move Winzou State Machine config to CompilerPass to enable/disable if a particular graph is available --- config/config/winzou_state_machine.php | 32 -------- src/CommerceWeaversSyliusTpayPlugin.php | 2 + ...AddWinzouStateMachineConfigurationPass.php | 80 +++++++++++++++++++ .../Application/config/packages/_sylius.yaml | 1 - 4 files changed, 82 insertions(+), 33 deletions(-) delete mode 100644 config/config/winzou_state_machine.php create mode 100644 src/DependencyInjection/CompilerPass/AddWinzouStateMachineConfigurationPass.php diff --git a/config/config/winzou_state_machine.php b/config/config/winzou_state_machine.php deleted file mode 100644 index 3cf90ee6..00000000 --- a/config/config/winzou_state_machine.php +++ /dev/null @@ -1,32 +0,0 @@ -extension('winzou_state_machine', [ - 'sylius_payment' => [ - 'callbacks' => [ - 'before' => [ - 'tpay_refund_payment' => [ - 'on' => ['refund'], - 'do' => ['@commerce_weavers_sylius_tpay.refunding.dispatcher.refund', 'dispatch'], - 'args' => ['object'], - ], - ], - ], - ], - 'sylius_refund_refund_payment' => [ - 'callbacks' => [ - 'before' => [ - 'tpay_refund_payment' => [ - 'on' => ['complete'], - 'do' => ['@commerce_weavers_sylius_tpay.refunding.dispatcher.refund', 'dispatch'], - 'args' => ['object'], - ], - ] - ] - ], - ]); -}; diff --git a/src/CommerceWeaversSyliusTpayPlugin.php b/src/CommerceWeaversSyliusTpayPlugin.php index 230fc5a8..3b553db7 100644 --- a/src/CommerceWeaversSyliusTpayPlugin.php +++ b/src/CommerceWeaversSyliusTpayPlugin.php @@ -5,6 +5,7 @@ namespace CommerceWeavers\SyliusTpayPlugin; use CommerceWeavers\SyliusTpayPlugin\DependencyInjection\CompilerPass\AddSupportedRefundPaymentMethodPass; +use CommerceWeavers\SyliusTpayPlugin\DependencyInjection\CompilerPass\AddWinzouStateMachineConfigurationPass; use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -16,6 +17,7 @@ final class CommerceWeaversSyliusTpayPlugin extends Bundle public function build(ContainerBuilder $container): void { $container->addCompilerPass(new AddSupportedRefundPaymentMethodPass()); + $container->addCompilerPass(new AddWinzouStateMachineConfigurationPass()); } public function getPath(): string diff --git a/src/DependencyInjection/CompilerPass/AddWinzouStateMachineConfigurationPass.php b/src/DependencyInjection/CompilerPass/AddWinzouStateMachineConfigurationPass.php new file mode 100644 index 00000000..832522f9 --- /dev/null +++ b/src/DependencyInjection/CompilerPass/AddWinzouStateMachineConfigurationPass.php @@ -0,0 +1,80 @@ +resolveEnvPlaceholders( + $container->getParameter('sylius_abstraction.state_machine.default_adapter'), + true, + ); + /** @var array $stateMachineAdapterMapping */ + $stateMachineAdapterMapping = $container->resolveEnvPlaceholders( + $container->getParameter('sylius_abstraction.state_machine.graphs_to_adapters_mapping'), + true, + ); + } catch (ParameterNotFoundException) { + return; + } + + if ($this->hasWinzouStateMachineGraph('sylius_payment', $stateMachineDefaultAdapter, $stateMachineAdapterMapping)) { + $container->prependExtensionConfig('winzou_state_machine', [ + 'sylius_payment' => [ + 'callbacks' => [ + 'before' => [ + 'tpay_refund_payment' => [ + 'on' => ['refund'], + 'do' => ['@commerce_weavers_sylius_tpay.refunding.dispatcher.refund', 'dispatch'], + 'args' => ['object'], + ], + ], + ], + ], + ]); + } + + if ($this->hasWinzouStateMachineGraph('sylius_refund_refund_payment', $stateMachineDefaultAdapter, $stateMachineAdapterMapping)) { + $container->prependExtensionConfig('winzou_state_machine', [ + 'sylius_refund_refund_payment' => [ + 'callbacks' => [ + 'before' => [ + 'tpay_refund_payment' => [ + 'on' => ['complete'], + 'do' => ['@commerce_weavers_sylius_tpay.refunding.dispatcher.refund', 'dispatch'], + 'args' => ['object'], + ], + ], + ], + ], + ]); + } + } + + /** + * @param array $stateMachineAdapterMapping + */ + private function hasWinzouStateMachineGraph( + string $graphName, + string $stateMachineDefaultAdapter, + array $stateMachineAdapterMapping, + ): bool { + return ( + isset($stateMachineAdapterMapping[$graphName]) && + $stateMachineAdapterMapping[$graphName] === 'winzou_state_machine' + ) || + ( + !isset($stateMachineAdapterMapping[$graphName]) && + $stateMachineDefaultAdapter === 'winzou_state_machine' + ); + } +} diff --git a/tests/Application/config/packages/_sylius.yaml b/tests/Application/config/packages/_sylius.yaml index 655cf95b..92690bb2 100644 --- a/tests/Application/config/packages/_sylius.yaml +++ b/tests/Application/config/packages/_sylius.yaml @@ -8,7 +8,6 @@ imports: - { resource: "@SyliusApiBundle/Resources/config/app/config.yaml" } parameters: - env(STATE_MACHINE_DEFAULT_ADAPTER): 'winzou_state_machine' sylius_core.public_dir: '%kernel.project_dir%/public' sylius_shop: