|
11 | 11 | use Neo4j\Neo4jBundle\Event\FailureEvent;
|
12 | 12 | use Neo4j\Neo4jBundle\Event\PostRunEvent;
|
13 | 13 | use Neo4j\Neo4jBundle\Event\PreRunEvent;
|
14 |
| -use Neo4j\Neo4jBundle\Event\TransactionEvent; |
| 14 | +use Neo4j\Neo4jBundle\Event\Transaction\PostTransactionBeginEvent; |
| 15 | +use Neo4j\Neo4jBundle\Event\Transaction\PostTransactionCommitEvent; |
| 16 | +use Neo4j\Neo4jBundle\Event\Transaction\PostTransactionRollbackEvent; |
| 17 | +use Neo4j\Neo4jBundle\Event\Transaction\PreTransactionBeginEvent; |
| 18 | +use Neo4j\Neo4jBundle\Event\Transaction\PreTransactionCommitEvent; |
| 19 | +use Neo4j\Neo4jBundle\Event\Transaction\PreTransactionRollbackEvent; |
15 | 20 | use Neo4j\Neo4jBundle\Factories\StopwatchEventNameFactory;
|
16 | 21 | use Symfony\Component\Stopwatch\Stopwatch;
|
17 | 22 | use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
@@ -92,16 +97,21 @@ public function handleTransactionAction(
|
92 | 97 | ): mixed {
|
93 | 98 | $stopWatchName = $this->nameFactory->createTransactionEventName($alias, $transactionId, $nextTransactionState);
|
94 | 99 |
|
95 |
| - if (TransactionState::ACTIVE === $nextTransactionState) { |
96 |
| - $this->dispatchTransactionEvent($alias, $scheme, $transactionId); |
97 |
| - } |
| 100 | + [ |
| 101 | + 'preEvent' => $preEvent, |
| 102 | + 'preEventId' => $preEventId, |
| 103 | + 'postEvent' => $postEvent, |
| 104 | + 'postEventId' => $postEventId, |
| 105 | + ] = $this->createPreAndPostEventsAndIds( |
| 106 | + nextTransactionState: $nextTransactionState, |
| 107 | + alias: $alias, |
| 108 | + scheme: $scheme, |
| 109 | + transactionId: $transactionId |
| 110 | + ); |
98 | 111 |
|
| 112 | + $this->dispatcher?->dispatch($preEvent, $preEventId); |
99 | 113 | $result = $this->handleAction(runHandler: $runHandler, alias: $alias, scheme: $scheme, stopwatchName: $stopWatchName, transactionId: $transactionId, statement: null);
|
100 |
| - |
101 |
| - if (TransactionState::COMMITTED === $nextTransactionState |
102 |
| - || TransactionState::ROLLED_BACK === $nextTransactionState) { |
103 |
| - $this->dispatchTransactionEvent($alias, $scheme, $transactionId); |
104 |
| - } |
| 114 | + $this->dispatcher?->dispatch($postEvent, $postEventId); |
105 | 115 |
|
106 | 116 | return $result;
|
107 | 117 | }
|
@@ -138,15 +148,79 @@ private function handleAction(callable $runHandler, string $alias, string $schem
|
138 | 148 | }
|
139 | 149 | }
|
140 | 150 |
|
141 |
| - private function dispatchTransactionEvent(?string $alias, string $scheme, string $transactionId): void |
142 |
| - { |
143 |
| - $event = new TransactionEvent( |
144 |
| - alias: $alias, |
145 |
| - time: new \DateTimeImmutable(), |
146 |
| - scheme: $scheme, |
147 |
| - transactionId: $transactionId, |
148 |
| - ); |
149 |
| - |
150 |
| - $this->dispatcher?->dispatch($event, TransactionEvent::EVENT_ID); |
| 151 | + /** |
| 152 | + * @return array{'preEvent': object, 'preEventId': string, 'postEvent': object, 'postEventId': string} |
| 153 | + */ |
| 154 | + private function createPreAndPostEventsAndIds( |
| 155 | + TransactionState $nextTransactionState, |
| 156 | + string $alias, |
| 157 | + string $scheme, |
| 158 | + string $transactionId, |
| 159 | + ): array { |
| 160 | + [$preEvent, $preEventId] = match ($nextTransactionState) { |
| 161 | + TransactionState::ACTIVE => [ |
| 162 | + new PreTransactionBeginEvent( |
| 163 | + alias: $alias, |
| 164 | + time: new \DateTimeImmutable(), |
| 165 | + scheme: $scheme, |
| 166 | + transactionId: $transactionId, |
| 167 | + ), |
| 168 | + PreTransactionBeginEvent::EVENT_ID, |
| 169 | + ], |
| 170 | + TransactionState::TERMINATED, TransactionState::ROLLED_BACK => [ |
| 171 | + new PreTransactionRollbackEvent( |
| 172 | + alias: $alias, |
| 173 | + time: new \DateTimeImmutable(), |
| 174 | + scheme: $scheme, |
| 175 | + transactionId: $transactionId, |
| 176 | + ), |
| 177 | + PreTransactionRollbackEvent::EVENT_ID, |
| 178 | + ], |
| 179 | + TransactionState::COMMITTED => [ |
| 180 | + new PreTransactionCommitEvent( |
| 181 | + alias: $alias, |
| 182 | + time: new \DateTimeImmutable(), |
| 183 | + scheme: $scheme, |
| 184 | + transactionId: $transactionId, |
| 185 | + ), |
| 186 | + PreTransactionCommitEvent::EVENT_ID, |
| 187 | + ], |
| 188 | + }; |
| 189 | + [$postEvent, $postEventId] = match ($nextTransactionState) { |
| 190 | + TransactionState::ACTIVE => [ |
| 191 | + new PostTransactionBeginEvent( |
| 192 | + alias: $alias, |
| 193 | + time: new \DateTimeImmutable(), |
| 194 | + scheme: $scheme, |
| 195 | + transactionId: $transactionId, |
| 196 | + ), |
| 197 | + PostTransactionBeginEvent::EVENT_ID, |
| 198 | + ], |
| 199 | + TransactionState::TERMINATED, TransactionState::ROLLED_BACK => [ |
| 200 | + new PostTransactionRollbackEvent( |
| 201 | + alias: $alias, |
| 202 | + time: new \DateTimeImmutable(), |
| 203 | + scheme: $scheme, |
| 204 | + transactionId: $transactionId, |
| 205 | + ), |
| 206 | + PostTransactionRollbackEvent::EVENT_ID, |
| 207 | + ], |
| 208 | + TransactionState::COMMITTED => [ |
| 209 | + new PostTransactionCommitEvent( |
| 210 | + alias: $alias, |
| 211 | + time: new \DateTimeImmutable(), |
| 212 | + scheme: $scheme, |
| 213 | + transactionId: $transactionId, |
| 214 | + ), |
| 215 | + PostTransactionCommitEvent::EVENT_ID, |
| 216 | + ], |
| 217 | + }; |
| 218 | + |
| 219 | + return [ |
| 220 | + 'preEvent' => $preEvent, |
| 221 | + 'preEventId' => $preEventId, |
| 222 | + 'postEvent' => $postEvent, |
| 223 | + 'postEventId' => $postEventId, |
| 224 | + ]; |
151 | 225 | }
|
152 | 226 | }
|
0 commit comments