transactions->run(function () use ($level, $id, $name): void { match ($level) { CatalogLevel::ServiceUnit => $this->renameServiceUnit($id, $name), CatalogLevel::Service => $this->renameService($id, $name), CatalogLevel::WorkType => throw new InvalidArgumentException('Use UpdateWorkType for work types.'), }; }); } private function renameServiceUnit(int $id, CatalogName $name): void { $serviceUnit = $this->serviceUnits->findById($id) ?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $id); if ($this->serviceUnits->isNameTaken($name->value, $id)) { throw new CatalogNameTaken; } $serviceUnit->rename($name); $this->serviceUnits->save($serviceUnit); } private function renameService(int $id, CatalogName $name): void { $service = $this->services->findById($id) ?? throw new CatalogItemNotFound(CatalogLevel::Service, $id); if ($this->services->isNameTaken($service->serviceUnitId(), $name->value, $id)) { throw new CatalogNameTaken; } $service->rename($name); $this->services->save($service); } }