Skip to content

Commit a61a5d8

Browse files
committed
feat: Add events for pre and post: begin, rollback and commit transaction events
1 parent 6afef54 commit a61a5d8

8 files changed

+176
-47
lines changed

src/Event/PostTransactionEvent.php

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Neo4j\Neo4jBundle\Event\Transaction;
4+
5+
class PostTransactionBeginEvent
6+
{
7+
public const EVENT_ID = 'neo4j.transaction.begin.post';
8+
9+
public function __construct(
10+
public readonly ?string $alias,
11+
public readonly \DateTimeInterface $time,
12+
public readonly ?string $scheme,
13+
public readonly string $transactionId,
14+
) {
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Neo4j\Neo4jBundle\Event\Transaction;
4+
5+
class PostTransactionCommitEvent
6+
{
7+
public const EVENT_ID = 'neo4j.transaction.commit.post';
8+
9+
public function __construct(
10+
public readonly ?string $alias,
11+
public readonly \DateTimeInterface $time,
12+
public readonly ?string $scheme,
13+
public readonly string $transactionId,
14+
) {
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Neo4j\Neo4jBundle\Event\Transaction;
4+
5+
class PostTransactionRollbackEvent
6+
{
7+
public const EVENT_ID = 'neo4j.transaction.rollback.post';
8+
9+
public function __construct(
10+
public readonly ?string $alias,
11+
public readonly \DateTimeInterface $time,
12+
public readonly ?string $scheme,
13+
public readonly string $transactionId,
14+
) {
15+
}
16+
}

src/Event/TransactionEvent.php renamed to src/Event/Transaction/PreTransactionBeginEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace Neo4j\Neo4jBundle\Event;
3+
namespace Neo4j\Neo4jBundle\Event\Transaction;
44

5-
class TransactionEvent
5+
class PreTransactionBeginEvent
66
{
7-
public const EVENT_ID = 'neo4j.on_transaction_begin';
7+
public const EVENT_ID = 'neo4j.transaction.begin.pre';
88

99
public function __construct(
1010
public readonly ?string $alias,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Neo4j\Neo4jBundle\Event\Transaction;
4+
5+
class PreTransactionCommitEvent
6+
{
7+
public const EVENT_ID = 'neo4j.transaction.commit.pre';
8+
9+
public function __construct(
10+
public readonly ?string $alias,
11+
public readonly \DateTimeInterface $time,
12+
public readonly ?string $scheme,
13+
public readonly string $transactionId,
14+
) {
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Neo4j\Neo4jBundle\Event\Transaction;
4+
5+
class PreTransactionRollbackEvent
6+
{
7+
public const EVENT_ID = 'neo4j.transaction.rollback.pre';
8+
9+
public function __construct(
10+
public readonly ?string $alias,
11+
public readonly \DateTimeInterface $time,
12+
public readonly ?string $scheme,
13+
public readonly string $transactionId,
14+
) {
15+
}
16+
}

src/EventHandler.php

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
use Neo4j\Neo4jBundle\Event\FailureEvent;
1212
use Neo4j\Neo4jBundle\Event\PostRunEvent;
1313
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;
1520
use Neo4j\Neo4jBundle\Factories\StopwatchEventNameFactory;
1621
use Symfony\Component\Stopwatch\Stopwatch;
1722
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
@@ -92,16 +97,21 @@ public function handleTransactionAction(
9297
): mixed {
9398
$stopWatchName = $this->nameFactory->createTransactionEventName($alias, $transactionId, $nextTransactionState);
9499

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+
);
98111

112+
$this->dispatcher?->dispatch($preEvent, $preEventId);
99113
$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);
105115

106116
return $result;
107117
}
@@ -138,15 +148,79 @@ private function handleAction(callable $runHandler, string $alias, string $schem
138148
}
139149
}
140150

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+
];
151225
}
152226
}

0 commit comments

Comments
 (0)