Skip to content

Commit

Permalink
Add functionality to wrap closure into transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
waska14 committed Mar 18, 2023
1 parent c773b03 commit f37b7c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
46 changes: 38 additions & 8 deletions src/Helpers/WithDBTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Throwable;
use Waska\LaravelWithDBTransactions\Exceptions\InvalidCallableParameterException;
use Waska\LaravelWithDBTransactions\Exceptions\MiddlewareIsNotPassedException;

Expand Down Expand Up @@ -48,6 +49,35 @@ public static function startMiddleware()
self::$middlewareStarted = true;
}

/**
* Wraps your closure into transaction
* Note: this function uses default DB::transaction, but you have possibility to listen events like
* before/after commit/rollback/beginTransaction or event set closures for them.
*
* @param callable $closure
* @param int $attempts
* @return mixed|null
* @throws Throwable
*/
public static function transaction(callable $closure, int $attempts = 1)
{
self::startMiddleware();
do {
self::beginTransaction();
try {
$result = $closure();
self::commit();
return $result;
} catch (Throwable $e) {
self::rollback(null, $attempts - 1 <= 0);
if ($attempts - 1 <= 0) {
throw $e;
}
}
} while (--$attempts > 0);
return null;
}

public static function stopMiddleware()
{
self::$middlewareStarted = false;
Expand All @@ -61,10 +91,10 @@ public static function isMiddlewareStarted(): bool
/**
* Begin transaction and do before and after stuff.
*
* @param Request $request
* @param Request|null $request
* @return void
*/
public static function beginTransaction($request)
public static function beginTransaction(Request $request = null)
{
self::$closures = [];
self::$currentAttempt++;
Expand All @@ -76,10 +106,10 @@ public static function beginTransaction($request)
/**
* Commit transaction and do before and after stuff.
*
* @param Request $request
* @param Request|null $request
* @return void
*/
public static function commit($request)
public static function commit(Request $request = null)
{
self::execute('before_commit');
self::event('before_commit_event', $request);
Expand All @@ -91,11 +121,11 @@ public static function commit($request)
/**
* Rollback transaction and do before and after stuff.
*
* @param Request $request
* @param Request|null $request
* @param bool $latest
* @return void
*/
public static function rollback($request, bool $latest = true)
public static function rollback(Request $request = null, bool $latest = true)
{
$method = $latest ? 'rollback' : 'every_rollback';
self::execute('before_' . $method);
Expand All @@ -119,7 +149,7 @@ protected static function execute(string $name)
}

/**
* This function dispatches an event depending event $key
* This function dispatches an event depending on event $key
*
* @param string $key
* @param $request
Expand All @@ -129,4 +159,4 @@ protected static function event(string $key, $request)
$class = config('waska.with_db_transactions.' . $key);
event(new $class($request, self::$currentAttempt));
}
}
}
6 changes: 3 additions & 3 deletions src/Traits/BaseEventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ trait BaseEventTrait
/**
* Create a new event instance.
*
* @param Request $request
* @param Request|null $request
* @param int $currentAttempt
*/
public function __construct($request, int $currentAttempt)
public function __construct(Request $request = null, int $currentAttempt = 1)
{
$this->request = $request;
$this->currentAttempt = $currentAttempt;
}

/**
* @return Request
* @return Request|null
*/
public function getRequest()
{
Expand Down

0 comments on commit f37b7c5

Please sign in to comment.