Total Complexity | 7 |
Total Lines | 67 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
13 | trait TransactionTrait |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * Begins a transaction |
||
18 | * @throws BaseException |
||
19 | */ |
||
20 | public static function beginTransaction(): void |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Commits a transaction |
||
27 | * @throws BaseException |
||
28 | * @throws DatabaseException |
||
29 | */ |
||
30 | public static function commit(): void |
||
31 | { |
||
32 | self::resolveTransaction(__FUNCTION__); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Rolls back a transaction |
||
37 | * @throws BaseException |
||
38 | */ |
||
39 | public static function rollback(): void |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Resolves the transaction method call |
||
46 | * @param string $method |
||
47 | * @return mixed |
||
48 | * @throws BaseException |
||
49 | */ |
||
50 | protected static function resolveTransaction(string $method) |
||
51 | { |
||
52 | $db = self::getInstance()->getOrmClass(); |
||
53 | |||
54 | if (!method_exists($db, $method)) { |
||
55 | throw DatabaseException::methodNotSupported($method, __CLASS__); |
||
56 | } |
||
57 | |||
58 | return $db::$method(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Transaction wrapper using a closure |
||
63 | * @param callable $callback |
||
64 | * @return mixed |
||
65 | * @throws BaseException |
||
66 | * @throws DatabaseException |
||
67 | * @throws Throwable |
||
68 | */ |
||
69 | public static function transaction(callable $callback) |
||
80 | } |
||
81 | } |
||
82 | } |