-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathAbstractRunner.php
More file actions
155 lines (134 loc) · 5.28 KB
/
Copy pathAbstractRunner.php
File metadata and controls
155 lines (134 loc) · 5.28 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
/*
* This file is part of the Neo4j PHP Client and Driver package.
*
* (c) Nagels <https://nagels.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Laudis\Neo4j\TestkitBackend\Handlers;
use Exception;
use Laudis\Neo4j\Contracts\SessionInterface;
use Laudis\Neo4j\Contracts\TransactionInterface;
use Laudis\Neo4j\Databags\SummarizedResult;
use Laudis\Neo4j\Databags\TransactionConfiguration;
use Laudis\Neo4j\Exception\Neo4jException;
use Laudis\Neo4j\Exception\TransactionException;
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
use Laudis\Neo4j\TestkitBackend\MainRepository;
use Laudis\Neo4j\TestkitBackend\Requests\SessionRunRequest;
use Laudis\Neo4j\TestkitBackend\Requests\TransactionRunRequest;
use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse;
use Laudis\Neo4j\TestkitBackend\Responses\ResultResponse;
use Laudis\Neo4j\Types\AbstractCypherObject;
use Laudis\Neo4j\Types\CypherList;
use Laudis\Neo4j\Types\CypherMap;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;
/**
* @psalm-import-type OGMTypes from \Laudis\Neo4j\Formatter\OGMFormatter
*
* @template T of \Laudis\Neo4j\TestkitBackend\Requests\SessionRunRequest|\Laudis\Neo4j\TestkitBackend\Requests\TransactionRunRequest
*
* @implements RequestHandlerInterface<T>
*/
abstract class AbstractRunner implements RequestHandlerInterface
{
protected MainRepository $repository;
private LoggerInterface $logger;
public function __construct(MainRepository $repository, LoggerInterface $logger)
{
$this->repository = $repository;
$this->logger = $logger;
}
public function handle($request): ResultResponse|DriverErrorResponse
{
$session = $this->getRunner($request);
$id = Uuid::v4();
try {
$params = [];
if ($request->getParams() !== null) {
foreach ($request->getParams() as $key => $value) {
$params[$key] = $this->decodeToValue($value);
}
}
if ($request instanceof SessionRunRequest && $session instanceof SessionInterface) {
$metaData = $request->getTxMeta();
$actualMeta = [];
if ($metaData !== null) {
foreach ($metaData as $key => $meta) {
$actualMeta[$key] = $this->decodeToValue($meta);
}
}
$config = TransactionConfiguration::default()->withMetadata($actualMeta)->withTimeout($request->getTimeout());
$result = $session->run($request->getCypher(), $params, $config);
} else {
$result = $session->run($request->getCypher(), $params);
}
$this->repository->addRecords($id, $result);
return new ResultResponse($id, $result->keys());
} catch (Neo4jException $exception) {
if ($request instanceof SessionRunRequest) {
return new DriverErrorResponse($request->getSessionId(), $exception);
}
if ($request instanceof TransactionRunRequest) {
return new DriverErrorResponse($request->getTxId(), $exception);
}
throw new Exception('Unhandled neo4j exception for run request of type: '.get_class($request));
} catch (TransactionException $exception) {
if ($request instanceof TransactionRunRequest) {
return new DriverErrorResponse($request->getTxId(), $exception);
}
throw new Exception('Unhandled neo4j exception for run request of type: '.get_class($request));
}
// NOTE: all other exceptions will be caught in the Backend
}
/**
* @param array{name: string, data: array{value: iterable|scalar|null}} $param
*
* @return scalar|AbstractCypherObject|iterable|null
*/
private function decodeToValue(array $param)
{
$value = $param['data']['value'];
if (is_iterable($value)) {
if ($param['name'] === 'CypherMap') {
/** @psalm-suppress MixedArgumentTypeCoercion */
$map = [];
/**
* @var numeric $k
* @var mixed $v
*/
foreach ($value as $k => $v) {
/** @psalm-suppress MixedArgument */
$map[(string) $k] = $this->decodeToValue($v);
}
return new CypherMap($map);
}
if ($param['name'] === 'CypherList') {
$list = [];
/**
* @var mixed $v
*/
foreach ($value as $v) {
/** @psalm-suppress MixedArgument */
$list[] = $this->decodeToValue($v);
}
return new CypherList($list);
}
}
return $value;
}
/**
* @param T $request
*
* @return SessionInterface<SummarizedResult<CypherMap<OGMTypes>>>|TransactionInterface<SummarizedResult<CypherMap<OGMTypes>>>
*/
abstract protected function getRunner($request);
/**
* @param T $request
*/
abstract protected function getId($request): Uuid;
}