Hi,
For example, there's remote API with the following calls:
getGroupCapacity(group)
setGroupCapacity(group, quantity)
getNumberOfItemsInGroup(group)
addItemToGroup(group, item)
deleteItemFromGroup(group, item)
The task is to add some item to some group. Groups have capacity. So first we should check if group is not full. If it is, increase capacity, then add item. Something like this (for example API is exposed with SOAP):
function add_item($group, $item) {
$soap = new SoapClient(...);
$capacity = $soap->getGroupCapacity($group);
$itemsInGroup = $soap->getNumberOfItemsInGroup($group);
if ($itemsInGroup == $capacity) {
$soap->setGroupCapacity($group, $capacity + 1);
}
$soap->addItemToGroup($group, $item);
}
Now what if addItemToGroup failed (item was bad)? We need to rollback group's capacity.
Now imagine that you have to add 10 items to group and then setup added items with some properties - and all this in a single transaction. That means if it fails somewhere in the middle you must rollback everything to previous state.
Is it possible without bunch of IF's and spaghetti code? Any library, framework, pattern, or architecture decision which will simplify such operations (in PHP)?
UPD: SOAP is just an example. Solution should fit any service, even raw TCP. The main point of the question is how to organize transactional behavior with underlying non-transactional API.
UPD2: I guess this problem is pretty same in all programming languages. So any answers are welcomed, not only PHP.
Thanks in advance!