Skip to content

Commit

Permalink
Move Winzou State Machine config to the CompilerPass (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel authored Nov 5, 2024
2 parents 0d37d31 + 506f043 commit 7ab779b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"firebase/php-jwt": "^6.10",
"payum/core": "^1.7",
"sylius/core-bundle": "^1.12",
"sylius/refund-plugin": "^1.5",
"sylius/resource-bundle": "^1.10",
"symfony/config": "5.4.* || ^6.0",
"symfony/dependency-injection": "5.4.* || ^6.0",
Expand Down Expand Up @@ -51,6 +50,7 @@
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
"shipmonk/composer-dependency-analyser": "^1.7",
"sylius-labs/coding-standard": "^4.2",
"sylius/refund-plugin": "^1.5",
"sylius/sylius": "^1.12",
"symfony/browser-kit": "^5.4 || ^6.0",
"symfony/debug-bundle": "^5.4 || ^6.0",
Expand Down
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 7ab779b

Please sign in to comment.