for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Quantum\Libraries\Database\Traits;
use Quantum\Libraries\Database\Exceptions\DatabaseException;
use Quantum\App\Exceptions\BaseException;
use Throwable;
/**
* Trait TransactionTrait
* @package Quantum\Libraries\Database
*/
trait TransactionTrait
{
* Begins a transaction
* @throws BaseException
public static function beginTransaction(): void
self::resolveTransaction(__FUNCTION__);
}
* Commits a transaction
* @throws DatabaseException
public static function commit(): void
* Rolls back a transaction
public static function rollback(): void
* Resolves the transaction method call
* @param string $method
* @return mixed
protected static function resolveTransaction(string $method)
$db = self::getInstance()->getOrmClass();
if (!method_exists($db, $method)) {
throw DatabaseException::methodNotSupported($method, __CLASS__);
return $db::$method();
* Transaction wrapper using a closure
* @param callable $callback
* @throws Throwable
public static function transaction(callable $callback)
self::beginTransaction();
try {
$result = $callback();
self::commit();
return $result;
} catch (Throwable $e) {
self::rollback();
throw $e;