$memberUserIds * * @throws InvalidTaskReference */ public function check(CustomerJob $job, int $workTypeId, ?int $assigneeUserId, array $memberUserIds = [], ?Task $current = null): void { if ($workTypeId !== $current?->workTypeId() && ! in_array($workTypeId, $this->workTypeIdsFor($job), true)) { throw new InvalidTaskReference('work_type'); } $activeIds = array_map(fn ($option): int => $option->id, $this->users->activeUserOptions()); $currentIds = $current === null ? [] : [$current->assigneeUserId(), ...$current->memberUserIds()]; $allowed = fn (int $userId): bool => in_array($userId, $activeIds, true) || in_array($userId, $currentIds, true); if ($assigneeUserId !== null && ! $allowed($assigneeUserId)) { throw new InvalidTaskReference('assignee'); } foreach ($memberUserIds as $userId) { if (! $allowed($userId)) { throw new InvalidTaskReference('members'); } } } /** * @return list */ private function workTypeIdsFor(CustomerJob $job): array { $ids = []; foreach ($this->catalog->activeWorkTypesFor($job->details()->serviceUnitIds) as $unit) { foreach ($unit->workTypes as $workType) { $ids[] = $workType->id; } foreach ($unit->services as $service) { foreach ($service->workTypes as $workType) { $ids[] = $workType->id; } } } return $ids; } }