Skip to content

Commit

Permalink
install winzou conditionally
Browse files Browse the repository at this point in the history
  • Loading branch information
mpysiak committed Nov 26, 2024
1 parent 47f7773 commit ac816b1
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 125 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ jobs:
if: matrix.wkhtmltopdf == false
run: |
mv tests/Application/etc/sylius_refund_pdf_generation_disabled.yaml tests/Application/config/packages
sed -i 's/pdf_generation_disabled/pdf_generation_enabled/g' behat.yml.dist
-
name: Output PHP version for Symfony CLI
Expand Down Expand Up @@ -180,6 +179,13 @@ jobs:
if: matrix.sylius != ''
run: composer require "sylius/sylius:${{ matrix.sylius }}" --no-update --no-scripts --no-interaction

-
name: Require Winzou State Machine
if: "${{ matrix.state_machine_adapter == 'winzou_state_machine' }}"
run: |
composer require winzou/state-machine:^0.4 --no-update
composer require winzou/state-machine-bundle:^0.6 --no-update
-
name: Install PHP dependencies
run: composer update --no-interaction
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
"sylius/resource-bundle": "^1.12",
"sylius/sylius": "^2.0",
"symfony/messenger": "^6.4 || ^7.1",
"php-http/discovery": "^1.20",
"winzou/state-machine": "^0.4",
"winzou/state-machine-bundle": "^0.6"
"php-http/discovery": "^1.20"
},
"require-dev": {
"behat/behat": "^3.6.1",
Expand Down Expand Up @@ -91,7 +89,7 @@
},
"extra": {
"branch-alias": {
"dev-1.6": "1.6-dev"
"dev-2.0": "2.0-dev"
},
"symfony": {
"require": "^7.1"
Expand Down
30 changes: 16 additions & 14 deletions src/Resources/config/app/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@ sylius_mailer:
subject: Units refunded
template: "@SyliusRefundPlugin/Email/unitsRefunded.html.twig"

winzou_state_machine:
sylius_refund_refund_payment:
class: Sylius\RefundPlugin\Entity\RefundPayment
property_path: state
graph: sylius_refund_refund_payment
state_machine_class: "%sylius.state_machine.class%"
states:
new: ~
completed: ~
transitions:
complete:
from: [new]
to: completed

framework:
messenger:
buses:
Expand All @@ -44,6 +30,22 @@ framework:
default_middleware: allow_no_handlers
sylius_refund_plugin.command_bus: *command_bus
sylius_refund_plugin.event_bus: *event_bus
workflows:
sylius_refund_refund_payment:
type: state_machine
marking_store:
type: method
property: state
supports:
- Sylius\RefundPlugin\Entity\RefundPayment
initial_marking: new
places:
- !php/const Sylius\RefundPlugin\Entity\RefundPaymentInterface::STATE_NEW
- !php/const Sylius\RefundPlugin\Entity\RefundPaymentInterface::STATE_COMPLETED
transitions:
complete:
from: !php/const Sylius\RefundPlugin\Entity\RefundPaymentInterface::STATE_NEW
to: !php/const Sylius\RefundPlugin\Entity\RefundPaymentInterface::STATE_COMPLETED

sylius_grid:
grids:
Expand Down
5 changes: 3 additions & 2 deletions src/Resources/config/app/integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use winzou\Bundle\StateMachineBundle\winzouStateMachineBundle;

return static function (ContainerConfigurator $configurator, ContainerBuilder $container): void {
if (class_exists('\Symfony\Component\Workflow\Workflow')) {
$configurator->import('../integrations/workflow.yaml');
if (class_exists(winzouStateMachineBundle::class)) {
$configurator->import('../integrations/winzou_state_machine.yaml');
}
};
13 changes: 13 additions & 0 deletions src/Resources/config/integrations/winzou_state_machine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
winzou_state_machine:
sylius_refund_refund_payment:
class: Sylius\RefundPlugin\Entity\RefundPayment
property_path: state
graph: sylius_refund_refund_payment
state_machine_class: "%sylius.state_machine.class%"
states:
new: ~
completed: ~
transitions:
complete:
from: [new]
to: completed
17 changes: 0 additions & 17 deletions src/Resources/config/integrations/workflow.yaml

This file was deleted.

34 changes: 6 additions & 28 deletions src/StateResolver/OrderFullyRefundedStateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,23 @@
namespace Sylius\RefundPlugin\StateResolver;

use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Abstraction\StateMachine\WinzouStateMachineAdapter;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderPaymentTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Checker\OrderFullyRefundedTotalCheckerInterface;
use Webmozart\Assert\Assert;

final class OrderFullyRefundedStateResolver implements OrderFullyRefundedStateResolverInterface
final readonly class OrderFullyRefundedStateResolver implements OrderFullyRefundedStateResolverInterface
{
/** @param OrderRepositoryInterface<OrderInterface> $orderRepository */
public function __construct(
private readonly FactoryInterface|StateMachineInterface $stateMachineFactory,
private readonly ObjectManager $orderManager,
private readonly OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
private readonly OrderRepositoryInterface $orderRepository,
private StateMachineInterface $stateMachineFactory,
private ObjectManager $orderManager,
private OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
private OrderRepositoryInterface $orderRepository,
) {
if ($this->stateMachineFactory instanceof FactoryInterface) {
trigger_deprecation(
'sylius/refund-plugin',
'1.6',
sprintf(
'Passing an instance of "%s" as the first argument is deprecated. It will accept only instances of "%s" in RefundPlugin 2.0.',
FactoryInterface::class,
StateMachineInterface::class,
),
);
}
}

public function resolve(string $orderNumber): void
Expand All @@ -60,19 +47,10 @@ public function resolve(string $orderNumber): void
}

$this
->getStateMachine()
->stateMachineFactory
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_REFUND)
;

$this->orderManager->flush();
}

private function getStateMachine(): StateMachineInterface
{
if ($this->stateMachineFactory instanceof FactoryInterface) {
return new WinzouStateMachineAdapter($this->stateMachineFactory);
}

return $this->stateMachineFactory;
}
}
32 changes: 5 additions & 27 deletions src/StateResolver/OrderPartiallyRefundedStateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,21 @@
namespace Sylius\RefundPlugin\StateResolver;

use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Abstraction\StateMachine\WinzouStateMachineAdapter;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderPaymentTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\RefundPlugin\Exception\OrderNotFound;

final class OrderPartiallyRefundedStateResolver implements OrderPartiallyRefundedStateResolverInterface
final readonly class OrderPartiallyRefundedStateResolver implements OrderPartiallyRefundedStateResolverInterface
{
/** @param OrderRepositoryInterface<OrderInterface> $orderRepository */
public function __construct(
private readonly OrderRepositoryInterface $orderRepository,
private readonly FactoryInterface|StateMachineInterface $stateMachineFactory,
private readonly ObjectManager $orderManager,
private OrderRepositoryInterface $orderRepository,
private StateMachineInterface $stateMachineFactory,
private ObjectManager $orderManager,
) {
if ($this->stateMachineFactory instanceof FactoryInterface) {
trigger_deprecation(
'sylius/refund-plugin',
'1.6',
sprintf(
'Passing an instance of "%s" as the second argument is deprecated. It will accept only instances of "%s" in RefundPlugin 2.0.',
FactoryInterface::class,
StateMachineInterface::class,
),
);
}
}

public function resolve(string $orderNumber): void
Expand All @@ -57,19 +44,10 @@ public function resolve(string $orderNumber): void
}

$this
->getStateMachine()
->stateMachineFactory
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND)
;

$this->orderManager->flush();
}

private function getStateMachine(): StateMachineInterface
{
if ($this->stateMachineFactory instanceof FactoryInterface) {
return new WinzouStateMachineAdapter($this->stateMachineFactory);
}

return $this->stateMachineFactory;
}
}
30 changes: 4 additions & 26 deletions src/StateResolver/RefundPaymentCompletedStateApplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,24 @@
namespace Sylius\RefundPlugin\StateResolver;

use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Abstraction\StateMachine\WinzouStateMachineAdapter;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;

final class RefundPaymentCompletedStateApplier implements RefundPaymentCompletedStateApplierInterface
final readonly class RefundPaymentCompletedStateApplier implements RefundPaymentCompletedStateApplierInterface
{
public function __construct(
private readonly FactoryInterface|StateMachineInterface $stateMachineFactory,
private readonly ObjectManager $refundPaymentManager,
private StateMachineInterface $stateMachineFactory,
private ObjectManager $refundPaymentManager,
) {
if ($this->stateMachineFactory instanceof FactoryInterface) {
trigger_deprecation(
'sylius/refund-plugin',
'1.6',
sprintf(
'Passing an instance of "%s" as the first argument is deprecated. It will accept only instances of "%s" in RefundPlugin 2.0.',
FactoryInterface::class,
StateMachineInterface::class,
),
);
}
}

public function apply(RefundPaymentInterface $refundPayment): void
{
$this
->getStateMachine()
->stateMachineFactory
->apply($refundPayment, RefundPaymentTransitions::GRAPH, RefundPaymentTransitions::TRANSITION_COMPLETE)
;

$this->refundPaymentManager->flush();
}

private function getStateMachine(): StateMachineInterface
{
if ($this->stateMachineFactory instanceof FactoryInterface) {
return new WinzouStateMachineAdapter($this->stateMachineFactory);
}

return $this->stateMachineFactory;
}
}
5 changes: 5 additions & 0 deletions tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use winzou\Bundle\StateMachineBundle\winzouStateMachineBundle;

final class Kernel extends BaseKernel
{
Expand All @@ -29,6 +30,10 @@ public function registerBundles(): iterable
foreach ($this->getBundleListFiles() as $file) {
yield from $this->registerBundlesFromFile($file);
}

if (class_exists(winzouStateMachineBundl::class)) {
yield new winzouStateMachineBundle();
}
}

protected function getContainerBaseClass(): string
Expand Down
1 change: 0 additions & 1 deletion tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@
Symfony\UX\Autocomplete\AutocompleteBundle::class => ['all' => true],
Symfony\UX\Icons\UXIconsBundle::class => ['all' => true],
Sylius\TwigExtra\Symfony\SyliusTwigExtraBundle::class => ['all' => true],
winzou\Bundle\StateMachineBundle\winzouStateMachineBundle::class => ['all' => true],
];
8 changes: 4 additions & 4 deletions tests/Behat/Context/Setup/PaymentContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Tests\Sylius\RefundPlugin\Behat\Context\Setup;

use Behat\Behat\Context\Context;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
Expand All @@ -24,7 +24,7 @@
final class PaymentContext implements Context
{
public function __construct(
private readonly StateMachineFactoryInterface $stateMachineFactory,
private readonly StateMachineInterface $stateMachineFactory,
private readonly SharedStorageInterface $sharedStorage
) {
}
Expand All @@ -35,7 +35,7 @@ public function __construct(
public function paymentOfOrderFailed(OrderInterface $order): void
{
$payment = $order->getLastPayment();
$this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH)->apply(PaymentTransitions::TRANSITION_FAIL);
$this->stateMachineFactory->apply($payment, PaymentTransitions::GRAPH, PaymentTransitions::TRANSITION_FAIL);
}

/**
Expand All @@ -57,6 +57,6 @@ public function theCustomerChosePaymentMethod(PaymentMethodInterface $paymentMet
*/
public function andThisPaymentHasBeenPaid(PaymentInterface $payment): void
{
$this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH)->apply(PaymentTransitions::TRANSITION_COMPLETE);
$this->stateMachineFactory->apply($payment, PaymentTransitions::GRAPH, PaymentTransitions::TRANSITION_COMPLETE);
}
}
2 changes: 1 addition & 1 deletion tests/Behat/Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
</service>

<service id="Tests\Sylius\RefundPlugin\Behat\Context\Setup\PaymentContext">
<argument type="service" id="SM\Factory\Factory" />
<argument type="service" id="sylius_abstraction.state_machine" />
<argument type="service" id="sylius.behat.shared_storage" />
</service>

Expand Down

0 comments on commit ac816b1

Please sign in to comment.