From 1b76e14d21d194b73fb87da3c3977ce054cc7b67 Mon Sep 17 00:00:00 2001 From: Ibes Date: Thu, 9 Jun 2022 16:42:42 +0200 Subject: [PATCH 1/6] OrderPlacedProducer linked to state machine in order to Doctrine post(persist|update) --- spec/Creator/InvoiceCreatorSpec.php | 8 +-- .../EventProducer/OrderPlacedProducerSpec.php | 68 +------------------ src/Creator/InvoiceCreator.php | 2 +- src/EventProducer/OrderPlacedProducer.php | 41 +---------- src/Resources/config/config.yml | 7 ++ src/Resources/config/services/events.xml | 2 - 6 files changed, 15 insertions(+), 113 deletions(-) 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..163ecaa8 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,12 +73,11 @@ 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); + $this->__invoke($order); } function it_does_nothing_after_persisting_if_event_entity_is_not_order( @@ -89,8 +87,6 @@ function it_does_nothing_after_persisting_if_event_entity_is_not_order( $event->getEntity()->willReturn('notAnOrder'); $eventBus->dispatch(Argument::any())->shouldNotBeCalled(); - - $this->postPersist($event); } function it_does_nothing_after_update_if_event_entity_is_not_order( @@ -100,65 +96,5 @@ function it_does_nothing_after_update_if_event_entity_is_not_order( $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); } } diff --git a/src/Creator/InvoiceCreator.php b/src/Creator/InvoiceCreator.php index 16faaa32..cc4a2334 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -38,7 +38,7 @@ public function __construct( public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void { /** @var OrderInterface $order */ - $order = $this->orderRepository->findOneByNumber($orderNumber); + $order = $this->orderRepository->findOneBy(['number' => $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..47b0d594 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: + callbacks: + after: + sylius_invoicing_plugin_order_placed_producer: + on: ['create'] + 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 @@ - - From a817d692b8252901b2240d489909c172116ccdb9 Mon Sep 17 00:00:00 2001 From: Ibes Date: Wed, 15 Jun 2022 08:35:05 +0200 Subject: [PATCH 2/6] OrderPlaced Event based on orderId in order to orderNumber --- spec/Creator/InvoiceCreatorSpec.php | 16 ++++++++-------- spec/Creator/MassInvoicesCreatorSpec.php | 12 ++++++------ spec/Event/OrderPlacedSpec.php | 4 ++-- .../CreateInvoiceOnOrderPlacedListenerSpec.php | 4 ++-- spec/EventProducer/OrderPlacedProducerSpec.php | 8 ++++---- src/Creator/InvoiceCreator.php | 6 +++--- src/Creator/InvoiceCreatorInterface.php | 2 +- src/Creator/MassInvoicesCreator.php | 2 +- src/Event/OrderPlaced.php | 10 +++++----- .../CreateInvoiceOnOrderPlacedListener.php | 2 +- src/EventProducer/OrderPlacedProducer.php | 2 +- src/Exception/InvoiceAlreadyGenerated.php | 4 ++-- 12 files changed, 36 insertions(+), 36 deletions(-) diff --git a/spec/Creator/InvoiceCreatorSpec.php b/spec/Creator/InvoiceCreatorSpec.php index 0059b632..8f32e784 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->findOneBy(['number' => '0000001'])->willReturn($order); + $orderRepository->find(1)->willReturn($order); $invoiceRepository->findOneByOrder($order)->willReturn(null); @@ -73,7 +73,7 @@ function it_creates_invoice_for_order( $invoiceRepository->add($invoice)->shouldBeCalled(); - $this->__invoke('0000001', $invoiceDateTime); + $this->__invoke(1, $invoiceDateTime); } function it_creates_invoice_without_generating_pdf_file( @@ -94,7 +94,7 @@ function it_creates_invoice_without_generating_pdf_file( false ); - $orderRepository->findOneBy(['number' => '0000001'])->willReturn($order); + $orderRepository->find(1)->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->findOneBy(['number' => '0000001'])->willReturn($order); + $orderRepository->find(1)->willReturn($order); $invoiceRepository->findOneByOrder($order)->willReturn(null); @@ -134,7 +134,7 @@ function it_removes_saved_invoice_file_if_database_update_fails( $invoiceRepository->add($invoice)->willThrow(ORMException::class); $invoiceFileManager->remove($invoicePdf)->shouldBeCalled(); - $this->__invoke('0000001', $invoiceDateTime); + $this->__invoke(1, $invoiceDateTime); } function it_throws_an_exception_when_invoice_was_already_created_for_given_order( @@ -144,7 +144,7 @@ function it_throws_an_exception_when_invoice_was_already_created_for_given_order OrderInterface $order, InvoiceInterface $invoice ): void { - $orderRepository->findOneBy(['number' => '0000001'])->willReturn($order); + $orderRepository->find(1)->willReturn($order); $invoiceRepository->findOneByOrder($order)->willReturn($invoice); $invoiceDateTime = new \DateTimeImmutable('2019-02-25'); @@ -153,8 +153,8 @@ function it_throws_an_exception_when_invoice_was_already_created_for_given_order $invoiceRepository->add(Argument::any())->shouldNotBeCalled(); $this - ->shouldThrow(InvoiceAlreadyGenerated::withOrderNumber('0000001')) - ->during('__invoke', ['0000001', $invoiceDateTime]) + ->shouldThrow(InvoiceAlreadyGenerated::withOrderId(1)) + ->during('__invoke', [1, $invoiceDateTime]) ; } } diff --git a/spec/Creator/MassInvoicesCreatorSpec.php b/spec/Creator/MassInvoicesCreatorSpec.php index 7f361d2b..b5235738 100644 --- a/spec/Creator/MassInvoicesCreatorSpec.php +++ b/spec/Creator/MassInvoicesCreatorSpec.php @@ -34,9 +34,9 @@ function it_requests_invoices_creation_for_multiple_orders( OrderInterface $secondOrder, OrderInterface $thirdOrder ): void { - $firstOrder->getNumber()->willReturn('0000001'); - $secondOrder->getNumber()->willReturn('0000002'); - $thirdOrder->getNumber()->willReturn('0000003'); + $firstOrder->getId()->willReturn(1); + $secondOrder->getId()->willReturn(2); + $thirdOrder->getId()->willReturn(3); $firstInvoiceDateTime = new \DateTimeImmutable('2019-02-25'); $secondInvoiceDateTime = new \DateTimeImmutable('2019-02-25'); @@ -44,9 +44,9 @@ function it_requests_invoices_creation_for_multiple_orders( $dateTimeProvider->__invoke()->willReturn($firstInvoiceDateTime, $secondInvoiceDateTime, $thirdInvoiceDateTime); - $invoiceCreator->__invoke('0000001', $firstInvoiceDateTime)->shouldBeCalled(); - $invoiceCreator->__invoke('0000002', $secondInvoiceDateTime)->shouldBeCalled(); - $invoiceCreator->__invoke('0000003', $thirdInvoiceDateTime)->shouldBeCalled(); + $invoiceCreator->__invoke(1, $firstInvoiceDateTime)->shouldBeCalled(); + $invoiceCreator->__invoke(2, $secondInvoiceDateTime)->shouldBeCalled(); + $invoiceCreator->__invoke(3, $thirdInvoiceDateTime)->shouldBeCalled(); $this->__invoke([$firstOrder->getWrappedObject(), $secondOrder->getWrappedObject(), $thirdOrder->getWrappedObject()]); } diff --git a/spec/Event/OrderPlacedSpec.php b/spec/Event/OrderPlacedSpec.php index 7f5d4d5e..ee04c5bf 100644 --- a/spec/Event/OrderPlacedSpec.php +++ b/spec/Event/OrderPlacedSpec.php @@ -21,9 +21,9 @@ function it_represents_an_immutable_fact_that_an_order_has_been_placed(): void { $date = new \DateTimeImmutable(); - $this->beConstructedWith('000001', $date); + $this->beConstructedWith(1, $date); - $this->orderNumber()->shouldReturn('000001'); + $this->orderId()->shouldReturn(1); $this->date()->shouldBeLike($date); } } diff --git a/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php b/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php index 8ced625a..66a5e488 100644 --- a/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php +++ b/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php @@ -28,8 +28,8 @@ function it_requests_invoice_creation(InvoiceCreatorInterface $invoiceCreator): { $issuedAt = new \DateTimeImmutable(); - $invoiceCreator->__invoke('0000001', $issuedAt)->shouldBeCalled(); + $invoiceCreator->__invoke(1, $issuedAt)->shouldBeCalled(); - $this(new OrderPlaced('0000001', $issuedAt)); + $this(new OrderPlaced(1, $issuedAt)); } } diff --git a/spec/EventProducer/OrderPlacedProducerSpec.php b/spec/EventProducer/OrderPlacedProducerSpec.php index 163ecaa8..6deb905f 100644 --- a/spec/EventProducer/OrderPlacedProducerSpec.php +++ b/spec/EventProducer/OrderPlacedProducerSpec.php @@ -44,10 +44,10 @@ function it_dispatches_an_order_placed_event_for_persisted_order( $dateTime = new \DateTime('2018-12-14'); $dateTimeProvider->__invoke()->willReturn($dateTime); - $order->getNumber()->willReturn('000666'); + $order->getId()->willReturn(666); $order->getCheckoutState()->willReturn(OrderCheckoutStates::STATE_COMPLETED); - $orderPlacedEvent = new OrderPlaced('000666', $dateTime); + $orderPlacedEvent = new OrderPlaced(666, $dateTime); $eventBus->dispatch($orderPlacedEvent)->shouldBeCalled()->willReturn(new Envelope($orderPlacedEvent)); @@ -71,9 +71,9 @@ function it_dispatches_an_order_placed_event_for_updated_order( $entityManager->getUnitOfWork()->willReturn($unitOfWork); - $order->getNumber()->willReturn('000666'); + $order->getId()->willReturn(666); - $orderPlacedEvent = new OrderPlaced('000666', $dateTime); + $orderPlacedEvent = new OrderPlaced(666, $dateTime); $eventBus->dispatch($orderPlacedEvent)->shouldBeCalled()->willReturn(new Envelope($orderPlacedEvent)); diff --git a/src/Creator/InvoiceCreator.php b/src/Creator/InvoiceCreator.php index cc4a2334..08cf54cd 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -35,16 +35,16 @@ public function __construct( ) { } - public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void + public function __invoke(int $orderId, \DateTimeInterface $dateTime): void { /** @var OrderInterface $order */ - $order = $this->orderRepository->findOneBy(['number' => $orderNumber]); + $order = $this->orderRepository->find($orderId); /** @var InvoiceInterface|null $invoice */ $invoice = $this->invoiceRepository->findOneByOrder($order); if (null !== $invoice) { - throw InvoiceAlreadyGenerated::withOrderNumber($orderNumber); + throw InvoiceAlreadyGenerated::withOrderId($orderId); } $invoice = $this->invoiceGenerator->generateForOrder($order, $dateTime); diff --git a/src/Creator/InvoiceCreatorInterface.php b/src/Creator/InvoiceCreatorInterface.php index fdff4c01..07fdab6a 100644 --- a/src/Creator/InvoiceCreatorInterface.php +++ b/src/Creator/InvoiceCreatorInterface.php @@ -15,5 +15,5 @@ interface InvoiceCreatorInterface { - public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void; + public function __invoke(int $orderId, \DateTimeInterface $dateTime): void; } diff --git a/src/Creator/MassInvoicesCreator.php b/src/Creator/MassInvoicesCreator.php index 70269593..5e752f39 100644 --- a/src/Creator/MassInvoicesCreator.php +++ b/src/Creator/MassInvoicesCreator.php @@ -36,7 +36,7 @@ public function __invoke(array $orders): void /** @var OrderInterface $order */ foreach ($orders as $order) { try { - $this->invoiceCreator->__invoke($order->getNumber(), $this->dateTimeProvider->__invoke()); + $this->invoiceCreator->__invoke($order->getId(), $this->dateTimeProvider->__invoke()); } catch (InvoiceAlreadyGenerated $exception) { continue; } diff --git a/src/Event/OrderPlaced.php b/src/Event/OrderPlaced.php index b78a6771..fd36869b 100644 --- a/src/Event/OrderPlaced.php +++ b/src/Event/OrderPlaced.php @@ -15,19 +15,19 @@ final class OrderPlaced { - private string $orderNumber; + private int $orderId; private \DateTimeInterface $date; - public function __construct(string $orderNumber, \DateTimeInterface $date) + public function __construct(int $orderId, \DateTimeInterface $date) { - $this->orderNumber = $orderNumber; + $this->orderId = $orderId; $this->date = clone $date; } - public function orderNumber(): string + public function orderId(): int { - return $this->orderNumber; + return $this->orderId; } public function date(): \DateTimeInterface diff --git a/src/EventListener/CreateInvoiceOnOrderPlacedListener.php b/src/EventListener/CreateInvoiceOnOrderPlacedListener.php index da6d6cbc..e273f254 100644 --- a/src/EventListener/CreateInvoiceOnOrderPlacedListener.php +++ b/src/EventListener/CreateInvoiceOnOrderPlacedListener.php @@ -29,7 +29,7 @@ public function __construct(InvoiceCreatorInterface $invoiceCreator) public function __invoke(OrderPlaced $event): void { try { - $this->invoiceCreator->__invoke($event->orderNumber(), $event->date()); + $this->invoiceCreator->__invoke($event->orderId(), $event->date()); } catch (InvoiceAlreadyGenerated $exception) { return; } diff --git a/src/EventProducer/OrderPlacedProducer.php b/src/EventProducer/OrderPlacedProducer.php index 74f74a63..e9f2711d 100644 --- a/src/EventProducer/OrderPlacedProducer.php +++ b/src/EventProducer/OrderPlacedProducer.php @@ -33,7 +33,7 @@ public function __construct(MessageBusInterface $eventBus, DateTimeProvider $dat public function __invoke(OrderInterface $order): void { $this->eventBus->dispatch( - new OrderPlaced($order->getNumber(), $this->dateTimeProvider->__invoke()) + new OrderPlaced($order->getId(), $this->dateTimeProvider->__invoke()) ); } } diff --git a/src/Exception/InvoiceAlreadyGenerated.php b/src/Exception/InvoiceAlreadyGenerated.php index d551330b..71279a53 100644 --- a/src/Exception/InvoiceAlreadyGenerated.php +++ b/src/Exception/InvoiceAlreadyGenerated.php @@ -15,8 +15,8 @@ final class InvoiceAlreadyGenerated extends \DomainException { - public static function withOrderNumber(string $orderNumber): self + public static function withOrderId(int $orderId): self { - return new self(sprintf('An invoice for order with number %s was already generated', $orderNumber)); + return new self(sprintf('An invoice for order with id %s was already generated', $orderId)); } } From 380941a832aad5fbf70510a58e1802431d22ee47 Mon Sep 17 00:00:00 2001 From: Ibes Date: Thu, 30 Jun 2022 21:31:58 +0200 Subject: [PATCH 3/6] Linked to state machine with orderNumber --- composer.json | 6 +- .../EventProducer/OrderPlacedProducerSpec.php | 86 +------------------ src/Creator/InvoiceCreator.php | 3 +- src/EventProducer/OrderPlacedProducer.php | 41 +-------- src/Resources/config/config.yml | 7 ++ src/Resources/config/services/events.xml | 2 - 6 files changed, 17 insertions(+), 128 deletions(-) diff --git a/composer.json b/composer.json index 645f648b..9b52a53f 100644 --- a/composer.json +++ b/composer.json @@ -75,7 +75,11 @@ ] }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "phpstan/extension-installer": true + } }, "extra": { "branch-alias": { 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..742708bd 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -38,7 +38,8 @@ public function __construct( public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void { /** @var OrderInterface $order */ - $order = $this->orderRepository->findOneByNumber($orderNumber); + $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 @@ - - From 3ec77e8d5d73dc67e9d248ef6d38f24016ed087b Mon Sep 17 00:00:00 2001 From: Ibes Date: Thu, 30 Jun 2022 22:52:15 +0200 Subject: [PATCH 4/6] Spec order number fix --- .php-version | 1 + spec/Creator/MassInvoicesCreatorSpec.php | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 .php-version diff --git a/.php-version b/.php-version new file mode 100644 index 00000000..cc40bca6 --- /dev/null +++ b/.php-version @@ -0,0 +1 @@ +8.0 diff --git a/spec/Creator/MassInvoicesCreatorSpec.php b/spec/Creator/MassInvoicesCreatorSpec.php index e51aa642..7f361d2b 100644 --- a/spec/Creator/MassInvoicesCreatorSpec.php +++ b/spec/Creator/MassInvoicesCreatorSpec.php @@ -34,9 +34,9 @@ function it_requests_invoices_creation_for_multiple_orders( OrderInterface $secondOrder, OrderInterface $thirdOrder ): void { - $firstOrder->getNumber()->willReturn('000001'); - $secondOrder->getNumber()->willReturn('000002'); - $thirdOrder->getNumber()->willReturn('000003'); + $firstOrder->getNumber()->willReturn('0000001'); + $secondOrder->getNumber()->willReturn('0000002'); + $thirdOrder->getNumber()->willReturn('0000003'); $firstInvoiceDateTime = new \DateTimeImmutable('2019-02-25'); $secondInvoiceDateTime = new \DateTimeImmutable('2019-02-25'); @@ -44,9 +44,9 @@ function it_requests_invoices_creation_for_multiple_orders( $dateTimeProvider->__invoke()->willReturn($firstInvoiceDateTime, $secondInvoiceDateTime, $thirdInvoiceDateTime); - $invoiceCreator->__invoke('000001', $firstInvoiceDateTime)->shouldBeCalled(); - $invoiceCreator->__invoke('000002', $secondInvoiceDateTime)->shouldBeCalled(); - $invoiceCreator->__invoke('000003', $thirdInvoiceDateTime)->shouldBeCalled(); + $invoiceCreator->__invoke('0000001', $firstInvoiceDateTime)->shouldBeCalled(); + $invoiceCreator->__invoke('0000002', $secondInvoiceDateTime)->shouldBeCalled(); + $invoiceCreator->__invoke('0000003', $thirdInvoiceDateTime)->shouldBeCalled(); $this->__invoke([$firstOrder->getWrappedObject(), $secondOrder->getWrappedObject(), $thirdOrder->getWrappedObject()]); } From f77bc23b6cd55807246c808393a56bde5d9d2796 Mon Sep 17 00:00:00 2001 From: Ibes Date: Thu, 30 Jun 2022 22:56:28 +0200 Subject: [PATCH 5/6] Php doc and orderNumber fix --- spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php | 4 ++-- src/Creator/InvoiceCreator.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php b/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php index 392074c6..8ced625a 100644 --- a/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php +++ b/spec/EventListener/CreateInvoiceOnOrderPlacedListenerSpec.php @@ -28,8 +28,8 @@ function it_requests_invoice_creation(InvoiceCreatorInterface $invoiceCreator): { $issuedAt = new \DateTimeImmutable(); - $invoiceCreator->__invoke('000001', $issuedAt)->shouldBeCalled(); + $invoiceCreator->__invoke('0000001', $issuedAt)->shouldBeCalled(); - $this(new OrderPlaced('000001', $issuedAt)); + $this(new OrderPlaced('0000001', $issuedAt)); } } diff --git a/src/Creator/InvoiceCreator.php b/src/Creator/InvoiceCreator.php index 797825a9..ef10d31c 100644 --- a/src/Creator/InvoiceCreator.php +++ b/src/Creator/InvoiceCreator.php @@ -38,7 +38,7 @@ public function __construct( public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void { - /** @var OrderInterface $order */ + /** @var OrderInterface|null $order */ $order = $this->orderRepository->findOneBy(['number' => $orderNumber]); Assert::notNull($order, sprintf('Order with number "%s" does not exist.', $orderNumber)); From 3c3a1fd703dcd07449c3b230f25747c12361c3a3 Mon Sep 17 00:00:00 2001 From: Ibes Date: Fri, 1 Jul 2022 08:02:48 +0200 Subject: [PATCH 6/6] remove php-version --- .php-version | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .php-version diff --git a/.php-version b/.php-version deleted file mode 100644 index cc40bca6..00000000 --- a/.php-version +++ /dev/null @@ -1 +0,0 @@ -8.0