1 | <?php |
||
10 | class Connection |
||
11 | { |
||
12 | /** |
||
13 | * @var Operation[] |
||
14 | */ |
||
15 | protected $operations = []; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $idx = 0; |
||
21 | |||
22 | /** |
||
23 | * @var int[] |
||
24 | */ |
||
25 | protected $savePoints = []; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $depth = 0; |
||
31 | |||
32 | /** |
||
33 | * @var null|string |
||
34 | */ |
||
35 | protected $connectionId; |
||
36 | |||
37 | /** |
||
38 | * Connection constructor. |
||
39 | * |
||
40 | * @param null|string $connectionId |
||
41 | * (optional) The id of the connection. |
||
42 | */ |
||
43 | 1 | public function __construct($connectionId = null) |
|
47 | |||
48 | /** |
||
49 | * Get connection id. |
||
50 | * |
||
51 | * @return null|string |
||
52 | */ |
||
53 | 1 | public function connectionId() |
|
57 | |||
58 | /** |
||
59 | * Get current depth. |
||
60 | * |
||
61 | * @return int |
||
62 | */ |
||
63 | 1 | public function getDepth() |
|
64 | { |
||
65 | 1 | return $this->depth; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Remove savepoints to and acquire index of latest active savepoint. |
||
70 | * |
||
71 | * @param int $oldDepth |
||
72 | * The old depth. |
||
73 | * @param $newDepth |
||
74 | * The new depth. |
||
75 | * |
||
76 | * @return int|null |
||
77 | * The last index, if found. |
||
78 | */ |
||
79 | 2 | protected function closeSavePoints($oldDepth, $newDepth) |
|
90 | |||
91 | /** |
||
92 | * Run commit on operations and remove them from the buffer, back to the index point specified. |
||
93 | * |
||
94 | * @param int $idx |
||
95 | * Index to commit to. |
||
96 | */ |
||
97 | 1 | protected function commitSavePoints($idx) { |
|
109 | |||
110 | /** |
||
111 | * Run rollback on operations and remove them from the buffer, back to the index point specified. |
||
112 | * |
||
113 | * @param int $idx |
||
114 | * Index to rollback to. |
||
115 | */ |
||
116 | 1 | protected function rollbackSavePoints($idx) |
|
128 | |||
129 | /** |
||
130 | * Start transaction. |
||
131 | * |
||
132 | * @param int $newDepth |
||
133 | * (optional) If specified, use as new depth, otherwise increment current depth. |
||
134 | * |
||
135 | */ |
||
136 | 1 | public function startTransaction($newDepth = null) |
|
141 | |||
142 | /** |
||
143 | * Commit transaction. |
||
144 | * |
||
145 | * @param int $newDepth |
||
146 | * (optional) If specified, use as new depth, otherwise decrement current depth. |
||
147 | * |
||
148 | */ |
||
149 | 2 | public function commitTransaction($newDepth = null) |
|
165 | |||
166 | /** |
||
167 | * Rollback transaction. |
||
168 | * |
||
169 | * @param int $newDepth |
||
170 | * (optional) If specified, use as new depth, otherwise decrement current depth. |
||
171 | * |
||
172 | */ |
||
173 | 2 | public function rollbackTransaction($newDepth = null) |
|
189 | |||
190 | /** |
||
191 | * Add operation. |
||
192 | * |
||
193 | * @param Operation $operation |
||
194 | * The operation to add to the connection. |
||
195 | * |
||
196 | * @return Operation |
||
197 | * The operation added. |
||
198 | */ |
||
199 | 1 | public function addOperation(Operation $operation) |
|
211 | |||
212 | /** |
||
213 | * Check if the connection has an operation. |
||
214 | * |
||
215 | * @param Operation $operation |
||
216 | * The operation to check for. |
||
217 | * |
||
218 | * @return bool |
||
219 | * TRUE if the operation exists. |
||
220 | */ |
||
221 | 2 | public function hasOperation(Operation $operation) |
|
225 | |||
226 | /** |
||
227 | * Remove operation. |
||
228 | * |
||
229 | * @param Operation $operation |
||
230 | * The operation to remove from the connection. |
||
231 | */ |
||
232 | 1 | public function removeOperation(Operation $operation) |
|
236 | |||
237 | /** |
||
238 | * Short-hand notation for adding code to be run on commit. |
||
239 | * |
||
240 | * @param callable $callback |
||
241 | * The code to run on commit. |
||
242 | * |
||
243 | * @return Operation |
||
244 | * The operation created. |
||
245 | */ |
||
246 | 1 | public function onCommit(callable $callback) |
|
251 | |||
252 | /** |
||
253 | * Short-hand notation for adding code to be run on rollback. |
||
254 | * |
||
255 | * @param callable $callback |
||
256 | * The code to run on rollback. |
||
257 | * |
||
258 | * @return Operation |
||
259 | * The operation created. |
||
260 | */ |
||
261 | 1 | public function onRollback(callable $callback) |
|
266 | |||
267 | /** |
||
268 | * Short-hand notation for adding code to be run on rollback. |
||
269 | * |
||
270 | * @param mixed $value |
||
271 | * The value to add. |
||
272 | * |
||
273 | * @return Operation |
||
274 | * The operation created. |
||
275 | */ |
||
276 | 1 | public function addValue($value) |
|
281 | } |
||
282 |