From caf95fb5e94b8901cfa8176196d3c82eb3134dc1 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Wed, 17 Jul 2019 10:38:49 +0200 Subject: [PATCH] Check if transition can be applied before apply it during order creation --- spec/EventListener/OrderCreationListenerSpec.php | 8 ++++---- src/EventListener/OrderCreationListener.php | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/spec/EventListener/OrderCreationListenerSpec.php b/spec/EventListener/OrderCreationListenerSpec.php index c2ba19ca..8379385c 100644 --- a/spec/EventListener/OrderCreationListenerSpec.php +++ b/spec/EventListener/OrderCreationListenerSpec.php @@ -43,10 +43,10 @@ function it_completes_order_before_creation( $event->getSubject()->willReturn($order); $stateMachineFactory->get($order, 'sylius_order_checkout')->willReturn($stateMachine); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS)->shouldBeCalled(); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->shouldBeCalled(); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->shouldBeCalled(); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE)->shouldBeCalled(); + $stateMachine->can(OrderCheckoutTransitions::TRANSITION_ADDRESS)->shouldBeCalled(); + $stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->shouldBeCalled(); + $stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->shouldBeCalled(); + $stateMachine->can(OrderCheckoutTransitions::TRANSITION_COMPLETE)->shouldBeCalled(); $this->completeOrderBeforeCreation($event); } diff --git a/src/EventListener/OrderCreationListener.php b/src/EventListener/OrderCreationListener.php index 6e90beab..6239e0ea 100644 --- a/src/EventListener/OrderCreationListener.php +++ b/src/EventListener/OrderCreationListener.php @@ -40,9 +40,17 @@ public function completeOrderBeforeCreation(GenericEvent $event): void Assert::isInstanceOf($order, OrderInterface::class); $stateMachine = $this->stateMachineFactory->get($order, 'sylius_order_checkout'); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT); - $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE); + $transitions = [ + OrderCheckoutTransitions::TRANSITION_ADDRESS, + OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING, + OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT, + OrderCheckoutTransitions::TRANSITION_COMPLETE, + ]; + + foreach ($transitions as $transition) { + if ($stateMachine->can($transition)) { + $stateMachine->apply($transition); + } + } } }