transactions->run(function () use ($groupId, $name): void { $group = $this->groups->findById($groupId) ?? throw new StockItemGroupNotFound; if ($this->groups->isNameTaken($name->value, $groupId)) { throw new StockNameTaken; } $group->rename($name); $this->groups->save($group); }); } /** * @return bool False when the group already was first (up) or last. * * @throws StockItemGroupNotFound */ public function move(int $groupId, bool $up): bool { return $this->transactions->run(function () use ($groupId, $up): bool { $group = $this->groups->findById($groupId) ?? throw new StockItemGroupNotFound; $neighbour = $this->groups->neighbour($group, $up); if ($neighbour === null) { return false; } $group->swapOrderWith($neighbour); $this->groups->save($group); $this->groups->save($neighbour); return true; }); } /** * @throws StockItemGroupNotFound */ public function setActive(int $groupId, bool $active): void { $this->transactions->run(function () use ($groupId, $active): void { $group = $this->groups->findById($groupId) ?? throw new StockItemGroupNotFound; $active ? $group->activate() : $group->deactivate(); $this->groups->save($group); }); } }