Files
Toiminnanohjaus/app/Modules/Machines/Domain/Entities/QueueEntry.php
T
2026-11-26 16:43:55 +02:00

133 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Entities;
use App\Modules\Machines\Domain\Exceptions\QueueEntryCompleted;
use DateTimeImmutable;
/**
* A row of a machine's work list (spec 19.4): a job's task, or a step of a
* quick sale order line, to be made on the machine on its planned day, in
* its position among that day's rows. A row not made on its day stays open
* and shows as late on the following days. Completed rows leave the list.
*/
final class QueueEntry
{
private function __construct(
private ?int $id,
private int $machineId,
private DateTimeImmutable $plannedOn,
private int $position,
private ?int $taskId,
private ?int $orderLineId,
private ?int $stepPosition,
private ?DateTimeImmutable $completedAt,
private ?int $completedBy,
) {}
public static function forTask(int $machineId, int $taskId, DateTimeImmutable $plannedOn, int $position): self
{
return new self(null, $machineId, $plannedOn, $position, $taskId, null, null, null, null);
}
public static function forOrderLine(int $machineId, int $orderLineId, int $stepPosition, DateTimeImmutable $plannedOn, int $position): self
{
return new self(null, $machineId, $plannedOn, $position, null, $orderLineId, $stepPosition, null, null);
}
public static function reconstitute(int $id, int $machineId, DateTimeImmutable $plannedOn, int $position, ?int $taskId, ?int $orderLineId, ?int $stepPosition, ?DateTimeImmutable $completedAt, ?int $completedBy): self
{
return new self($id, $machineId, $plannedOn, $position, $taskId, $orderLineId, $stepPosition, $completedAt, $completedBy);
}
/**
* @throws QueueEntryCompleted
*/
public function plan(int $machineId, DateTimeImmutable $plannedOn, int $position): void
{
$this->ensureOpen();
$this->machineId = $machineId;
$this->plannedOn = $plannedOn;
$this->position = $position;
}
public function moveTo(int $position): void
{
$this->position = $position;
}
/**
* @param int|null $userId Null when the task was marked done elsewhere.
*
* @throws QueueEntryCompleted
*/
public function complete(?int $userId, DateTimeImmutable $at): void
{
$this->ensureOpen();
$this->completedBy = $userId;
$this->completedAt = $at;
}
/**
* @throws QueueEntryCompleted
*/
public function ensureOpen(): void
{
if ($this->completedAt !== null) {
throw new QueueEntryCompleted;
}
}
public function isOrderLine(): bool
{
return $this->orderLineId !== null;
}
public function id(): ?int
{
return $this->id;
}
public function machineId(): int
{
return $this->machineId;
}
public function plannedOn(): DateTimeImmutable
{
return $this->plannedOn;
}
public function position(): int
{
return $this->position;
}
public function taskId(): ?int
{
return $this->taskId;
}
public function orderLineId(): ?int
{
return $this->orderLineId;
}
public function stepPosition(): ?int
{
return $this->stepPosition;
}
public function completedAt(): ?DateTimeImmutable
{
return $this->completedAt;
}
public function completedBy(): ?int
{
return $this->completedBy;
}
}