-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathSessionBeginTransaction.php
More file actions
124 lines (105 loc) · 3.86 KB
/
Copy pathSessionBeginTransaction.php
File metadata and controls
124 lines (105 loc) · 3.86 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
<?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 Laudis\Neo4j\Databags\TransactionConfiguration;
use Laudis\Neo4j\Exception\Neo4jException;
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
use Laudis\Neo4j\TestkitBackend\MainRepository;
use Laudis\Neo4j\TestkitBackend\Requests\SessionBeginTransactionRequest;
use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse;
use Laudis\Neo4j\TestkitBackend\Responses\TransactionResponse;
use Laudis\Neo4j\Types\AbstractCypherObject;
use Laudis\Neo4j\Types\CypherList;
use Laudis\Neo4j\Types\CypherMap;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;
/**
* @implements RequestHandlerInterface<SessionBeginTransactionRequest>
*/
final class SessionBeginTransaction implements RequestHandlerInterface
{
private MainRepository $repository;
private LoggerInterface $logger;
public function __construct(MainRepository $repository, LoggerInterface $logger)
{
$this->repository = $repository;
$this->logger = $logger;
}
/**
* @param SessionBeginTransactionRequest $request
*/
public function handle($request): TestkitResponseInterface
{
$session = $this->repository->getSession($request->getSessionId());
$config = TransactionConfiguration::default();
if ($request->getTimeout()) {
$config = $config->withTimeout($request->getTimeout());
}
if ($request->getTxMeta()) {
$metaData = $request->getTxMeta();
$actualMeta = [];
if ($metaData !== null) {
foreach ($metaData as $key => $meta) {
$actualMeta[$key] = $this->decodeToValue($meta);
}
}
$config = $config->withMetaData($actualMeta);
}
// TODO - Create beginReadTransaction and beginWriteTransaction
try {
$transaction = $session->beginTransaction(null, $config);
} catch (Neo4jException $exception) {
$this->logger->debug($exception->__toString());
return new DriverErrorResponse($request->getSessionId(), $exception);
}
$id = Uuid::v4();
$this->repository->addTransaction($id, $transaction);
$this->repository->bindTransactionToSession($request->getSessionId(), $id);
return new TransactionResponse($id);
}
/**
* @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;
}
}