Files
Toiminnanohjaus/app/Modules/Catalog/Application/Services/WorkflowStepBuilder.php

97 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\Services;
use App\Modules\Catalog\Application\DTOs\WorkflowStepData;
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
use App\Modules\Catalog\Domain\Contracts\WorkTypeRepository;
use App\Modules\Catalog\Domain\Entities\WorkType;
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepMaterial;
use App\Modules\Catalog\Domain\Exceptions\InvalidWorkflowStepTitle;
use App\Modules\Catalog\Domain\Exceptions\WorkflowStepWorkTypeNotAllowed;
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStep;
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStepMaterial;
use App\Modules\Catalog\Domain\ValueObjects\WorkflowStepTitle;
/**
* Builds the steps of a workflow template from input and checks their own work types.
*/
final class WorkflowStepBuilder
{
public function __construct(
private WorkTypeRepository $workTypes,
private ServiceUnitRepository $serviceUnits,
) {}
/**
* @param list<WorkflowStepData> $input
* @return list<WorkflowStep>
*
* @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<WorkflowStep> $steps
* @param list<WorkflowStep> $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);
}
}