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