diff --git a/src/Sylius/Bundle/ApiBundle/Command/SendOrderConfirmation.php b/src/Sylius/Bundle/ApiBundle/Command/SendOrderConfirmation.php index f41facf5baf..4d62f485450 100644 --- a/src/Sylius/Bundle/ApiBundle/Command/SendOrderConfirmation.php +++ b/src/Sylius/Bundle/ApiBundle/Command/SendOrderConfirmation.php @@ -13,20 +13,18 @@ namespace Sylius\Bundle\ApiBundle\Command; -use Sylius\Component\Order\Model\OrderInterface; - class SendOrderConfirmation { - /** @var OrderInterface */ - protected $order; + /** @var string */ + protected $orderToken; - public function __construct(OrderInterface $order) + public function __construct(string $orderToken) { - $this->order = $order; + $this->orderToken = $orderToken; } - public function order(): OrderInterface + public function orderToken(): string { - return $this->order; + return $this->orderToken; } } diff --git a/src/Sylius/Bundle/ApiBundle/CommandHandler/Checkout/CompleteOrderHandler.php b/src/Sylius/Bundle/ApiBundle/CommandHandler/Checkout/CompleteOrderHandler.php index c6aece624da..a081a2777a1 100644 --- a/src/Sylius/Bundle/ApiBundle/CommandHandler/Checkout/CompleteOrderHandler.php +++ b/src/Sylius/Bundle/ApiBundle/CommandHandler/Checkout/CompleteOrderHandler.php @@ -14,8 +14,9 @@ namespace Sylius\Bundle\ApiBundle\CommandHandler\Checkout; use SM\Factory\FactoryInterface; +use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart; use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder; -use Sylius\Bundle\ApiBundle\Event\OrderCompletedEvent; +use Sylius\Bundle\ApiBundle\Event\OrderCompleted; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\OrderCheckoutTransitions; use Sylius\Component\Core\Repository\OrderRepositoryInterface; @@ -34,16 +35,16 @@ final class CompleteOrderHandler implements MessageHandlerInterface private $stateMachineFactory; /** @var MessageBusInterface */ - private $messageBus; + private $eventBus; public function __construct( OrderRepositoryInterface $orderRepository, FactoryInterface $stateMachineFactory, - MessageBusInterface $messageBus + MessageBusInterface $eventBus ) { $this->orderRepository = $orderRepository; $this->stateMachineFactory = $stateMachineFactory; - $this->messageBus = $messageBus; + $this->eventBus = $eventBus; } public function __invoke(CompleteOrder $completeOrder): OrderInterface @@ -68,7 +69,7 @@ public function __invoke(CompleteOrder $completeOrder): OrderInterface $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE); - $this->messageBus->dispatch(new OrderCompletedEvent($orderTokenValue), [new DispatchAfterCurrentBusStamp()]); + $this->eventBus->dispatch(new OrderCompleted($cart->getTokenValue()), [new DispatchAfterCurrentBusStamp()]); return $cart; } diff --git a/src/Sylius/Bundle/ApiBundle/CommandHandler/OrderCompletedHandler.php b/src/Sylius/Bundle/ApiBundle/CommandHandler/OrderCompletedHandler.php new file mode 100644 index 00000000000..88416e6f42e --- /dev/null +++ b/src/Sylius/Bundle/ApiBundle/CommandHandler/OrderCompletedHandler.php @@ -0,0 +1,36 @@ +bus = $bus; + } + + public function __invoke(OrderCompleted $orderCompleted): void + { + $this->bus->dispatch(new SendOrderConfirmation($orderCompleted->orderToken())); + } +} diff --git a/src/Sylius/Bundle/ApiBundle/CommandHandler/SendOrderConfirmationHandler.php b/src/Sylius/Bundle/ApiBundle/CommandHandler/SendOrderConfirmationHandler.php index 2a0e501e678..65a076146fe 100644 --- a/src/Sylius/Bundle/ApiBundle/CommandHandler/SendOrderConfirmationHandler.php +++ b/src/Sylius/Bundle/ApiBundle/CommandHandler/SendOrderConfirmationHandler.php @@ -15,6 +15,8 @@ use Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation; use Sylius\Bundle\CoreBundle\Mailer\Emails; +use Sylius\Component\Core\Model\OrderInterface; +use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Mailer\Sender\SenderInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; @@ -24,14 +26,19 @@ final class SendOrderConfirmationHandler implements MessageHandlerInterface /** @var SenderInterface */ private $emailSender; - public function __construct(SenderInterface $emailSender) + /** @var OrderRepositoryInterface */ + private $orderRepository; + + public function __construct(SenderInterface $emailSender, OrderRepositoryInterface $orderRepository) { $this->emailSender = $emailSender; + $this->orderRepository = $orderRepository; } public function __invoke(SendOrderConfirmation $sendOrderConfirmation): void { - $order = $sendOrderConfirmation->order(); + /** @var OrderInterface $order */ + $order = $this->orderRepository->findOneByTokenValue($sendOrderConfirmation->orderToken()); $this->emailSender->send( Emails::ORDER_CONFIRMATION_RESENT, diff --git a/src/Sylius/Bundle/ApiBundle/Event/OrderCompletedEvent.php b/src/Sylius/Bundle/ApiBundle/Event/OrderCompleted.php similarity index 94% rename from src/Sylius/Bundle/ApiBundle/Event/OrderCompletedEvent.php rename to src/Sylius/Bundle/ApiBundle/Event/OrderCompleted.php index 6495e064c17..0802f7fc43f 100644 --- a/src/Sylius/Bundle/ApiBundle/Event/OrderCompletedEvent.php +++ b/src/Sylius/Bundle/ApiBundle/Event/OrderCompleted.php @@ -13,7 +13,7 @@ namespace Sylius\Bundle\ApiBundle\Event; -final class OrderCompletedEvent +final class OrderCompleted { /** @var string */ protected $orderToken; diff --git a/src/Sylius/Bundle/ApiBundle/EventListener/OrderCompleteListener.php b/src/Sylius/Bundle/ApiBundle/EventListener/OrderCompleteListener.php deleted file mode 100644 index 8ef9ee023d7..00000000000 --- a/src/Sylius/Bundle/ApiBundle/EventListener/OrderCompleteListener.php +++ /dev/null @@ -1,42 +0,0 @@ -bus = $bus; - $this->orderRepository = $orderRepository; - } - - public function __invoke(OrderCompletedEvent $orderCompleted): void - { - $order = $this->orderRepository->findOneByTokenValue($orderCompleted->orderToken()); - - $this->bus->dispatch(new Envelope(new SendOrderConfirmation($order))); - } -} diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml index f0a3021903c..e3ef5b69a38 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/services/command_handlers.xml @@ -78,8 +78,8 @@ - - + + @@ -104,7 +104,14 @@ + + + + + + + diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/services/listeners.xml deleted file mode 100644 index 72cb7279f91..00000000000 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/services/listeners.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Checkout/CompleteOrderHandlerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Checkout/CompleteOrderHandlerSpec.php index 3963d7d7747..8eb3546d04a 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Checkout/CompleteOrderHandlerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Checkout/CompleteOrderHandlerSpec.php @@ -14,15 +14,14 @@ namespace spec\Sylius\Bundle\ApiBundle\CommandHandler\Checkout; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; use SM\Factory\FactoryInterface; use SM\StateMachine\StateMachineInterface; use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder; -use Sylius\Bundle\ApiBundle\Event\OrderCompletedEvent; -use Sylius\Bundle\CoreBundle\Mailer\OrderEmailManagerInterface; +use Sylius\Bundle\ApiBundle\Event\OrderCompleted; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\OrderCheckoutTransitions; use Sylius\Component\Core\Repository\OrderRepositoryInterface; +use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp; @@ -31,9 +30,9 @@ final class CompleteOrderHandlerSpec extends ObjectBehavior function let( OrderRepositoryInterface $orderRepository, FactoryInterface $stateMachineFactory, - MessageBusInterface $messageBus + MessageBusInterface $eventBus ): void { - $this->beConstructedWith($orderRepository, $stateMachineFactory, $messageBus); + $this->beConstructedWith($orderRepository, $stateMachineFactory, $eventBus); } function it_handles_order_completion_without_notes( @@ -41,7 +40,7 @@ function it_handles_order_completion_without_notes( StateMachineInterface $stateMachine, OrderInterface $order, FactoryInterface $stateMachineFactory, - MessageBusInterface $messageBus + MessageBusInterface $eventBus ): void { $completeOrder = new CompleteOrder(); $completeOrder->setOrderTokenValue('ORDERTOKEN'); @@ -56,6 +55,12 @@ function it_handles_order_completion_without_notes( $stateMachine->apply('complete')->shouldBeCalled(); + $orderCompleted = new OrderCompleted('COMPLETED_ORDER_TOKEN'); + + $eventBus->dispatch($orderCompleted, [new DispatchAfterCurrentBusStamp()]) + ->willReturn(new Envelope($orderCompleted)) + ->shouldBeCalled(); + $this($completeOrder)->shouldReturn($order); } @@ -64,7 +69,7 @@ function it_handles_order_completion_with_notes( StateMachineInterface $stateMachine, OrderInterface $order, FactoryInterface $stateMachineFactory, - OrderEmailManagerInterface $emailManager + MessageBusInterface $eventBus ): void { $completeOrder = new CompleteOrder('ThankYou'); $completeOrder->setOrderTokenValue('ORDERTOKEN'); @@ -79,7 +84,11 @@ function it_handles_order_completion_with_notes( $stateMachine->apply('complete')->shouldBeCalled(); - $emailManager->sendConfirmationEmail($order)->shouldBeCalled(); + $orderCompleted = new OrderCompleted('COMPLETED_ORDER_TOKEN'); + + $eventBus->dispatch($orderCompleted, [new DispatchAfterCurrentBusStamp()]) + ->willReturn(new Envelope($orderCompleted)) + ->shouldBeCalled(); $this($completeOrder)->shouldReturn($order); } diff --git a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/SendOrderConfirmationHandlerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/SendOrderConfirmationHandlerSpec.php index a418ce9c094..97e97ac7b9a 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/SendOrderConfirmationHandlerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/SendOrderConfirmationHandlerSpec.php @@ -19,15 +19,15 @@ use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\OrderInterface; +use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Mailer\Sender\SenderInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; final class SendOrderConfirmationHandlerSpec extends ObjectBehavior { - function let( - SenderInterface $sender - ): void { - $this->beConstructedWith($sender); + function let(SenderInterface $sender, OrderRepositoryInterface $orderRepository): void + { + $this->beConstructedWith($sender, $orderRepository); } function it_is_a_message_handler(): void @@ -40,9 +40,12 @@ function it_sends_order_confirmation_message( SendOrderConfirmation $sendOrderConfirmation, SenderInterface $sender, CustomerInterface $customer, - ChannelInterface $channel + ChannelInterface $channel, + OrderRepositoryInterface $orderRepository ): void { - $sendOrderConfirmation->order()->willReturn($order); + $sendOrderConfirmation->orderToken()->willReturn('TOKEN'); + + $orderRepository->findOneByTokenValue('TOKEN')->willReturn($order); $order->getChannel()->willReturn($channel); $order->getLocaleCode()->willReturn('pl_PL'); @@ -60,6 +63,6 @@ function it_sends_order_confirmation_message( ] )->shouldBeCalled(); - $this(new SendOrderConfirmation($order->getWrappedObject())); + $this(new SendOrderConfirmation('TOKEN')); } }