ldc-boutique-cnc-sable/domain/Models/ValueObjects/OrderLineProduct.php

65 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?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
);
}
2026-06-14 21:32:07 +02:00
}