Skip to content

Commit

Permalink
Move Winzou State Machine config to CompilerPass to enable/disable if…
Browse files Browse the repository at this point in the history
… a particular graph is available
  • Loading branch information
coldic3 authored and lchrusciel committed Nov 5, 2024
1 parent 89430d1 commit 7ff57ba
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
32 changes: 0 additions & 32 deletions config/config/winzou_state_machine.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/CommerceWeaversSyliusTpayPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;

final class AddWinzouStateMachineConfigurationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
try {
/** @var string $stateMachineDefaultAdapter */
$stateMachineDefaultAdapter = $container->resolveEnvPlaceholders(
$container->getParameter('sylius_abstraction.state_machine.default_adapter'),
true,
);
/** @var array<string, string> $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<string, string> $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'
);
}
}
1 change: 0 additions & 1 deletion tests/Application/config/packages/_sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7ff57ba

Please sign in to comment.