| Total Complexity | 43 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ManagesTransactions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ManagesTransactions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait ManagesTransactions |
||
| 11 | { |
||
| 12 | protected $transactions = 0; |
||
| 13 | |||
| 14 | protected $transactionCommands = []; |
||
| 15 | |||
| 16 | protected $arangoTransaction; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Execute a Closure within a transaction. |
||
| 20 | * |
||
| 21 | * @param \Closure $callback |
||
| 22 | * @param array $options |
||
| 23 | * @param int $attempts |
||
| 24 | * |
||
| 25 | * @throws \Exception|\Throwable |
||
| 26 | * |
||
| 27 | * @return mixed |
||
| 28 | */ |
||
| 29 | public function transaction(Closure $callback, $options = [], $attempts = 1) |
||
| 30 | { |
||
| 31 | $this->beginTransaction(); |
||
| 32 | |||
| 33 | return tap($callback($this), function () use ($options, $attempts) { |
||
| 34 | $this->commit($options, $attempts); |
||
| 35 | }); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Start a new database transaction. |
||
| 40 | * |
||
| 41 | * @throws \Exception |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function beginTransaction() |
||
| 46 | { |
||
| 47 | $this->transactions++; |
||
| 48 | |||
| 49 | $this->transactionCommands[$this->transactions] = []; |
||
| 50 | |||
| 51 | $this->fireConnectionEvent('beganTransaction'); |
||
|
|
|||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add a command to the transaction. Parameters must include: |
||
| 56 | * collections['write'][]: collections that are written to |
||
| 57 | * collections['read'][]: collections that are read from |
||
| 58 | * command: the db command to execute. |
||
| 59 | * |
||
| 60 | * @param \Illuminate\Support\Fluent $command |
||
| 61 | */ |
||
| 62 | public function addTransactionCommand(IlluminateFluent $command) |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Add a query command to the transaction. |
||
| 69 | * |
||
| 70 | * @param $query |
||
| 71 | * @param $bindings |
||
| 72 | * @param array|null $collections |
||
| 73 | * |
||
| 74 | * @return IlluminateFluent |
||
| 75 | */ |
||
| 76 | public function addQueryToTransaction($query, $bindings = [], $collections = null) |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Transaction like a list of read collections to prevent possible read deadlocks. |
||
| 103 | * Transactions require a list of write collections to prepare write locks. |
||
| 104 | * |
||
| 105 | * @param $query |
||
| 106 | * @param $bindings |
||
| 107 | * @param $collections |
||
| 108 | * |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | public function extractTransactionCollections($query, $bindings, $collections) |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Extract collections that are read from in a query. Not required but can prevent deadlocks. |
||
| 122 | * |
||
| 123 | * @param $query |
||
| 124 | * @param $bindings |
||
| 125 | * @param $collections |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | public function extractReadCollections($query, $bindings, $collections) |
||
| 130 | { |
||
| 131 | $extractedCollections = []; |
||
| 132 | $rawWithCollections = []; |
||
| 133 | $rawForCollections = []; |
||
| 134 | $rawDocCollections = []; |
||
| 135 | |||
| 136 | //WITH statement at the start of the query |
||
| 137 | preg_match_all('/^(?:\s+?)WITH(?:\s+?)([\S\s]*?)(?:\s+?)FOR/mis', $query, $rawWithCollections); |
||
| 138 | foreach ($rawWithCollections[1] as $value) { |
||
| 139 | $splits = preg_split("/\s*,\s*/", $value); |
||
| 140 | $extractedCollections = array_merge($extractedCollections, $splits); |
||
| 141 | } |
||
| 142 | |||
| 143 | //FOR statements |
||
| 144 | preg_match_all( |
||
| 145 | '/FOR(?:\s+?)(?:\w+)(?:\s+?)(?:IN|INTO)(?:\s+?)(?!OUTBOUND|INBOUND|ANY)(@?@?\w+(?!\.))/mis', |
||
| 146 | $query, |
||
| 147 | $rawForCollections |
||
| 148 | ); |
||
| 149 | $extractedCollections = array_merge($extractedCollections, $rawForCollections[1]); |
||
| 150 | |||
| 151 | //Document functions which require a document as their first argument |
||
| 152 | preg_match_all( |
||
| 153 | '/(?:DOCUMENT\(|ATTRIBUTES\(|HAS\(|KEEP\(|LENGTH\(|MATCHES' |
||
| 154 | . '\(|PARSE_IDENTIFIER\(|UNSET\(|UNSET_RECURSIVE\(|VALUES\(|OUTBOUND|INBOUND|ANY)' |
||
| 155 | . '(?:\s+?)(?!\{)(?:\"|\'|\`)(@?@?\w+)\/(?:\w+)(?:\"|\'|\`)/mis', |
||
| 156 | $query, |
||
| 157 | $rawDocCollections |
||
| 158 | ); |
||
| 159 | $extractedCollections = array_merge($extractedCollections, $rawDocCollections[1]); |
||
| 160 | |||
| 161 | $extractedCollections = array_map('trim', $extractedCollections); |
||
| 162 | |||
| 163 | $extractedCollections = $this->getCollectionByBinding($extractedCollections, $bindings); |
||
| 164 | |||
| 165 | if (isset($collections['read'])) { |
||
| 166 | $collections['read'] = array_merge($collections['read'], $extractedCollections); |
||
| 167 | } |
||
| 168 | if (! isset($collections['read'])) { |
||
| 169 | $collections['read'] = $extractedCollections; |
||
| 170 | } |
||
| 171 | |||
| 172 | $collections['read'] = array_unique($collections['read']); |
||
| 173 | |||
| 174 | return $collections; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Extract collections that are written to in a query. |
||
| 179 | * |
||
| 180 | * @param $query |
||
| 181 | * @param $bindings |
||
| 182 | * @param $collections |
||
| 183 | * |
||
| 184 | * @return mixed |
||
| 185 | */ |
||
| 186 | public function extractWriteCollections($query, $bindings, $collections) |
||
| 187 | { |
||
| 188 | preg_match_all( |
||
| 189 | '/(?:\s+?)(?:INSERT|REPLACE|UPDATE|REMOVE)' |
||
| 190 | . '(?:\s+?)(?:{(?:.*?)}|@?@?\w+?)(?:\s+?)(?:IN|INTO)(?:\s+?)(@?@?\w+)/mis', |
||
| 191 | $query, |
||
| 192 | $extractedCollections |
||
| 193 | ); |
||
| 194 | $extractedCollections = array_map('trim', $extractedCollections[1]); |
||
| 195 | |||
| 196 | $extractedCollections = $this->getCollectionByBinding($extractedCollections, $bindings); |
||
| 197 | |||
| 198 | if (isset($collections['write'])) { |
||
| 199 | $collections['write'] = array_merge($collections['write'], $extractedCollections); |
||
| 200 | } |
||
| 201 | if (! isset($collections['write'])) { |
||
| 202 | $collections['write'] = $extractedCollections; |
||
| 203 | } |
||
| 204 | |||
| 205 | $collections['read'] = array_unique($collections['read']); |
||
| 206 | |||
| 207 | return $collections; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the collection names that are bound in a query. |
||
| 212 | * |
||
| 213 | * @param $collections |
||
| 214 | * @param $bindings |
||
| 215 | * |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | public function getCollectionByBinding($collections, $bindings) |
||
| 219 | { |
||
| 220 | foreach ($collections as $key => $collection) { |
||
| 221 | if (strpos($collection, '@@') === 0 && isset($bindings[$collection])) { |
||
| 222 | $collections[$key] = $bindings[$collection]; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | return $collections; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Commit the current transaction. |
||
| 231 | * |
||
| 232 | * @param array $options |
||
| 233 | * @param int $attempts |
||
| 234 | * |
||
| 235 | * @throws Exception |
||
| 236 | * |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | public function commit($options = [], $attempts = 1) |
||
| 240 | { |
||
| 241 | if (!$this->transactions > 0) { |
||
| 242 | throw new Exception('Transaction committed before starting one.'); |
||
| 243 | } |
||
| 244 | if ( |
||
| 245 | !isset($this->transactionCommands[$this->transactions]) |
||
| 246 | || empty($this->transactionCommands[$this->transactions]) |
||
| 247 | ) { |
||
| 248 | throw new Exception('Cannot commit an empty transaction.'); |
||
| 249 | } |
||
| 250 | |||
| 251 | $options['collections'] = $this->compileTransactionCollections(); |
||
| 252 | |||
| 253 | $options['action'] = $this->compileTransactionAction(); |
||
| 254 | |||
| 255 | $results = $this->executeTransaction($options, $attempts); |
||
| 256 | |||
| 257 | $this->fireConnectionEvent('committed'); |
||
| 258 | |||
| 259 | return $results; |
||
| 260 | } |
||
| 261 | |||
| 262 | public function executeTransaction($options, $attempts = 1) |
||
| 263 | { |
||
| 264 | $results = null; |
||
| 265 | |||
| 266 | $this->arangoTransaction = new ArangoTransaction($this->arangoConnection, $options); |
||
| 267 | |||
| 268 | for ($currentAttempt = 1; $currentAttempt <= $attempts; $currentAttempt++) { |
||
| 269 | try { |
||
| 270 | $results = $this->arangoTransaction->execute(); |
||
| 271 | |||
| 272 | $this->transactions--; |
||
| 273 | } catch (Exception $e) { |
||
| 274 | $this->fireConnectionEvent('rollingBack'); |
||
| 275 | |||
| 276 | $results = $this->handleTransactionException($e, $currentAttempt, $attempts); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | return $results; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Handle an exception encountered when running a transacted statement. |
||
| 285 | * |
||
| 286 | * @param $e |
||
| 287 | * @param $currentAttempt |
||
| 288 | * @param $attempts |
||
| 289 | * |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | protected function handleTransactionException($e, $currentAttempt, $attempts) |
||
| 293 | { |
||
| 294 | $retry = false; |
||
| 295 | // If the failure was due to a lost connection we can just try again. |
||
| 296 | if ($this->causedByLostConnection($e)) { |
||
| 297 | $this->reconnect(); |
||
| 298 | $retry = true; |
||
| 299 | } |
||
| 300 | |||
| 301 | // Retry if the failure was caused by a deadlock or ArangoDB suggests we try so. |
||
| 302 | // We can check if we have exceeded the maximum attempt count for this and if |
||
| 303 | // we haven't we will return and try this transaction again. |
||
| 304 | if ( |
||
| 305 | $this->causedByDeadlock($e) && |
||
| 306 | $currentAttempt < $attempts |
||
| 307 | ) { |
||
| 308 | $retry = true; |
||
| 309 | } |
||
| 310 | |||
| 311 | if ($retry) { |
||
| 312 | return $this->arangoTransaction->execute(); |
||
| 313 | } |
||
| 314 | |||
| 315 | throw $e; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * compile an array of unique collections that are used to read from and/or write to. |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | public function compileTransactionCollections() |
||
| 324 | { |
||
| 325 | $result['write'] = []; |
||
| 326 | $result['read'] = []; |
||
| 327 | |||
| 328 | $commands = $this->transactionCommands[$this->transactions]; |
||
| 329 | |||
| 330 | foreach ($commands as $command) { |
||
| 331 | if (isset($command->collections['write'])) { |
||
| 332 | $write = $command->collections['write']; |
||
| 333 | if (is_string($write)) { |
||
| 334 | $write = (array) $write; |
||
| 335 | } |
||
| 336 | $result['write'] = array_merge($result['write'], $write); |
||
| 337 | } |
||
| 338 | if (isset($command->collections['read'])) { |
||
| 339 | $read = $command->collections['read']; |
||
| 340 | if (is_string($read)) { |
||
| 341 | $read = (array) $read; |
||
| 342 | } |
||
| 343 | $result['read'] = array_merge($result['write'], $read); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | $result['read'] = array_merge($result['read'], $result['write']); |
||
| 348 | |||
| 349 | $result['write'] = array_filter(array_unique($result['write'])); |
||
| 350 | if (empty($result['write'])) { |
||
| 351 | unset($result['write']); |
||
| 352 | } |
||
| 353 | |||
| 354 | $result['read'] = array_filter(array_unique($result['read'])); |
||
| 355 | if (empty($result['read'])) { |
||
| 356 | unset($result['read']); |
||
| 357 | } |
||
| 358 | |||
| 359 | $result = array_filter($result); |
||
| 360 | |||
| 361 | return $result; |
||
| 362 | } |
||
| 363 | |||
| 364 | public function compileTransactionAction() |
||
| 365 | { |
||
| 366 | $commands = collect($this->transactionCommands[$this->transactions]); |
||
| 367 | |||
| 368 | $action = "function () { var db = require('@arangodb').db; "; |
||
| 369 | $action .= $commands->implode('command', ' '); |
||
| 370 | $action .= ' }'; |
||
| 371 | |||
| 372 | return $action; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Handle an exception from a rollback. |
||
| 377 | * |
||
| 378 | * @param \Exception $e |
||
| 379 | * |
||
| 380 | * @throws \Exception |
||
| 381 | */ |
||
| 382 | protected function handleRollBackException($e) |
||
| 383 | { |
||
| 384 | if ($this->causedByLostConnection($e)) { |
||
| 385 | $this->transactions = 0; |
||
| 386 | } |
||
| 387 | |||
| 388 | throw $e; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get the number of active transactions. |
||
| 393 | * |
||
| 394 | * @return int |
||
| 395 | */ |
||
| 396 | public function transactionLevel() |
||
| 397 | { |
||
| 398 | return $this->transactions; |
||
| 399 | } |
||
| 400 | |||
| 401 | public function getTransactionCommands() |
||
| 404 | } |
||
| 405 | |||
| 406 | //Override unused trait transaction functions with dummy methods |
||
| 407 | |||
| 408 | // /** |
||
| 409 | // * Dummy. |
||
| 410 | // * |
||
| 411 | // * @param $e |
||
| 412 | // */ |
||
| 413 | // public function handleBeginTransactionException($e) |
||
| 414 | // { |
||
| 455 |