Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit b0332c7

Browse files
committed
dispatch events from a Transaction
1 parent fdaa349 commit b0332c7

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public function transaction($connectionAlias = null)
167167
$connection = $this->connectionManager->getConnection($connectionAlias);
168168
$driverTransaction = $connection->getTransaction();
169169

170-
return new Transaction($driverTransaction);
170+
return new Transaction($driverTransaction, $this->eventDispatcher);
171171
}
172172

173173
/**

src/Connection/Connection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public function run($statement, $parameters = null, $tag = null)
110110
$parameters = (array) $parameters;
111111

112112
try {
113-
return $this->session->run($statement, $parameters, $tag);
113+
$result = $this->session->run($statement, $parameters, $tag);
114+
return $result;
114115
} catch (MessageFailureException $e) {
115116
$exception = new Neo4jException($e->getMessage());
116117
$exception->setNeo4jStatusCode($e->getStatusCode());

src/Transaction/Transaction.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
use GraphAware\Common\Cypher\Statement;
1515
use GraphAware\Common\Transaction\TransactionInterface;
16+
use GraphAware\Neo4j\Client\Event\PostRunEvent;
17+
use GraphAware\Neo4j\Client\Event\PreRunEvent;
18+
use GraphAware\Neo4j\Client\Neo4jClientEvents;
19+
use GraphAware\Neo4j\Client\Result\ResultCollection;
1620
use GraphAware\Neo4j\Client\StackInterface;
21+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1722

1823
class Transaction
1924
{
@@ -27,12 +32,19 @@ class Transaction
2732
*/
2833
protected $queue = [];
2934

35+
/**
36+
* @var EventDispatcherInterface
37+
*/
38+
protected $eventDispatcher;
39+
3040
/**
3141
* @param TransactionInterface $driverTransaction
42+
* @param EventDispatcherInterface $eventDispatcher
3243
*/
33-
public function __construct(TransactionInterface $driverTransaction)
44+
public function __construct(TransactionInterface $driverTransaction, EventDispatcherInterface $eventDispatcher)
3445
{
3546
$this->driverTransaction = $driverTransaction;
47+
$this->eventDispatcher = $eventDispatcher;
3648
}
3749

3850
/**
@@ -59,8 +71,12 @@ public function run($statement, array $parameters = [], $tag = null)
5971
if (!$this->driverTransaction->isOpen() && !in_array($this->driverTransaction->status(), ['COMMITED', 'ROLLED_BACK'], true)) {
6072
$this->driverTransaction->begin();
6173
}
74+
$statement = Statement::create($statement, $parameters, $tag);
75+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_PRE_RUN, new PreRunEvent([$statement]));
76+
$result = $this->driverTransaction->run(Statement::create($statement, $parameters, $tag));
77+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_POST_RUN, new PostRunEvent(ResultCollection::withResult($result)));
6278

63-
return $this->driverTransaction->run(Statement::create($statement, $parameters, $tag));
79+
return $result;
6480
}
6581

6682
/**
@@ -90,7 +106,11 @@ public function runStack(StackInterface $stack)
90106
$sts[] = $statement;
91107
}
92108

93-
return $this->driverTransaction->runMultiple($sts);
109+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_PRE_RUN, new PreRunEvent($stack->statements()));
110+
$results = $this->driverTransaction->runMultiple($sts);
111+
$this->eventDispatcher->dispatch(Neo4jClientEvents::NEO4J_POST_RUN, new PostRunEvent($results));
112+
113+
return $results;
94114
}
95115

96116
public function begin()

tests/Integration/StatisticsIntegrationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
namespace GraphAware\Neo4j\Client\Tests\Integration;
13+
use GraphAware\Common\Result\StatementStatisticsInterface;
1314

1415
/**
1516
* Class StatisticsIntegrationTest.
@@ -52,4 +53,19 @@ public function testRelationshipsDeletedWithHttp()
5253

5354
$this->assertEquals(1, $result->summarize()->updateStatistics()->relationshipsDeleted());
5455
}
56+
57+
/**
58+
* @group bolt-stats
59+
*/
60+
public function testNodesCreatedWithBolt()
61+
{
62+
$this->emptyDb();
63+
$result = $this->client->run('MATCH (n) RETURN count(n)', [], null, 'bolt');
64+
$this->assertInstanceOf(StatementStatisticsInterface::class, $result->summarize()->updateStatistics());
65+
66+
$tx = $this->client->transaction('bolt');
67+
$result = $tx->run('MATCH (n) RETURN count(n)');
68+
$tx->commit();
69+
$this->assertInstanceOf(StatementStatisticsInterface::class, $result->summarize()->updateStatistics());
70+
}
5571
}

0 commit comments

Comments
 (0)