Skip to content

Instantly share code, notes, and snippets.

@alexey-bass
Created October 5, 2011 17:06
Show Gist options
  • Save alexey-bass/1265022 to your computer and use it in GitHub Desktop.
Save alexey-bass/1265022 to your computer and use it in GitHub Desktop.
Zend Transaction proposal draft
<?php
/**
* Zend Transaction proposal draft.
*
* @author Alexey Bass
* @copyright Alexey Bass, 2011
* @version 2011-10-06
*/
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
require_once './Client.php';
require_once './FlightBooking_Service.php';
require_once './HotelBooking_Service.php';
require_once './CarRent_Service.php';
require_once './Zend_Transaction.php';
require_once './Zend_Transaction_Operation.php';
require_once './Zend_Transaction_Exception.php';
require_once './Zend_Transaction_Control_Exception.php';
$logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
$client = new Client();
$client->setFirstName('Alexey');
$client->setLastName('Bass');
$client->setPassportId('JUST-DO-IT');
$flightService = new FlightBooking_Service();
$hotelService = new HotelBooking_Service();
$carService = new CarRent_Service();
$operation1 = new Zend_Transaction_Operation('flight');
$operation1->setAction(array($flightService, 'register'), array($client->getFirstName(), $client->getLastName(), $client->getPassportId()));
$operation1->setPassCriteria(Zend_Transaction_Operation::RESULT_IS_NOT_EMPTY_STRING);
$operation1->setRollbackAction(array($flightService, 'unregister'), array($operation1->getActionResult()));
$operation2 = new Zend_Transaction_Operation();
$operation3->setName('hotel');
$operation2->setAction(array($hotelService, 'register'), array($client->getFirstName(), $client->getLastName(), $client->getPassportId()));
$operation2->setPassCriteria(Zend_Transaction_Operation::RESULT_IS_TRUE, function($result) {
return true;
});
$operation2->addPassCriteria(Zend_Transaction_Operation::RESULT_HAS_TIMEOUT, 10); // in seconds
$operation2->setRollbackAction(array($hotelService, 'unregister'), array($operation2->getActionResult()));
$operation3 = new Zend_Transaction_Operation(); // no name set, autogenerated from action class name
$operation3->setAction(array($carService, 'register'), array($client->getFirstName(), $client->getLastName(), $client->getPassportId()));
// no operation check needed here so no rollback defined
// same as $operation3->setPassCriteria(Zend_Transaction_Operation::RESULT_IRRELEVANT);
$transaction = new Zend_Transaction();
// inject logger if we want details about transaction proccess
$transaction->setLogger($logger);
// adding our actions
$transaction->addOperation($operation1);
$transaction->addOperation($operation2);
$transaction->addOperation($operation3);
// will rollback automatically all operations if one of operations will fail
$transaction->setAutoRollback();
try {
$transaction->execute();
} catch (Zend_Transaction_Control_Exception $ztce) {
// thrown when in manual mode, no rollbacks done
// handle rollbacks by yourself
if ($transaction->getFailedOperation()->getName() == 'hotel') {
$logger->debug('Failed with hotel booking');
}
$operations = $transaction->getExecutedOperations(); // in reverse order: car, hotel, flight
foreach ($operations as $name => $operation) {
switch ($name) {
case 'flight':
case 'hotel':
$operation->runRollbackAction(array($operation->getActionResult()));
break;
}
}
} catch (Zend_Transaction_Exception $zte) {
// oops
} catch (Zend_Exception $ze) {
// nooo
} catch (Exception $e) {
// fuck
}
if ($transaction->isCompleted() && !$transaction->hasErrors()) {
$logger->info('We are good ^_^');
} else {
$logger->err('We are screwed 8===3');
if (!$transaction->hasRollbackedSuccessfully()) {
$logger->err('We are double screwed');
$operations = $transaction->getOperations();
foreach ($operations as $name => $operation) {
if ($operation->isExecuted()) {
if ($operation->isPassed()) {
$logger->debug('Operation "'.$name.'" has passed');
} else if ($operation->isRollbacked()) {
$logger->debug('Operation "'.$name.'" not passed and not rollbacked');
}
} else {
$logger->debug('Operation "'.$name.'" was not executed');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment