$input * @return list * * @throws InvalidWorkflowStepTitle with the index of the step * @throws InvalidWorkflowStepMaterial with the indexes of the step and the material */ public function build(array $input): array { $steps = []; foreach ($input as $index => $step) { try { $title = new WorkflowStepTitle($step->title); } catch (InvalidWorkflowStepTitle) { throw new InvalidWorkflowStepTitle($index); } $materials = []; foreach ($step->materials as $materialIndex => $material) { try { $materials[] = WorkflowStepMaterial::parse($material['stockItemId'], $material['quantity']); } catch (InvalidWorkflowStepMaterial) { throw new InvalidWorkflowStepMaterial($index, $materialIndex); } } $steps[] = new WorkflowStep($title, $step->description, $step->workTypeId, $materials); } return $steps; } /** * A step's own work type must be in use and belong to the template work * type's unit or to the shared unit. A work type the template already * uses stays allowed after it is taken out of use. * * @param list $steps * @param list $currentSteps * * @throws WorkflowStepWorkTypeNotAllowed */ public function checkWorkTypes(WorkType $templateWorkType, array $steps, array $currentSteps = []): void { $currentIds = array_map(fn (WorkflowStep $step): ?int => $step->workTypeId, $currentSteps); foreach ($steps as $index => $step) { if ($step->workTypeId === null || $step->workTypeId === $templateWorkType->id()) { continue; } $workType = $this->workTypes->findById($step->workTypeId); if ($workType === null || (! $workType->isActive() && ! in_array($workType->id(), $currentIds, true)) || ! $this->sameOrSharedUnit($templateWorkType, $workType)) { throw new WorkflowStepWorkTypeNotAllowed($index); } } } private function sameOrSharedUnit(WorkType $templateWorkType, WorkType $workType): bool { return $workType->serviceUnitId() === $templateWorkType->serviceUnitId() || ($this->serviceUnits->findById($workType->serviceUnitId())?->isShared() ?? false); } }