transactions->run(function () use ($jobId, $taskId, $fromUserId, $toUserId, $from, $to, $actorUserId): ScheduleResult { $job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId); $task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId); $peopleBefore = PeopleAddedToTask::peopleOf($task); if ($fromUserId !== $toUserId) { $this->references->check($job, $task->workTypeId(), $toUserId, [], $task); $task->assignTo($toUserId); } $task->schedule($from, $to ?? $this->planner->defaultEnd($from, $task)); $task = $this->tasks->save($task); $extended = $this->planner->plan($task); $task = $this->tasks->save($task); $added = PeopleAddedToTask::between($peopleBefore, $task, $job, $actorUserId); if ($added !== null) { $this->events->dispatch($added); } return new ScheduleResult( (string) $task->plannedFrom()?->format('Y-m-d'), (string) $task->plannedTo()?->format('Y-m-d'), $extended, ); }); } }