$lines */ private function __construct( private ?int $id, private DateTimeImmutable $orderedOn, private int $pickupNumber, private DateTimeImmutable $orderedAt, private int $userId, private ?int $customerId, private ?string $customerName, private array $lines, private ?DateTimeImmutable $paidAt, private ?DateTimeImmutable $cancelledAt, private ?int $cancelledBy, ) { if ($lines === []) { throw new QuickSaleEmpty; } } /** * @param list $lines * * @throws QuickSaleEmpty */ public static function place(DateTimeImmutable $orderedOn, int $pickupNumber, DateTimeImmutable $orderedAt, int $userId, ?int $customerId, ?string $customerName, array $lines): self { $customerName = $customerName === null ? null : mb_substr(trim($customerName), 0, 150); return new self(null, $orderedOn, $pickupNumber, $orderedAt, $userId, $customerId, $customerName === '' ? null : $customerName, $lines, null, null, null); } /** * @param list $lines */ public static function reconstitute(int $id, DateTimeImmutable $orderedOn, int $pickupNumber, DateTimeImmutable $orderedAt, int $userId, ?int $customerId, ?string $customerName, array $lines, ?DateTimeImmutable $paidAt, ?DateTimeImmutable $cancelledAt, ?int $cancelledBy): self { return new self($id, $orderedOn, $pickupNumber, $orderedAt, $userId, $customerId, $customerName, $lines, $paidAt, $cancelledAt, $cancelledBy); } public function status(): OrderStatus { return match (true) { $this->cancelledAt !== null => OrderStatus::Cancelled, $this->isMade() && $this->paidAt !== null => OrderStatus::Completed, $this->isMade() => OrderStatus::Ready, default => OrderStatus::InProduction, }; } public function isMade(): bool { foreach ($this->lines as $line) { if (! $line->isMade()) { return false; } } return true; } /** * @throws QuickSaleOrderClosed */ public function ensureOpen(): void { if ($this->cancelledAt !== null || $this->status() === OrderStatus::Completed) { throw new QuickSaleOrderClosed; } } /** * @throws QuickSaleOrderClosed */ public function pay(DateTimeImmutable $at): void { if ($this->paidAt !== null || $this->cancelledAt !== null) { throw new QuickSaleOrderClosed; } $this->paidAt = $at; } /** * @throws QuickSaleOrderPaid * @throws QuickSaleOrderClosed */ public function cancel(int $userId, DateTimeImmutable $at): void { if ($this->paidAt !== null) { throw new QuickSaleOrderPaid; } if ($this->cancelledAt !== null) { throw new QuickSaleOrderClosed; } $this->cancelledAt = $at; $this->cancelledBy = $userId; } public function lineById(int $lineId): ?OrderLine { foreach ($this->lines as $line) { if ($line->id() === $lineId) { return $line; } } return null; } public function id(): ?int { return $this->id; } public function orderedOn(): DateTimeImmutable { return $this->orderedOn; } public function pickupNumber(): int { return $this->pickupNumber; } public function orderedAt(): DateTimeImmutable { return $this->orderedAt; } public function userId(): int { return $this->userId; } public function customerId(): ?int { return $this->customerId; } public function customerName(): ?string { return $this->customerName; } /** * @return list */ public function lines(): array { return $this->lines; } public function paidAt(): ?DateTimeImmutable { return $this->paidAt; } public function cancelledAt(): ?DateTimeImmutable { return $this->cancelledAt; } public function cancelledBy(): ?int { return $this->cancelledBy; } }