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: