1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Concerns; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use LaravelFreelancerNL\Aranguent\Exceptions\NoArangoClientException; |
9
|
|
|
use Throwable; |
10
|
|
|
|
11
|
|
|
trait ManagesTransactions |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Execute a Closure within a transaction. |
15
|
|
|
* |
16
|
|
|
* @param \Closure $callback |
17
|
|
|
* @param int $attempts |
18
|
|
|
* @param array<mixed> $collections |
19
|
|
|
* |
20
|
|
|
* @return mixed |
21
|
|
|
* |
22
|
|
|
* @throws \Throwable |
23
|
|
|
*/ |
24
|
10 |
|
public function transaction(Closure $callback, $attempts = 1, array $collections = []) |
25
|
|
|
{ |
26
|
10 |
|
for ($currentAttempt = 1; $currentAttempt <= $attempts; $currentAttempt++) { |
27
|
10 |
|
$this->beginTransaction($collections); |
28
|
|
|
|
29
|
|
|
// We'll simply execute the given callback within a try / catch block and if we |
30
|
|
|
// catch any exception we can rollback this transaction so that none of this |
31
|
|
|
// gets actually persisted to a database or stored in a permanent fashion. |
32
|
|
|
try { |
33
|
10 |
|
$callbackResult = $callback($this); |
34
|
2 |
|
} catch (Throwable $e) { |
35
|
|
|
// If we catch an exception we'll rollback this transaction and try again if we |
36
|
|
|
// are not out of attempts. If we are out of attempts we will just throw the |
37
|
|
|
// exception back out and let the developer handle an uncaught exceptions. |
38
|
|
|
|
39
|
2 |
|
$this->handleTransactionException( |
|
|
|
|
40
|
2 |
|
$e, |
41
|
2 |
|
$currentAttempt, |
42
|
2 |
|
$attempts, |
43
|
2 |
|
); |
44
|
|
|
|
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
try { |
49
|
10 |
|
if ($this->transactions == 1) { |
50
|
1 |
|
$this->commit(); |
51
|
|
|
} |
52
|
|
|
|
53
|
10 |
|
$this->transactions = max(0, $this->transactions - 1); |
|
|
|
|
54
|
|
|
} catch (Throwable $e) { |
55
|
|
|
$this->handleCommitTransactionException( |
|
|
|
|
56
|
|
|
$e, |
57
|
|
|
$currentAttempt, |
58
|
|
|
$attempts, |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
10 |
|
$this->fireConnectionEvent('committed'); |
|
|
|
|
65
|
|
|
|
66
|
10 |
|
return $callbackResult; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Start a new database transaction. |
72
|
|
|
* |
73
|
|
|
* @param array<string, array<string>> $collections |
74
|
|
|
* |
75
|
|
|
* @throws Throwable |
76
|
|
|
*/ |
77
|
159 |
|
public function beginTransaction(array $collections = []): void |
78
|
|
|
{ |
79
|
159 |
|
$this->createTransaction($collections); |
80
|
|
|
|
81
|
159 |
|
$this->transactions++; |
82
|
|
|
|
83
|
159 |
|
$this->fireConnectionEvent('beganTransaction'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create a transaction within the database. |
88
|
|
|
* |
89
|
|
|
* @param array<string, array<string>> $collections |
90
|
|
|
* |
91
|
|
|
* @throws Throwable |
92
|
|
|
*/ |
93
|
159 |
|
protected function createTransaction(array $collections = []): void |
94
|
|
|
{ |
95
|
159 |
|
if ($this->transactions == 0) { |
96
|
159 |
|
$this->reconnectIfMissingConnection(); |
|
|
|
|
97
|
|
|
|
98
|
|
|
try { |
99
|
159 |
|
if ($this->arangoClient === null) { |
100
|
|
|
throw new NoArangoClientException(); |
101
|
|
|
} |
102
|
159 |
|
$this->arangoClient->begin($collections); |
103
|
|
|
} catch (Throwable $e) { |
104
|
|
|
$this->handleBeginTransactionException($e); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Commit the active database transaction. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* |
114
|
|
|
* @throws Throwable |
115
|
|
|
*/ |
116
|
3 |
|
public function commit() |
117
|
|
|
{ |
118
|
3 |
|
if ($this->arangoClient === null) { |
119
|
|
|
throw new NoArangoClientException(); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
if ($this->transactions == 1) { |
123
|
3 |
|
$this->arangoClient->commit(); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
$this->transactions = max(0, $this->transactions - 1); |
|
|
|
|
127
|
|
|
|
128
|
3 |
|
$this->fireConnectionEvent('committed'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Rollback the active database transaction. |
133
|
|
|
* |
134
|
|
|
* @param int|null $toLevel |
135
|
|
|
* @return void |
136
|
|
|
* |
137
|
|
|
* @throws \Throwable |
138
|
|
|
*/ |
139
|
156 |
|
public function rollBack($toLevel = null) |
140
|
|
|
{ |
141
|
|
|
// We allow developers to rollback to a certain transaction level. We will verify |
142
|
|
|
// that this given transaction level is valid before attempting to rollback to |
143
|
|
|
// that level. If it's not we will just return out and not attempt anything. |
144
|
156 |
|
$toLevel = is_null($toLevel) |
145
|
156 |
|
? $this->transactions - 1 |
146
|
2 |
|
: $toLevel; |
147
|
|
|
|
148
|
156 |
|
if ($toLevel < 0 || $toLevel >= $this->transactions) { |
149
|
2 |
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Next, we will actually perform this rollback within this database and fire the |
153
|
|
|
// rollback event. We will also set the current transaction level to the given |
154
|
|
|
// level that was passed into this method so it will be right from here out. |
155
|
|
|
try { |
156
|
156 |
|
$this->performRollBack($toLevel); |
157
|
|
|
} catch (Throwable $e) { |
158
|
|
|
$this->handleRollBackException($e); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
156 |
|
$this->transactions = $toLevel; |
|
|
|
|
162
|
|
|
|
163
|
156 |
|
$this->fireConnectionEvent('rollingBack'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Perform a rollback within the database. |
168
|
|
|
* |
169
|
|
|
* @param int $toLevel |
170
|
|
|
* @return void |
171
|
|
|
* |
172
|
|
|
* @throws Throwable |
173
|
|
|
*/ |
174
|
156 |
|
protected function performRollBack($toLevel) |
175
|
|
|
{ |
176
|
156 |
|
if ($this->arangoClient === null) { |
177
|
|
|
throw new NoArangoClientException(); |
178
|
|
|
} |
179
|
|
|
|
180
|
156 |
|
if ($toLevel == 0) { |
181
|
156 |
|
$this->arangoClient->abort(); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|