Skip to content

Commit

Permalink
Remove AbstractDataTransferObject::convertEnum because Valinor handle…
Browse files Browse the repository at this point in the history
…s it out of the box
  • Loading branch information
loevgaard committed Jun 6, 2024
1 parent 8b642f2 commit b19e1d9
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 50 deletions.
28 changes: 0 additions & 28 deletions src/DataTransferObject/AbstractDataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,4 @@ protected static function convertDateTime(null|\DateTimeImmutable|string $value)

return $res;
}

/**
* @template T of \BackedEnum
*
* @param int|string|T|null $value
* @param class-string<T> $enumClass
*
* @return T|null
*
* @throws \InvalidArgumentException if the value is not an instance of the enum class
*/
protected static function convertEnum(null|int|string|\BackedEnum $value, string $enumClass): ?\BackedEnum
{
if (null === $value) {
return null;
}

if ($value instanceof \BackedEnum) {
if (!is_a($value, $enumClass)) {
throw new \InvalidArgumentException(sprintf('The value %s is not an instance of the enum class %s', $value::class, $enumClass));
}

return $value;
}

/** @psalm-suppress PossiblyInvalidArgument */
return $enumClass::from($value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ final class SalesOrderLine extends AbstractDataTransferObject

public ?string $variantId;

public ?State $state;

public function __construct(
int|string $orderLineId,
/**
Expand All @@ -32,13 +30,12 @@ public function __construct(
public ?float $salesDiscountPiece = null,
public ?float $salesDiscountTaxPiece = null,
public ?string $comments = null,
State|int $state = null,
public ?State $state = null,
public ?int $materialId = null,
public ?int $sendAgainQuantity = null,
) {
$this->orderLineId = (string) $orderLineId;
$this->productId = (string) $productId;
$this->variantId = null === $variantId ? null : (string) $variantId;
$this->state = self::convertEnum($state, State::class);
}
}
20 changes: 4 additions & 16 deletions src/DataTransferObject/SalesOrder/SalesOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ final class SalesOrder extends AbstractDataTransferObject

public ?DateTimeImmutable $requestedDeliveryDate = null;

public ?State $state;

public ?OnHoldReason $onHoldReason;

public ?PaymentStatus $paymentStatus;

public ?PickAgainReason $pickAgainReason;

/**
* @param list<SalesOrderLine> $orderLines
*/
Expand All @@ -51,8 +43,8 @@ public function __construct(
DateTimeImmutable|string $requestedDeliveryDate = null,
/** @var int<1, 10>|null $priority */
public ?int $priority = null,
int|State $state = null,
int|OnHoldReason $onHoldReason = null,
public ?State $state = null,
public ?OnHoldReason $onHoldReason = null,
public ?string $paymentMethod = null,
public ?float $paymentFee = null,
public ?float $paymentFeeTax = null,
Expand All @@ -74,8 +66,8 @@ public function __construct(
public ?string $transactionNumber = null,
public bool $giftWrap = false,

Check warning on line 67 in src/DataTransferObject/SalesOrder/SalesOrder.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * Otherwise, PeakWMS will not be able to find and capture the order in the payment integration. */ public ?string $transactionNumber = null, - public bool $giftWrap = false, + public bool $giftWrap = true, public ?string $customerReference = null, public ?PaymentStatus $paymentStatus = null, public ?PickAgainReason $pickAgainReason = null,
public ?string $customerReference = null,
int|PaymentStatus $paymentStatus = null,
int|PickAgainReason $pickAgainReason = null,
public ?PaymentStatus $paymentStatus = null,
public ?PickAgainReason $pickAgainReason = null,
public ?string $storeId = null,
public ?Address $shippingAddress = null,
public ?Address $senderAddress = null,
Expand All @@ -88,9 +80,5 @@ public function __construct(
$this->orderNumber = (string) $orderNumber;

Check warning on line 80 in src/DataTransferObject/SalesOrder/SalesOrder.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ ) { $this->orderId = (string) $orderId; - $this->orderNumber = (string) $orderNumber; + $this->orderNumber = $orderNumber; $this->orderDateTime = self::convertDateTime($orderDateTime); $this->requestedDeliveryDate = self::convertDateTime($requestedDeliveryDate); } }
$this->orderDateTime = self::convertDateTime($orderDateTime);
$this->requestedDeliveryDate = self::convertDateTime($requestedDeliveryDate);
$this->state = self::convertEnum($state, State::class);
$this->onHoldReason = self::convertEnum($onHoldReason, OnHoldReason::class);
$this->paymentStatus = self::convertEnum($paymentStatus, PaymentStatus::class);
$this->pickAgainReason = self::convertEnum($pickAgainReason, PickAgainReason::class);
}
}
2 changes: 0 additions & 2 deletions tests/DataTransferObject/SalesOrder/SalesOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ public function it_initializes(): void
country: 'DK',
),
orderDateTime: $now->format(\DATE_ATOM),
state: 3,
);

self::assertSame('123', $salesOrder->orderId);
self::assertSame('gls', $salesOrder->forwarderProductId);
self::assertSame('123', $salesOrder->orderNumber);
self::assertInstanceOf(Address::class, $salesOrder->billingAddress);
self::assertSame($now->format(\DATE_ATOM), $salesOrder->orderDateTime?->format(\DATE_ATOM));
self::assertSame(State::Packed, $salesOrder->state);
}
}

0 comments on commit b19e1d9

Please sign in to comment.