Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| NestedTransactionWrapper | |
0.00% |
0 / 23 |
|
0.00% |
0 / 6 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| beginTransaction | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| commit | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| rollback | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| run | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| runStatement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2023 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\generis\persistence\Graph; |
| 23 | |
| 24 | use Laudis\Neo4j\Databags\Statement; |
| 25 | use Laudis\Neo4j\Databags\SummarizedResult; |
| 26 | |
| 27 | class NestedTransactionWrapper implements TransactionManagerInterface |
| 28 | { |
| 29 | private TransactionManagerInterface $nestedTransactionManager; |
| 30 | private int $transactionNestingLevel = 0; |
| 31 | private bool $isRollbackOnly = false; |
| 32 | |
| 33 | public function __construct(TransactionManagerInterface $nestedManager) |
| 34 | { |
| 35 | $this->nestedTransactionManager = $nestedManager; |
| 36 | } |
| 37 | |
| 38 | public function beginTransaction(): void |
| 39 | { |
| 40 | $this->transactionNestingLevel++; |
| 41 | |
| 42 | if ($this->transactionNestingLevel === 1) { |
| 43 | $this->nestedTransactionManager->beginTransaction(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public function commit(): void |
| 48 | { |
| 49 | if ($this->transactionNestingLevel === 0) { |
| 50 | throw new GraphTransactionException('Transaction should be started first.'); |
| 51 | } |
| 52 | |
| 53 | if ($this->isRollbackOnly) { |
| 54 | throw new GraphTransactionException( |
| 55 | 'Nested transaction failed, so all data should be rolled back now.' |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | if ($this->transactionNestingLevel === 1) { |
| 60 | $this->nestedTransactionManager->commit(); |
| 61 | } |
| 62 | |
| 63 | $this->transactionNestingLevel--; |
| 64 | } |
| 65 | |
| 66 | public function rollback(): void |
| 67 | { |
| 68 | if ($this->transactionNestingLevel === 0) { |
| 69 | throw new GraphTransactionException('Transaction should be started first.'); |
| 70 | } |
| 71 | |
| 72 | if ($this->transactionNestingLevel === 1) { |
| 73 | $this->nestedTransactionManager->rollBack(); |
| 74 | $this->isRollbackOnly = false; |
| 75 | } else { |
| 76 | $this->isRollbackOnly = true; |
| 77 | } |
| 78 | |
| 79 | $this->transactionNestingLevel--; |
| 80 | } |
| 81 | |
| 82 | public function run(string $statement, iterable $parameters = []): SummarizedResult |
| 83 | { |
| 84 | return $this->nestedTransactionManager->run($statement, $parameters); |
| 85 | } |
| 86 | |
| 87 | public function runStatement(Statement $statement): SummarizedResult |
| 88 | { |
| 89 | return $this->nestedTransactionManager->runStatement($statement); |
| 90 | } |
| 91 | } |