transactions->run(function () use ($level, $id, $active): void { match ($level) { CatalogLevel::ServiceUnit => $this->serviceUnits->save($this->toggled($this->serviceUnits->findById($id), $level, $id, $active)), CatalogLevel::Service => $this->services->save($this->toggled($this->services->findById($id), $level, $id, $active)), CatalogLevel::WorkType => $this->workTypes->save($this->toggled($this->workTypes->findById($id), $level, $id, $active)), }; }); } /** * @template T of CatalogItem * * @param T|null $item * @return T */ private function toggled(?CatalogItem $item, CatalogLevel $level, int $id, bool $active): CatalogItem { if ($item === null) { throw new CatalogItemNotFound($level, $id); } $active ? $item->activate() : $item->deactivate(); return $item; } }