anboo/transaction-manager

There is no license information available for the latest version (dev-master) of this package.

Transaction manager psr

dev-master 2021-01-11 19:43 UTC

This package is auto-updated.

Last update: 2024-11-12 04:22:16 UTC


README

Transaction Manager

Basic usage

use Anboo\TransactionManager\TransactionManager;
use Anboo\TransactionManager\TransactionInterface;

$transactionManager = new TransactionManager();
$transactionManager->addTransaction(fn() => file_get_contents('http://'));

$transactionManager->addTransaction(new class implements TransactionInterface {
    public function up()
    {
        $this->remoteServiceClient->createEntity(...);
    }

    public function down()
    {
        $this->remoteServiceClient->removeEntity(...);
    }
});

$transactionManager->addTransaction(fn() => /* Database Insert */)

try {
    $transactionManager->run();
} catch (Throwable $e) {
    echo 'All completed transaction has been rollback';
}

Merge transactions

use Anboo\TransactionManager\TransactionManager;

$transactionManagerA = new TransactionManager();
$transactionManagerA->addTransaction(...);

$transactionManagerB = new TransactionManager();
$transactionManagerB->addTransaction(...);
$transactionManagerB->addTransaction(...);
$transactionManagerB->addTransaction(...);

$transactionManagerB->merge($transactionManagerA);
$transactionManagerB->run();

Ignore specific exception for transaction

use Anboo\TransactionManager\TransactionManager;

$transactionManagerA = new TransactionManager();
$transactionManagerA->addTransaction(...);
$transactionManagerA->addIgnoreException(UserAlreadyExistsException::class);

$transactionManagerA->run();