generated from Setono/SyliusPluginSkeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split the generic sales order data mapper into multiple smaller mappers
- Loading branch information
Showing
8 changed files
with
336 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusShipmondoPlugin\DataMapper; | ||
|
||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Setono\Shipmondo\Request\SalesOrders\OrderLine; | ||
use Setono\Shipmondo\Request\SalesOrders\SalesOrder; | ||
use Setono\SyliusShipmondoPlugin\Event\MapOrderLineEvent; | ||
use Setono\SyliusShipmondoPlugin\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\OrderItemUnitInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class OrderLinesSalesOrderDataMapper implements SalesOrderDataMapperInterface | ||
{ | ||
public function __construct(private readonly EventDispatcherInterface $eventDispatcher) | ||
{ | ||
} | ||
|
||
public function map(OrderInterface $order, SalesOrder $salesOrder): void | ||
{ | ||
foreach ($order->getItems() as $orderItem) { | ||
/** @var OrderItemUnitInterface|false $orderItemUnit */ | ||
$orderItemUnit = $orderItem->getUnits()->first(); | ||
Assert::isInstanceOf($orderItemUnit, OrderItemUnitInterface::class); | ||
|
||
$unitPriceExcludingVat = $orderItemUnit->getTotal() - $orderItemUnit->getTaxTotal(); | ||
|
||
$orderLine = new OrderLine( | ||
itemName: sprintf('%s (%s)', (string) $orderItem->getProductName(), (string) $orderItem->getVariantName()), | ||
itemSku: $orderItem->getVariant()?->getCode(), | ||
quantity: $orderItem->getQuantity(), | ||
unitPriceExcludingVat: self::formatAmount($unitPriceExcludingVat), | ||
vatPercent: (string) ($orderItemUnit->getTaxTotal() / $unitPriceExcludingVat), | ||
currencyCode: $order->getCurrencyCode(), | ||
unitWeight: null === $orderItem->getVariant()?->getWeight() ? null : (int) $orderItem->getVariant()?->getWeight(), | ||
); | ||
|
||
$this->eventDispatcher->dispatch(new MapOrderLineEvent($orderLine, $orderItem, $order)); | ||
|
||
$salesOrder->orderLines[] = $orderLine; | ||
} | ||
} | ||
|
||
private static function formatAmount(int $amount): string | ||
{ | ||
return (string) round($amount / 100, 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusShipmondoPlugin\DataMapper; | ||
|
||
use Setono\Shipmondo\Request\SalesOrders\PaymentDetails; | ||
use Setono\Shipmondo\Request\SalesOrders\SalesOrder; | ||
use Setono\SyliusShipmondoPlugin\Model\OrderInterface; | ||
use Setono\SyliusShipmondoPlugin\Model\PaymentMethodInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class PaymentDetailsSalesOrderDataMapper implements SalesOrderDataMapperInterface | ||
{ | ||
public function map(OrderInterface $order, SalesOrder $salesOrder): void | ||
{ | ||
$paymentMethod = self::getPaymentMethod($order); | ||
|
||
$salesOrder->paymentDetails = new PaymentDetails( | ||
amountIncludingVat: self::formatAmount($order->getTotal()), // todo this is not necessarily correct | ||
currencyCode: $order->getCurrencyCode(), | ||
vatAmount: self::formatAmount($order->getTaxTotal()), | ||
paymentMethod: $paymentMethod?->getName(), | ||
paymentGatewayId: null === $paymentMethod ? null : (string) $paymentMethod->getShipmondoId(), | ||
); | ||
} | ||
|
||
private static function formatAmount(int $amount): string | ||
{ | ||
return (string) round($amount / 100, 2); | ||
} | ||
|
||
private static function getPaymentMethod(OrderInterface $order): ?PaymentMethodInterface | ||
{ | ||
$paymentMethod = null; | ||
$payment = $order->getPayments()->first(); | ||
if (false !== $payment) { | ||
$paymentMethod = $payment->getMethod(); | ||
} | ||
Assert::nullOrIsInstanceOf($paymentMethod, PaymentMethodInterface::class); | ||
|
||
return $paymentMethod; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
tests/DataMapper/OrderLinesSalesOrderDataMapperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Setono\SyliusShipmondoPlugin\DataMapper; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Setono\Shipmondo\Request\SalesOrders\SalesOrder; | ||
use Setono\SyliusShipmondoPlugin\DataMapper\OrderLinesSalesOrderDataMapper; | ||
use Sylius\Component\Core\Model\Adjustment; | ||
use Sylius\Component\Core\Model\AdjustmentInterface; | ||
use Sylius\Component\Core\Model\OrderItem; | ||
use Sylius\Component\Core\Model\OrderItemUnit; | ||
use Sylius\Component\Core\Model\Payment; | ||
use Tests\Setono\SyliusShipmondoPlugin\Application\Model\Order; | ||
use Tests\Setono\SyliusShipmondoPlugin\Application\Model\PaymentMethod; | ||
|
||
final class OrderLinesSalesOrderDataMapperTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_maps_order_lines_with_tax_included_in_price(): void | ||
{ | ||
$order = self::getOrder(); | ||
$order->addItem(self::getOrderItem(100, 20, true)); | ||
$order->addItem(self::getOrderItem(200, 40, true)); | ||
|
||
$salesOrder = new SalesOrder(); | ||
|
||
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); | ||
|
||
$mapper = new OrderLinesSalesOrderDataMapper($eventDispatcher->reveal()); | ||
$mapper->map($order, $salesOrder); | ||
|
||
self::assertCount(2, $salesOrder->orderLines); | ||
self::assertSame('0.8', $salesOrder->orderLines[0]->unitPriceExcludingVat); | ||
self::assertSame(1, $salesOrder->orderLines[0]->quantity); | ||
self::assertSame('0.25', $salesOrder->orderLines[0]->vatPercent); | ||
|
||
self::assertSame('1.6', $salesOrder->orderLines[1]->unitPriceExcludingVat); | ||
self::assertSame(1, $salesOrder->orderLines[1]->quantity); | ||
self::assertSame('0.25', $salesOrder->orderLines[1]->vatPercent); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_maps_order_lines_with_tax_not_included_in_price(): void | ||
{ | ||
$order = self::getOrder(); | ||
$order->addItem(self::getOrderItem(100, 20, false)); | ||
$order->addItem(self::getOrderItem(200, 40, false)); | ||
|
||
$salesOrder = new SalesOrder(); | ||
|
||
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); | ||
|
||
$mapper = new OrderLinesSalesOrderDataMapper($eventDispatcher->reveal()); | ||
$mapper->map($order, $salesOrder); | ||
|
||
self::assertCount(2, $salesOrder->orderLines); | ||
self::assertSame('1', $salesOrder->orderLines[0]->unitPriceExcludingVat); | ||
self::assertSame(1, $salesOrder->orderLines[0]->quantity); | ||
self::assertSame('0.2', $salesOrder->orderLines[0]->vatPercent); | ||
self::assertSame('T-Shirt (XL)', $salesOrder->orderLines[0]->itemName); | ||
|
||
self::assertSame('2', $salesOrder->orderLines[1]->unitPriceExcludingVat); | ||
self::assertSame(1, $salesOrder->orderLines[1]->quantity); | ||
self::assertSame('0.2', $salesOrder->orderLines[1]->vatPercent); | ||
self::assertSame('T-Shirt (XL)', $salesOrder->orderLines[1]->itemName); | ||
} | ||
|
||
private static function getOrder(): Order | ||
{ | ||
$order = new Order(); | ||
$order->setCurrencyCode('USD'); | ||
|
||
$paymentMethod = new PaymentMethod(); | ||
$paymentMethod->setCurrentLocale('en_US'); | ||
$paymentMethod->setName('Credit Card'); | ||
$paymentMethod->setShipmondoId(123); | ||
|
||
$payment = new Payment(); | ||
$payment->setMethod($paymentMethod); | ||
|
||
$order->addPayment($payment); | ||
|
||
return $order; | ||
} | ||
|
||
private static function getOrderItem(int $unitPrice, int $taxAmount, bool $taxNeutral): OrderItem | ||
{ | ||
$orderItem = new OrderItem(); | ||
$orderItem->setUnitPrice($unitPrice); | ||
|
||
$orderItem->setProductName('T-Shirt'); | ||
$orderItem->setVariantName('XL'); | ||
|
||
$taxAdjustment = new Adjustment(); | ||
$taxAdjustment->setAmount($taxAmount); | ||
$taxAdjustment->setNeutral($taxNeutral); | ||
$taxAdjustment->setType(AdjustmentInterface::TAX_ADJUSTMENT); | ||
|
||
$orderItemUnit = new OrderItemUnit($orderItem); | ||
$orderItemUnit->addAdjustment($taxAdjustment); | ||
|
||
return $orderItem; | ||
} | ||
} |
Oops, something went wrong.