items->findById($itemId) ?? throw new StockItemNotFound; $stored = $uploadedFilePath === null ? null : $this->images->store($uploadedFilePath, $itemId); /** @var list $old */ $old = $this->transactions->run(function () use ($itemId, $stored): array { $item = $this->items->findById($itemId) ?? throw new StockItemNotFound; $old = array_values(array_filter([$item->imagePath(), $item->thumbnailPath()])); if ($stored === null) { $item->removeImage(); } else { $item->setImage($stored['path'], $stored['thumbnailPath']); } $this->items->save($item); return $old; }); $this->images->delete($old); } }