Skip to content

Commit

Permalink
Support Symfony Workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Sep 26, 2024
1 parent 308ab37 commit 3d5e397
Show file tree
Hide file tree
Showing 25 changed files with 261 additions and 133 deletions.
35 changes: 23 additions & 12 deletions spec/Extension/UpdatePaymentStateExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\FluxSE\SyliusPayumStripePlugin\Extension;

use Exception;
use FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine\StateMachineInterface;
use Payum\Core\Extension\Context;
use Payum\Core\Extension\ExtensionInterface;
use Payum\Core\GatewayInterface;
Expand All @@ -15,20 +16,18 @@
use Payum\Core\Storage\IdentityInterface;
use Payum\Core\Storage\StorageInterface;
use PhpSpec\ObjectBehavior;
use SM\Factory\FactoryInterface;
use Sylius\Bundle\PayumBundle\Factory\GetStatusFactoryInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Resource\StateMachine\StateMachineInterface;

final class UpdatePaymentStateExtensionSpec extends ObjectBehavior
{
public function let(
FactoryInterface $factory,
StateMachineInterface $stateMachine,
StorageInterface $storage,
GetStatusFactoryInterface $getStatusRequestFactory
): void {
$this->beConstructedWith($factory, $storage, $getStatusRequestFactory);
$this->beConstructedWith($stateMachine, $storage, $getStatusRequestFactory);
}

public function it_is_initializable(): void
Expand Down Expand Up @@ -96,7 +95,6 @@ public function it_OnPostExecute_apply_a_transition(
GetStatusInterface $status,
GetStatusFactoryInterface $getStatusRequestFactory,
GatewayInterface $gateway,
FactoryInterface $factory,
StateMachineInterface $stateMachine
): void {
$context->getException()->willReturn(null);
Expand All @@ -114,9 +112,16 @@ public function it_OnPostExecute_apply_a_transition(
$payment->getState()->willReturn(PaymentInterface::STATE_NEW);
$status->getValue()->willReturn(PaymentInterface::STATE_COMPLETED);

$factory->get($payment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->getTransitionToState(PaymentInterface::STATE_COMPLETED)->willReturn('complete');
$stateMachine->apply('complete')->shouldBeCalled();
$stateMachine->getTransitionToState(
$payment,
PaymentTransitions::GRAPH,
PaymentInterface::STATE_COMPLETED
)->willReturn('complete');
$stateMachine->apply(
$payment,
PaymentTransitions::GRAPH,
'complete'
)->shouldBeCalled();

$this->onPostExecute($context);
}
Expand All @@ -131,7 +136,6 @@ public function it_OnPostExecute_apply_a_transition_without_a_Sylius_PaymentInte
GetStatusInterface $status,
GetStatusFactoryInterface $getStatusRequestFactory,
GatewayInterface $gateway,
FactoryInterface $factory,
StateMachineInterface $stateMachine
): void {
$context->getException()->willReturn(null);
Expand All @@ -152,9 +156,16 @@ public function it_OnPostExecute_apply_a_transition_without_a_Sylius_PaymentInte
$previousPayment->getState()->willReturn(PaymentInterface::STATE_NEW);
$status->getValue()->willReturn(PaymentInterface::STATE_COMPLETED);

$factory->get($previousPayment, PaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->getTransitionToState(PaymentInterface::STATE_COMPLETED)->willReturn('complete');
$stateMachine->apply('complete')->shouldBeCalled();
$stateMachine->getTransitionToState(
$previousPayment,
PaymentTransitions::GRAPH,
PaymentInterface::STATE_COMPLETED
)->willReturn('complete');
$stateMachine->apply(
$previousPayment,
PaymentTransitions::GRAPH,
'complete'
)->shouldBeCalled();

$this->onPreExecute($previousContext);

Expand Down
8 changes: 4 additions & 4 deletions spec/StateMachine/CancelOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_is_invokable_when_it_is_new(
$command = new CancelPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment);
}

public function it_is_invokable_when_is_authorized(
Expand All @@ -45,7 +45,7 @@ public function it_is_invokable_when_is_authorized(
$command = new CancelPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment);
}

public function it_do_nothing_when_it_is_completed(
Expand All @@ -54,7 +54,7 @@ public function it_do_nothing_when_it_is_completed(
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$this->__invoke($payment, $event);
$this->__invoke($payment);
}

public function it_do_nothing_when_it_is_refunded(
Expand All @@ -63,6 +63,6 @@ public function it_do_nothing_when_it_is_refunded(
): void {
$event->getState()->willReturn(PaymentInterface::STATE_REFUNDED);

$this->__invoke($payment, $event);
$this->__invoke($payment);
}
}
6 changes: 3 additions & 3 deletions spec/StateMachine/CaptureAuthorizedOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_is_invokable(
$command = new CaptureAuthorizedPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment);
}

public function it_do_nothing_when_it_is_completed(
Expand All @@ -38,7 +38,7 @@ public function it_do_nothing_when_it_is_completed(
): void {
$event->getState()->willReturn(PaymentInterface::STATE_COMPLETED);

$this->__invoke($payment, $event);
$this->__invoke($payment);
}

public function it_do_nothing_when_it_is_refunded(
Expand All @@ -47,6 +47,6 @@ public function it_do_nothing_when_it_is_refunded(
): void {
$event->getState()->willReturn(PaymentInterface::STATE_REFUNDED);

$this->__invoke($payment, $event);
$this->__invoke($payment);
}
}
4 changes: 2 additions & 2 deletions spec/StateMachine/RefundOrderProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_is_invokable(
$command = new RefundPayment(1);
$commandBus->dispatch($command)->willReturn(new Envelope($command));

$this->__invoke($payment, $event);
$this->__invoke($payment);
}

public function it_do_nothing_when_it_is_authorized(
Expand All @@ -38,6 +38,6 @@ public function it_do_nothing_when_it_is_authorized(
): void {
$event->getState()->willReturn(PaymentInterface::STATE_AUTHORIZED);

$this->__invoke($payment, $event);
$this->__invoke($payment);
}
}
26 changes: 26 additions & 0 deletions src/Abstraction/StateMachine/CompositeStateMachine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine;

use Sylius\Abstraction\StateMachine\StateMachineInterface as SyliusAbstractionStateMachineInterface;

final class CompositeStateMachine implements StateMachineInterface
{
private SyliusAbstractionStateMachineInterface $stateMachine;

public function __construct(SyliusAbstractionStateMachineInterface $stateMachine)
{
$this->stateMachine = $stateMachine;
}

public function getTransitionToState(object $subject, string $graphName, string $toState): ?string
{
return $this->stateMachine->getTransitionToState($subject, $graphName, $toState);
}

public function apply(object $subject, string $graphName, string $transition, array $context = []): void {
$this->stateMachine->apply($subject, $graphName, $transition, $context);
}
}
12 changes: 12 additions & 0 deletions src/Abstraction/StateMachine/StateMachineInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine;

interface StateMachineInterface
{
public function getTransitionToState(object $subject, string $graphName, string $toState): ?string;

public function apply(object $subject, string $graphName, string $transition, array $context = []): void;
}
34 changes: 34 additions & 0 deletions src/Abstraction/StateMachine/WinzouStateMachine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Abstraction\StateMachine;

use SM\Factory\FactoryInterface;
use Sylius\Resource\StateMachine\StateMachineInterface as SyliusStateMachineInterface;
use Webmozart\Assert\Assert;

final class WinzouStateMachine implements StateMachineInterface
{
private FactoryInterface $factory;

public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}

public function getTransitionToState(object $subject, string $graphName, string $toState): ?string
{
$stateMachine = $this->factory->get($subject, $graphName);
Assert::isInstanceOf($stateMachine, SyliusStateMachineInterface::class);

return $stateMachine->getTransitionToState($toState);
}

public function apply(object $subject, string $graphName, string $transition, array $context = []): void
{
$stateMachine = $this->factory->get($subject, $graphName);

$stateMachine->apply($transition);
}
}

This file was deleted.

5 changes: 5 additions & 0 deletions src/DependencyInjection/FluxSESyliusPayumStripeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ public function load(array $configs, ContainerBuilder $container): void
new FileLocator(dirname(__DIR__) . '/Resources/config'),
);
$loader->load('services.yaml');

if ($container->hasExtension('sylius_state_machine_abstraction')) {
$loader->load('services/abstraction/abstraction.yaml');
$loader->load('services/listener/workflow/sylius_payment.yaml');
}
}
}
35 changes: 35 additions & 0 deletions src/EventListener/Workflow/PaymentCompletedStateListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\EventListener\Workflow;

use FluxSE\SyliusPayumStripePlugin\StateMachine\PaymentStateProcessorInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Symfony\Component\Workflow\Event\CompletedEvent;
use Webmozart\Assert\Assert;

final class PaymentCompletedStateListener
{
private PaymentStateProcessorInterface $paymentStateProcessor;

public function __construct(PaymentStateProcessorInterface $paymentStateProcessor)
{
$this->paymentStateProcessor = $paymentStateProcessor;
}

public function __invoke(CompletedEvent $event): void
{
/** @var PaymentInterface $payment */
$payment = $event->getSubject();
Assert::isInstanceOf($payment, PaymentInterface::class);

$transition = $event->getTransition();
Assert::notNull($transition);
// state machine transition from list always contains 1 element
$fromState = $transition->getFroms()[0];
Assert::notNull($fromState);

$this->paymentStateProcessor->__invoke($payment, $fromState);
}
}
Loading

0 comments on commit 3d5e397

Please sign in to comment.