| Total Complexity | 7 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | trait HasDatabaseTransactions |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Determines whether class uses transaction. |
||
| 13 | */ |
||
| 14 | protected bool $useTransaction = false; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Enable transaction in pipeline. |
||
| 18 | */ |
||
| 19 | 3 | public function withTransaction(): static |
|
| 20 | { |
||
| 21 | 3 | $this->useTransaction = true; |
|
| 22 | |||
| 23 | 3 | return $this; |
|
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Begin the transaction if enabled. |
||
| 28 | */ |
||
| 29 | 31 | protected function beginTransaction(): void |
|
| 30 | { |
||
| 31 | 31 | if (! $this->useTransaction) { |
|
| 32 | 28 | return; |
|
| 33 | } |
||
| 34 | |||
| 35 | 3 | DB::beginTransaction(); |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Commit the transaction if enabled. |
||
| 40 | */ |
||
| 41 | 23 | protected function commitTransaction(): void |
|
| 42 | { |
||
| 43 | 23 | if (! $this->useTransaction) { |
|
| 44 | 22 | return; |
|
| 45 | } |
||
| 46 | |||
| 47 | 1 | DB::commit(); |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Rollback the transaction if enabled. |
||
| 52 | */ |
||
| 53 | 8 | protected function rollbackTransaction(): void |
|
| 60 | } |
||
| 61 | } |
||
| 62 |