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.
94 lines
2.6 KiB
PHP
94 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Models\Entities;
|
|
|
|
use Domain\Enums\OrderStatus;
|
|
use Domain\Models\ValueObjects\DeliveryAddress;
|
|
use Domain\Models\ValueObjects\Entities\OrderId;
|
|
use Domain\Models\ValueObjects\Entities\ShopId;
|
|
use Domain\Models\ValueObjects\OrderEmployee;
|
|
use Domain\Models\ValueObjects\OrderLine;
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
use Domain\Models\ValueObjects\PickupTimeSlot;
|
|
use Money\Money;
|
|
use Shared\DDD\Entity;
|
|
|
|
/** @implements \Shared\DDD\Entity<\Domain\Models\ValueObjects\Entities\OrderId> */
|
|
final class Order implements Entity
|
|
{
|
|
public function __construct(
|
|
public readonly OrderId $id,
|
|
public readonly ShopId $shopId,
|
|
public readonly OrderReference $reference,
|
|
public readonly OrderEmployee $employee,
|
|
public readonly DeliveryAddress $deliveryAddress,
|
|
public readonly PickupTimeSlot $pickupTimeSlot,
|
|
public OrderStatus $status,
|
|
private readonly Money $total,
|
|
/** @var OrderLine[] */
|
|
private array $lines,
|
|
) {
|
|
if ($total->getAmount() <= 0) {
|
|
throw new \DomainException('Total amount must be greater than zero');
|
|
}
|
|
foreach ($lines as $line) {
|
|
if (! ($line instanceof OrderLine)) {
|
|
throw new \DomainException('All lines must be instances of OrderLine');
|
|
}
|
|
}
|
|
}
|
|
|
|
#[\Override]
|
|
public function id(): OrderId
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getTotal(): Money
|
|
{
|
|
return $this->total;
|
|
}
|
|
|
|
/** @return OrderLine[] */
|
|
public function getLines(): array
|
|
{
|
|
return $this->lines;
|
|
}
|
|
|
|
public function cancel(): void
|
|
{
|
|
if ($this->status === OrderStatus::CANCELED) {
|
|
return;
|
|
}
|
|
|
|
$this->status = OrderStatus::CANCELED;
|
|
}
|
|
|
|
public static function fromBasket(
|
|
OrderId $id,
|
|
OrderReference $reference,
|
|
Basket $basket,
|
|
PickupTimeSlot $pickupTimeSlot,
|
|
Shopper $shopper,
|
|
Shop $shop
|
|
): self {
|
|
$lines = array_map(
|
|
fn (\Domain\Models\ValueObjects\BasketProduct $item) => OrderLine::fromBasketProduct($item),
|
|
array_values($basket->items())
|
|
);
|
|
|
|
return new self(
|
|
id: $id,
|
|
shopId: clone $shop->id(),
|
|
reference: $reference,
|
|
employee: OrderEmployee::fromShopper($shopper),
|
|
deliveryAddress: DeliveryAddress::fromShop($shop),
|
|
pickupTimeSlot: clone $pickupTimeSlot,
|
|
status: OrderStatus::VALIDATED,
|
|
total: clone $basket->totalTaxIncluded(),
|
|
lines: $lines
|
|
);
|
|
}
|
|
}
|