forked from neo4j-php/neo4j-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHandler.php
52 lines (43 loc) · 1.58 KB
/
EventHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Neo4j\Neo4jBundle;
use Laudis\Neo4j\Databags\Statement;
use Laudis\Neo4j\Databags\SummarizedResult;
use Laudis\Neo4j\Exception\Neo4jException;
use Laudis\Neo4j\Types\CypherList;
use Laudis\Neo4j\Types\CypherMap;
use Neo4j\Neo4jBundle\Events\FailureEvent;
use Neo4j\Neo4jBundle\Events\PostRunEvent;
use Neo4j\Neo4jBundle\Events\PreRunEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class EventHandler
{
private ?EventDispatcherInterface $dispatcher;
public function __construct(?EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
* @param iterable<Statement> $statements
* @param callable():CypherList<SummarizedResult<CypherList<CypherMap<mixed>>>> $runHandler
*
* @return CypherList<SummarizedResult<CypherList<CypherMap<mixed>>>>
*/
public function handle(callable $runHandler, iterable $statements): CypherList
{
if (null === $this->dispatcher) {
return $runHandler();
}
$this->dispatcher->dispatch(new PreRunEvent($statements), PreRunEvent::EVENT_ID);
try {
$tbr = $runHandler();
$this->dispatcher->dispatch(new PostRunEvent($tbr), PostRunEvent::EVENT_ID);
} catch (Neo4jException $e) {
$event = new FailureEvent($e);
$event = $this->dispatcher->dispatch($event, FailureEvent::EVENT_ID);
if ($event->shouldThrowException()) {
throw $e;
}
}
return new CypherList();
}
}