$memberUserIds */ private function __construct( private ?int $id, private int $jobId, private int $workTypeId, private TaskTitle $title, private ?string $description, private TaskStatus $status, private ?DateTimeImmutable $dueOn, private ?EstimatedHours $estimatedHours, private ?int $assigneeUserId, private array $memberUserIds, private int $createdBy, private ?DateTimeImmutable $plannedFrom = null, private ?DateTimeImmutable $plannedTo = null, ) { $this->memberUserIds = self::members($memberUserIds, $assigneeUserId); self::checkPlan($plannedFrom, $plannedTo); } /** * @param list $memberUserIds * * @throws TasksNotAllowedYet * @throws PlanEndsBeforeStart */ public static function create( int $jobId, JobStatus $jobStatus, int $workTypeId, TaskTitle $title, ?string $description, TaskStatus $status, ?DateTimeImmutable $dueOn, ?EstimatedHours $estimatedHours, ?int $assigneeUserId, array $memberUserIds, int $createdBy, ?DateTimeImmutable $plannedFrom = null, ?DateTimeImmutable $plannedTo = null, ): self { if (! $jobStatus->allowsTasks()) { throw new TasksNotAllowedYet; } return new self(null, $jobId, $workTypeId, $title, self::text($description), $status, $dueOn, $estimatedHours, $assigneeUserId, $memberUserIds, $createdBy, ...self::plan($plannedFrom, $plannedTo)); } /** * @param list $memberUserIds */ public static function reconstitute( int $id, int $jobId, int $workTypeId, TaskTitle $title, ?string $description, TaskStatus $status, ?DateTimeImmutable $dueOn, ?EstimatedHours $estimatedHours, ?int $assigneeUserId, array $memberUserIds, int $createdBy, ?DateTimeImmutable $plannedFrom = null, ?DateTimeImmutable $plannedTo = null, ): self { return new self($id, $jobId, $workTypeId, $title, $description, $status, $dueOn, $estimatedHours, $assigneeUserId, $memberUserIds, $createdBy, $plannedFrom, $plannedTo); } /** * @param list $memberUserIds */ public function changeDetails( int $workTypeId, TaskTitle $title, ?string $description, TaskStatus $status, ?DateTimeImmutable $dueOn, ?EstimatedHours $estimatedHours, ?int $assigneeUserId, array $memberUserIds, ): void { $this->workTypeId = $workTypeId; $this->title = $title; $this->description = self::text($description); $this->status = $status; $this->dueOn = $dueOn; $this->estimatedHours = $estimatedHours; $this->assigneeUserId = $assigneeUserId; $this->memberUserIds = self::members($memberUserIds, $assigneeUserId); } public function changeStatus(TaskStatus $status): void { $this->status = $status; } /** * Sets the planned schedule. A start without an end lasts one day; * null as the start removes the schedule. * * @throws PlanEndsBeforeStart */ public function schedule(?DateTimeImmutable $from, ?DateTimeImmutable $to): void { [$this->plannedFrom, $this->plannedTo] = self::plan($from, $to); } /** * Makes the user the assignee (null: no assignee). A member who becomes * the assignee is no longer a member; the earlier assignee is dropped. */ public function assignTo(?int $userId): void { $this->assigneeUserId = $userId; $this->memberUserIds = self::members($this->memberUserIds, $userId); } public function id(): ?int { return $this->id; } public function jobId(): int { return $this->jobId; } public function workTypeId(): int { return $this->workTypeId; } public function title(): TaskTitle { return $this->title; } public function description(): ?string { return $this->description; } public function status(): TaskStatus { return $this->status; } public function dueOn(): ?DateTimeImmutable { return $this->dueOn; } public function estimatedHours(): ?EstimatedHours { return $this->estimatedHours; } public function plannedFrom(): ?DateTimeImmutable { return $this->plannedFrom; } public function plannedTo(): ?DateTimeImmutable { return $this->plannedTo; } public function isScheduled(): bool { return $this->plannedFrom !== null; } public function assigneeUserId(): ?int { return $this->assigneeUserId; } /** * @return list */ public function memberUserIds(): array { return $this->memberUserIds; } public function createdBy(): int { return $this->createdBy; } /** * The members sorted, without duplicates and without the assignee. * * @param list $ids * @return list */ private static function members(array $ids, ?int $assigneeUserId): array { $ids = array_values(array_unique(array_filter($ids, fn (int $id): bool => $id !== $assigneeUserId))); sort($ids); return $ids; } /** * @return array{0: ?DateTimeImmutable, 1: ?DateTimeImmutable} * * @throws PlanEndsBeforeStart */ private static function plan(?DateTimeImmutable $from, ?DateTimeImmutable $to): array { if ($from === null) { return [null, null]; } $to ??= $from; self::checkPlan($from, $to); return [$from, $to]; } /** * @throws PlanEndsBeforeStart */ private static function checkPlan(?DateTimeImmutable $from, ?DateTimeImmutable $to): void { if ($from !== null && $to !== null && $to < $from) { throw new PlanEndsBeforeStart; } } private static function text(?string $value): ?string { $value = $value === null ? '' : trim($value); return $value === '' ? null : $value; } }