needle-project / process-transaction
Offer a transaction like support for php processes
Installs: 2 474
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: 7.*|8.*
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: 8.*|7.*|6.*
- squizlabs/php_codesniffer: 2.*
This package is auto-updated.
Last update: 2024-12-15 12:14:36 UTC
README
Transaction Process
This library helps you run your process with capabilities of rollback in case one of the processes fails! (Similar to Database Transactions).
1. Install
composer require needle-project/transactional
2. Usage
A simple usage:
<?php require_once 'vendor/autoload.php'; $paymentService = new class { public function chargeMoney() { // your logic echo "Customer has been charge!\n"; } public function refund() { echo "Customer has been refunded!\n"; } }; $stockReservationService = new class { public function reserveStock() { echo "Could not reserve stock!\n"; throw new \Exception("The trigger of failed process"); } }; $charge = new \NeedleProject\Transaction\Process( function () use ($paymentService) { return $paymentService->chargeMoney(); }, function () use ($paymentService) { return $paymentService->refund(); }, 'Payment Actions' ); $reserveStock = new \NeedleProject\Transaction\Process( function () use ($stockReservationService) { return $stockReservationService->reserveStock(); }, function () { echo "This will not be executed!\n"; }, "Stock Reserve" ); // Processing an order $executor = new \NeedleProject\Transaction\Executor(); $executor->addProcess($charge); $executor->addProcess($reserveStock); // Executing the processes try { $executor->execute(); } catch (\Exception $e) { $executor->rollBack(); } // Getting the process result echo $charge->getExecutionResult() . "\n"; echo $reserveStock->getRollBackResult() . "\n";