ldc-boutique-cnc-sable/domain/Models/ValueObjects/OrderLineProduct.php
Jérémy Munsch 71e0d65f31
refactor(OrderService): simplify order creation from basket
Updated the OrderService to utilize the new fromBasket method for creating orders, enhancing code clarity and maintainability. Removed the ProductAvailabilityService as its functionality is now integrated into the ProductRepository.
2026-10-29 20:21:32 +01:00

64 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Domain\Enums\ProductBrand;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\ValueObjects\Entities\ProductId;
use Money\Money;
final readonly class OrderLineProduct
{
public function __construct(
public IncoId $incoId,
public ProductId $originalId,
public string $name,
public ProductWeight $weight,
public ProductBrand $brand,
public OrderUnit $orderUnit,
public Money $price,
public Money $discountedPrice,
public Money $priceKgTtc,
public TaxRate $taxRate,
public ?string $picture = null
) {
if (trim($name) === '') {
throw new \DomainException('Product name cannot be empty');
}
if ($price->isNegative()) {
throw new \DomainException('Product price cannot be negative');
}
if ($discountedPrice->isNegative()) {
throw new \DomainException('Product discounted price cannot be negative');
}
if ($priceKgTtc->isNegative()) {
throw new \DomainException('Price per kg TTC cannot be negative');
}
if ($discountedPrice->greaterThan($price)) {
throw new \DomainException('Product discounted price cannot be greater than original price');
}
if (! $priceKgTtc->isPositive()) {
throw new \DomainException('Price per kg TTC has to be positive');
}
}
public static function fromBasketProduct(BasketProduct $item): self
{
return new self(
incoId: $item->incoId ?? throw new \DomainException('incoId cannot be null for an order'),
originalId: $item->id,
name: $item->name,
weight: new ProductWeight($item->weightGrams, $item->weightVariable),
brand: $item->brand ?? ProductBrand::none,
orderUnit: $item->orderUnit,
price: clone $item->unitPriceHt,
discountedPrice: $item->unitPriceDiscounted(),
priceKgTtc: clone $item->priceKgTtc,
taxRate: $item->taxRate,
picture: $item->picture
);
}
}