diff --git a/spec/Creator/InvoiceCreatorSpec.php b/spec/Creator/InvoiceCreatorSpec.php index 325c35d6..0059b632 100644 --- a/spec/Creator/InvoiceCreatorSpec.php +++ b/spec/Creator/InvoiceCreatorSpec.php @@ -61,7 +61,7 @@ function it_creates_invoice_for_order( ): void { $invoicePdf = new InvoicePdf('invoice.pdf', 'CONTENT'); - $orderRepository->findOneByNumber('0000001')->willReturn($order); + $orderRepository->findOneBy(['number' => '0000001'])->willReturn($order); $invoiceRepository->findOneByOrder($order)->willReturn(null); @@ -94,7 +94,7 @@ function it_creates_invoice_without_generating_pdf_file( false ); - $orderRepository->findOneByNumber('0000001')->willReturn($order); + $orderRepository->findOneBy(['number' => '0000001'])->willReturn($order); $invoiceRepository->findOneByOrder($order)->willReturn(null); @@ -121,7 +121,7 @@ function it_removes_saved_invoice_file_if_database_update_fails( ): void { $invoicePdf = new InvoicePdf('invoice.pdf', 'CONTENT'); - $orderRepository->findOneByNumber('0000001')->willReturn($order); + $orderRepository->findOneBy(['number' => '0000001'])->willReturn($order); $invoiceRepository->findOneByOrder($order)->willReturn(null); @@ -144,7 +144,7 @@ function it_throws_an_exception_when_invoice_was_already_created_for_given_order OrderInterface $order, InvoiceInterface $invoice ): void { - $orderRepository->findOneByNumber('0000001')->willReturn($order); + $orderRepository->findOneBy(['number' => '0000001'])->willReturn($order); $invoiceRepository->findOneByOrder($order)->willReturn($invoice); $invoiceDateTime = new \DateTimeImmutable('2019-02-25'); diff --git a/spec/EventProducer/OrderPlacedProducerSpec.php b/spec/EventProducer/OrderPlacedProducerSpec.php index dddc0ff0..7297c56a 100644 --- a/spec/EventProducer/OrderPlacedProducerSpec.php +++ b/spec/EventProducer/OrderPlacedProducerSpec.php @@ -47,12 +47,11 @@ function it_dispatches_an_order_placed_event_for_persisted_order( $order->getNumber()->willReturn('000666'); $order->getCheckoutState()->willReturn(OrderCheckoutStates::STATE_COMPLETED); - $postPersistEvent = new LifecycleEventArgs($order->getWrappedObject(), $objectManager->getWrappedObject()); $orderPlacedEvent = new OrderPlaced('000666', $dateTime); $eventBus->dispatch($orderPlacedEvent)->shouldBeCalled()->willReturn(new Envelope($orderPlacedEvent)); - $this->postPersist($postPersistEvent); + $this->__invoke($order); } function it_dispatches_an_order_placed_event_for_updated_order( @@ -74,91 +73,10 @@ function it_dispatches_an_order_placed_event_for_updated_order( $order->getNumber()->willReturn('000666'); - $postUpdateEvent = new LifecycleEventArgs($order->getWrappedObject(), $entityManager->getWrappedObject()); $orderPlacedEvent = new OrderPlaced('000666', $dateTime); $eventBus->dispatch($orderPlacedEvent)->shouldBeCalled()->willReturn(new Envelope($orderPlacedEvent)); - $this->postUpdate($postUpdateEvent); - } - - function it_does_nothing_after_persisting_if_event_entity_is_not_order( - MessageBusInterface $eventBus, - LifecycleEventArgs $event - ): void { - $event->getEntity()->willReturn('notAnOrder'); - - $eventBus->dispatch(Argument::any())->shouldNotBeCalled(); - - $this->postPersist($event); - } - - function it_does_nothing_after_update_if_event_entity_is_not_order( - MessageBusInterface $eventBus, - LifecycleEventArgs $event - ): void { - $event->getEntity()->willReturn('notAnOrder'); - - $eventBus->dispatch(Argument::any())->shouldNotBeCalled(); - - $this->postUpdate($event); - } - - function it_does_nothing_after_persisting_if_order_is_not_completed( - MessageBusInterface $eventBus, - LifecycleEventArgs $event, - OrderInterface $order - ): void { - $event->getEntity()->willReturn($order); - - $order->getCheckoutState()->willReturn(OrderCheckoutStates::STATE_CART); - - $eventBus->dispatch(Argument::any())->shouldNotBeCalled(); - - $this->postPersist($event); - } - - function it_does_nothing_after_update_if_order_checkout_state_has_not_changed( - MessageBusInterface $eventBus, - LifecycleEventArgs $event, - EntityManagerInterface $entityManager, - OrderInterface $order - ): void { - $event->getEntity()->willReturn($order); - - $event->getEntityManager()->willReturn($entityManager); - - /** @var UnitOfWork|MockInterface $unitOfWork */ - $unitOfWork = Mockery::mock(UnitOfWork::class); - $unitOfWork->shouldReceive('getEntityChangeSet')->withArgs([$order->getWrappedObject()])->andReturn([]); - - $entityManager->getUnitOfWork()->willReturn($unitOfWork); - - $eventBus->dispatch(Argument::any())->shouldNotBeCalled(); - - $this->postUpdate($event); - } - - function it_does_nothing_after_update_if_order_checkout_state_has_not_changed_to_completed( - MessageBusInterface $eventBus, - LifecycleEventArgs $event, - EntityManagerInterface $entityManager, - OrderInterface $order - ): void { - $event->getEntity()->willReturn($order); - - $event->getEntityManager()->willReturn($entityManager); - - /** @var UnitOfWork|MockInterface $unitOfWork */ - $unitOfWork = Mockery::mock(UnitOfWork::class); - $unitOfWork->shouldReceive('getEntityChangeSet')->withArgs([$order->getWrappedObject()])->andReturn([ - 'checkoutState' => [OrderCheckoutStates::STATE_CART, OrderCheckoutStates::STATE_ADDRESSED], - ]); - - $entityManager->getUnitOfWork()->willReturn($unitOfWork); - - $eventBus->dispatch(Argument::any())->shouldNotBeCalled(); - - $this->postUpdate($event); + $this->__invoke($order); } } diff --git a/src/Creator/InvoiceCreator.php b/src/Creator/InvoiceCreator.php index 16faaa32..ef10d31c 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -13,15 +13,16 @@ namespace Sylius\InvoicingPlugin\Creator; +use Webmozart\Assert\Assert; use Doctrine\ORM\ORMException; use Sylius\Component\Core\Model\OrderInterface; -use Sylius\Component\Core\Repository\OrderRepositoryInterface; -use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface; use Sylius\InvoicingPlugin\Entity\InvoiceInterface; use Sylius\InvoicingPlugin\Exception\InvoiceAlreadyGenerated; +use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\InvoicingPlugin\Generator\InvoiceGeneratorInterface; -use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface; use Sylius\InvoicingPlugin\Manager\InvoiceFileManagerInterface; +use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface; +use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface; final class InvoiceCreator implements InvoiceCreatorInterface { @@ -37,8 +38,9 @@ public function __construct( public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void { - /** @var OrderInterface $order */ - $order = $this->orderRepository->findOneByNumber($orderNumber); + /** @var OrderInterface|null $order */ + $order = $this->orderRepository->findOneBy(['number' => $orderNumber]); + Assert::notNull($order, sprintf('Order with number "%s" does not exist.', $orderNumber)); /** @var InvoiceInterface|null $invoice */ $invoice = $this->invoiceRepository->findOneByOrder($order); diff --git a/src/EventProducer/OrderPlacedProducer.php b/src/EventProducer/OrderPlacedProducer.php index 7e536aee..74f74a63 100644 --- a/src/EventProducer/OrderPlacedProducer.php +++ b/src/EventProducer/OrderPlacedProducer.php @@ -13,9 +13,7 @@ namespace Sylius\InvoicingPlugin\EventProducer; -use Doctrine\ORM\Event\LifecycleEventArgs; use Sylius\Component\Core\Model\OrderInterface; -use Sylius\Component\Core\OrderCheckoutStates; use Sylius\InvoicingPlugin\DateTimeProvider; use Sylius\InvoicingPlugin\Event\OrderPlaced; use Symfony\Component\Messenger\MessageBusInterface; @@ -32,44 +30,7 @@ public function __construct(MessageBusInterface $eventBus, DateTimeProvider $dat $this->dateTimeProvider = $dateTimeProvider; } - public function postPersist(LifecycleEventArgs $event): void - { - $order = $event->getEntity(); - - if ( - !$order instanceof OrderInterface || - $order->getCheckoutState() !== OrderCheckoutStates::STATE_COMPLETED - ) { - return; - } - - $this->dispatchOrderPlacedEvent($order); - } - - public function postUpdate(LifecycleEventArgs $event): void - { - $order = $event->getEntity(); - - if (!$order instanceof OrderInterface) { - return; - } - - $entityManager = $event->getEntityManager(); - - $unitOfWork = $entityManager->getUnitOfWork(); - $changeSet = $unitOfWork->getEntityChangeSet($order); - - if ( - !isset($changeSet['checkoutState']) || - $changeSet['checkoutState'][1] !== OrderCheckoutStates::STATE_COMPLETED - ) { - return; - } - - $this->dispatchOrderPlacedEvent($order); - } - - private function dispatchOrderPlacedEvent(OrderInterface $order): void + public function __invoke(OrderInterface $order): void { $this->eventBus->dispatch( new OrderPlaced($order->getNumber(), $this->dateTimeProvider->__invoke()) diff --git a/src/Resources/config/config.yml b/src/Resources/config/config.yml index a5fc7bf0..51426b5b 100644 --- a/src/Resources/config/config.yml +++ b/src/Resources/config/config.yml @@ -25,6 +25,13 @@ winzou_state_machine: on: ['complete'] do: ['@sylius_invoicing_plugin.event_producer.order_payment_paid', '__invoke'] args: ['object'] + sylius_order_checkout: + callbacks: + after: + sylius_invoicing_plugin_order_placed_producer: + on: ['complete'] + do: ['@sylius_invoicing_plugin.event_producer.order_placed', '__invoke'] + args: ['object'] sylius_grid: templates: diff --git a/src/Resources/config/services/events.xml b/src/Resources/config/services/events.xml index 4e2f50ef..d818dc01 100644 --- a/src/Resources/config/services/events.xml +++ b/src/Resources/config/services/events.xml @@ -18,8 +18,6 @@ - -