Skip to content

Commit

Permalink
Take the 'ship' transition on the shipment instead of the order and a…
Browse files Browse the repository at this point in the history
…lso set the tracking number on the shipment
  • Loading branch information
loevgaard committed Sep 9, 2024
1 parent c34e895 commit 1e03ddd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"sylius/payment": "^1.0",
"sylius/product": "^1.0",
"sylius/resource-bundle": "^1.0",
"sylius/shipping": "^1.0",
"sylius/ui-bundle": "^1.0",
"symfony/config": "^5.4 || ^6.4",
"symfony/console": "^5.4 || ^6.4",
Expand Down
30 changes: 22 additions & 8 deletions src/WebhookHandler/OrderPackedWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Setono\SyliusPeakPlugin\Model\OrderInterface;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\OrderShippingTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Order\OrderTransitions;
use Sylius\Component\Payment\PaymentTransitions;
use Sylius\Component\Shipping\ShipmentTransitions;
use Webmozart\Assert\Assert;

final class OrderPackedWebhookHandler implements WebhookHandlerInterface, LoggerAwareInterface
Expand Down Expand Up @@ -65,13 +65,7 @@ public function handle(object $data): void
throw new \InvalidArgumentException(sprintf('Order lines on order %s are different', $data->orderId));
}

$orderShippingStateMachine = $this->stateMachineFactory->get($order, OrderShippingTransitions::GRAPH);

if ($orderShippingStateMachine->can(OrderShippingTransitions::TRANSITION_SHIP)) {
$this->logger->debug(sprintf('Shipment: Taking the "%s" transition', OrderShippingTransitions::TRANSITION_SHIP));

$orderShippingStateMachine->apply(OrderShippingTransitions::TRANSITION_SHIP);
}
$this->completeShipment($order, $data);

if ($data->paymentCaptured) {
$this->completePayment($order);
Expand Down Expand Up @@ -126,6 +120,26 @@ private static function assertSame(array $syliusOrderLines, array $peakOrderLine
Assert::count($peakOrderLines, 0);
}

private function completeShipment(OrderInterface $order, WebhookDataPickOrderPacked $data): void
{
$shipment = $order->getshipments()->last();
if (false === $shipment) {
$this->logger->debug('There is no shipment on the order');

return;
}

$shipment->setTracking($data->trackingNumber);

Check warning on line 132 in src/WebhookHandler/OrderPackedWebhookHandler.php

View check run for this annotation

Codecov / codecov/patch

src/WebhookHandler/OrderPackedWebhookHandler.php#L132

Added line #L132 was not covered by tests

$shipmentStateMachine = $this->stateMachineFactory->get($shipment, ShipmentTransitions::GRAPH);

Check warning on line 134 in src/WebhookHandler/OrderPackedWebhookHandler.php

View check run for this annotation

Codecov / codecov/patch

src/WebhookHandler/OrderPackedWebhookHandler.php#L134

Added line #L134 was not covered by tests

if ($shipmentStateMachine->can(ShipmentTransitions::TRANSITION_SHIP)) {
$this->logger->debug(sprintf('Shipment: Taking the "%s" transition', ShipmentTransitions::TRANSITION_SHIP));

Check warning on line 137 in src/WebhookHandler/OrderPackedWebhookHandler.php

View check run for this annotation

Codecov / codecov/patch

src/WebhookHandler/OrderPackedWebhookHandler.php#L136-L137

Added lines #L136 - L137 were not covered by tests

$shipmentStateMachine->apply(ShipmentTransitions::TRANSITION_SHIP);

Check warning on line 139 in src/WebhookHandler/OrderPackedWebhookHandler.php

View check run for this annotation

Codecov / codecov/patch

src/WebhookHandler/OrderPackedWebhookHandler.php#L139

Added line #L139 was not covered by tests
}
}

private function completePayment(OrderInterface $order): void
{
$this->logger->debug(sprintf('The payment is captured, so we will check if we can take the "%s" transition', PaymentTransitions::TRANSITION_COMPLETE));
Expand Down

0 comments on commit 1e03ddd

Please sign in to comment.