needle-project/process-transaction

Offer a transaction like support for php processes

v0.1.0 2024-01-15 10:08 UTC

This package is auto-updated.

Last update: 2024-12-15 12:14:36 UTC


README

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