1 | <?php |
||
10 | class Connection |
||
11 | { |
||
12 | /** |
||
13 | * Operations buffer. |
||
14 | * |
||
15 | * @var Operation[] |
||
16 | */ |
||
17 | protected $operations = []; |
||
18 | |||
19 | /** |
||
20 | * Current index in operations buffer. |
||
21 | * |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $idx = 0; |
||
25 | |||
26 | /** |
||
27 | * Save points in the buffer, indexed by depth. |
||
28 | * |
||
29 | * @var int[] |
||
30 | */ |
||
31 | protected $savePoints = []; |
||
32 | |||
33 | /** |
||
34 | * Current depth. |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $depth = 0; |
||
39 | |||
40 | /** |
||
41 | * The id of the connection. |
||
42 | * |
||
43 | * @var null|string |
||
44 | */ |
||
45 | protected $connectionId; |
||
46 | |||
47 | /** |
||
48 | * Connection constructor. |
||
49 | * |
||
50 | * @param null|string $connectionId |
||
51 | * (optional) The id of the connection. |
||
52 | */ |
||
53 | 1 | public function __construct($connectionId = null) |
|
57 | |||
58 | /** |
||
59 | * Get connection id. |
||
60 | * |
||
61 | * @return null|string |
||
62 | */ |
||
63 | 9 | public function connectionId() |
|
67 | |||
68 | /** |
||
69 | * Get current depth. |
||
70 | * |
||
71 | * @return int |
||
72 | */ |
||
73 | 2 | public function getDepth() |
|
77 | |||
78 | /** |
||
79 | * Set current depth. |
||
80 | * |
||
81 | * @param int $newDepth |
||
82 | * The new depth. |
||
83 | * |
||
84 | * @return int |
||
85 | * The old depth. |
||
86 | */ |
||
87 | 11 | public function setDepth($newDepth) |
|
96 | |||
97 | |||
98 | /** |
||
99 | * Add operation. |
||
100 | * |
||
101 | * @param Operation $operation |
||
102 | * The operation to add to the connection. |
||
103 | * |
||
104 | * @return Operation |
||
105 | * The operation added. |
||
106 | */ |
||
107 | 9 | public function addOperation(Operation $operation) |
|
120 | |||
121 | /** |
||
122 | * Check if the connection has an operation. |
||
123 | * |
||
124 | * @param Operation $operation |
||
125 | * The operation to check for. |
||
126 | * |
||
127 | * @return bool |
||
128 | * TRUE if the operation exists. |
||
129 | */ |
||
130 | 4 | public function hasOperation(Operation $operation) |
|
134 | |||
135 | /** |
||
136 | * Remove operation. |
||
137 | * |
||
138 | * @param Operation $operation |
||
139 | * The operation to remove from the connection. |
||
140 | */ |
||
141 | 7 | public function removeOperation(Operation $operation) |
|
146 | |||
147 | /** |
||
148 | * Short-hand notation for adding code to be run on commit. |
||
149 | * |
||
150 | * @param callable $callback |
||
151 | * The code to run on commit. |
||
152 | * |
||
153 | * @return Operation |
||
154 | * The operation created. |
||
155 | */ |
||
156 | 1 | public function onCommit(callable $callback) |
|
161 | |||
162 | /** |
||
163 | * Short-hand notation for adding code to be run on rollback. |
||
164 | * |
||
165 | * @param callable $callback |
||
166 | * The code to run on rollback. |
||
167 | * |
||
168 | * @return Operation |
||
169 | * The operation created. |
||
170 | */ |
||
171 | 1 | public function onRollback(callable $callback) |
|
176 | |||
177 | /** |
||
178 | * Short-hand notation for adding code to be run when removed from buffer. |
||
179 | * |
||
180 | * @param callable $callback |
||
181 | * The code to run on removal from buffer. |
||
182 | * |
||
183 | * @return Operation |
||
184 | * The operation created. |
||
185 | */ |
||
186 | 1 | public function onRemove(callable $callback) |
|
191 | |||
192 | /** |
||
193 | * Short-hand notation for adding metadata to operation. |
||
194 | * |
||
195 | * @param string $key |
||
196 | * The key of the metadata to add. |
||
197 | * @param mixed $value |
||
198 | * The value to add. |
||
199 | * |
||
200 | * @return Operation |
||
201 | * The operation created. |
||
202 | */ |
||
203 | 1 | public function addMetadata($key, $value) |
|
208 | |||
209 | /** |
||
210 | * Start transaction. |
||
211 | * |
||
212 | * @param int $newDepth |
||
213 | * (optional) If specified, use as new depth, otherwise increment current depth. |
||
214 | * |
||
215 | */ |
||
216 | 11 | public function startTransaction($newDepth = null) |
|
221 | |||
222 | /** |
||
223 | * Commit transaction. |
||
224 | * |
||
225 | * @param int $newDepth |
||
226 | * (optional) If specified, use as new depth, otherwise decrement current depth. |
||
227 | * |
||
228 | */ |
||
229 | 6 | public function commitTransaction($newDepth = null) |
|
242 | |||
243 | /** |
||
244 | * Rollback transaction. |
||
245 | * |
||
246 | * @param int $newDepth |
||
247 | * (optional) If specified, use as new depth, otherwise decrement current depth. |
||
248 | * |
||
249 | */ |
||
250 | 6 | public function rollbackTransaction($newDepth = null) |
|
259 | |||
260 | /** |
||
261 | * Remove save points to and acquire index of latest active save point. |
||
262 | * |
||
263 | * @param int $oldDepth |
||
264 | * The old depth. |
||
265 | * @param int $newDepth |
||
266 | * The new depth. |
||
267 | * |
||
268 | * @return int |
||
269 | * The index of the latest active save point. |
||
270 | */ |
||
271 | 9 | protected function closeSavePoints($oldDepth, $newDepth) |
|
282 | |||
283 | /** |
||
284 | * Collection operations from the specified index. |
||
285 | * |
||
286 | * @param int $idx |
||
287 | * The starting index. |
||
288 | * |
||
289 | * @return Operation[] |
||
290 | * The operations from the specified index (included) |
||
291 | */ |
||
292 | 9 | protected function collectOperations($idx) |
|
306 | |||
307 | /** |
||
308 | * Run commit on operations and remove them from the buffer. |
||
309 | * |
||
310 | * @param Operation[] $operations |
||
311 | * The operations to commit. |
||
312 | */ |
||
313 | 6 | protected function commitOperations(array $operations) |
|
320 | |||
321 | /** |
||
322 | * Run rollback on operations and remove them from the buffer. |
||
323 | * |
||
324 | * @param Operation[] $operations |
||
325 | * The operations to rollback. |
||
326 | */ |
||
327 | 6 | protected function rollbackOperations(array $operations) |
|
334 | } |
||
335 |