Skip to content

A simple method of handling transactional processes in PHP (including transactions over distributed systems)

License

Notifications You must be signed in to change notification settings

needle-project/process-transaction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Scrutinizer Code Quality Code Coverage

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";

About

A simple method of handling transactional processes in PHP (including transactions over distributed systems)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages