1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018-2019 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace ArangoDb; |
13
|
|
|
|
14
|
|
|
use ArangoDb\Type\Batch; |
15
|
|
|
use ArangoDb\Type\GuardSupport; |
16
|
|
|
use ArangoDb\Type\Transaction as TransactionType; |
17
|
|
|
use ArangoDb\Type\Transactional; |
18
|
|
|
use ArangoDb\Type\Type; |
19
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
20
|
|
|
use function MongoDB\BSON\toPHP; |
21
|
|
|
use Psr\Http\Client\ClientInterface; |
22
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
23
|
|
|
use Psr\Http\Message\RequestInterface; |
24
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
27
|
|
|
|
28
|
|
|
final class TransactionalClient implements ClientInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ClientInterface |
32
|
|
|
*/ |
33
|
|
|
private $client; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RequestFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $requestFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ResponseFactoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $responseFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Types |
47
|
|
|
* |
48
|
|
|
* @var Type[] |
49
|
|
|
*/ |
50
|
|
|
private $types = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Types |
54
|
|
|
* |
55
|
|
|
* @var Transactional[] |
56
|
|
|
*/ |
57
|
|
|
private $transactionalTypes = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var StreamFactoryInterface |
61
|
|
|
*/ |
62
|
|
|
private $streamFactory; |
63
|
|
|
|
64
|
|
|
public function __construct( |
65
|
|
|
ClientInterface $client, |
66
|
|
|
RequestFactoryInterface $requestFactory, |
67
|
|
|
ResponseFactoryInterface $responseFactory, |
68
|
|
|
StreamFactoryInterface $streamFactory |
69
|
|
|
) { |
70
|
|
|
$this->client = $client; |
71
|
|
|
$this->requestFactory = $requestFactory; |
72
|
|
|
$this->responseFactory = $responseFactory; |
73
|
|
|
$this->streamFactory = $streamFactory; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function sendRequest(RequestInterface $request): ResponseInterface |
77
|
|
|
{ |
78
|
|
|
return $this->client->sendRequest($request); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sends types and transactional types. Type responses and transaction response are validated via guards if provided |
83
|
|
|
* to a type. You can also manually validate the transaction response but not the non transaction response. |
84
|
|
|
* |
85
|
|
|
* @param array $params |
86
|
|
|
* @param bool $waitForSync |
87
|
|
|
* @return ResponseInterface |
88
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
89
|
|
|
*/ |
90
|
|
|
public function send(array $params = [], bool $waitForSync = false): ResponseInterface |
91
|
|
|
{ |
92
|
|
|
if (0 !== count($this->types)) { |
93
|
|
|
$batch = Batch::fromTypes(...$this->types); |
94
|
|
|
$responseBatch = $this->client->sendRequest($batch->toRequest($this->requestFactory, $this->streamFactory)); |
95
|
|
|
|
96
|
|
|
if (null !== ($guards = $batch->guards())) { |
|
|
|
|
97
|
|
|
BatchResult::fromResponse( |
98
|
|
|
$responseBatch, |
99
|
|
|
$this->responseFactory, |
100
|
|
|
$this->streamFactory |
101
|
|
|
)->validate(...$guards); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$actions = ''; |
106
|
|
|
$collectionsWrite = [[]]; |
107
|
|
|
$collectionsRead = [[]]; |
108
|
|
|
$return = []; |
109
|
|
|
$guards = []; |
110
|
|
|
|
111
|
|
|
if (0 === count($this->transactionalTypes)) { |
112
|
|
|
return $this->responseFactory->createResponse(StatusCodeInterface::STATUS_OK); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($this->transactionalTypes as $key => $type) { |
116
|
|
|
$collectionsWrite[] = $type->collectionsWrite(); |
117
|
|
|
$collectionsRead[] = $type->collectionsRead(); |
118
|
|
|
|
119
|
|
|
if ($type instanceof GuardSupport |
120
|
|
|
&& ($guard = $type->guard()) !== null |
121
|
|
|
) { |
122
|
|
|
$guards[] = $guard; |
123
|
|
|
$key = $guard->contentId(); |
124
|
|
|
} |
125
|
|
|
$actions .= str_replace('var rId', 'var rId' . $key, $type->toJs()); |
126
|
|
|
$return[] = 'rId' . $key; |
127
|
|
|
} |
128
|
|
|
$collectionsWrite = array_merge(...$collectionsWrite); |
129
|
|
|
$collectionsRead = array_merge(...$collectionsRead); |
130
|
|
|
|
131
|
|
|
$response = $this->client->sendRequest( |
132
|
|
|
TransactionType::with( |
133
|
|
|
sprintf( |
134
|
|
|
'function () {var db = require("@arangodb").db;%s return {%s}}', |
135
|
|
|
$actions, |
136
|
|
|
implode(',', $return) |
137
|
|
|
), |
138
|
|
|
array_unique($collectionsWrite), |
139
|
|
|
$params, |
140
|
|
|
array_unique($collectionsRead), |
141
|
|
|
$waitForSync |
142
|
|
|
)->toRequest($this->requestFactory, $this->streamFactory) |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
if (0 !== count($guards)) { |
146
|
|
|
\array_walk($guards, static function ($guard) use ($response) { |
147
|
|
|
$guard($response); |
148
|
|
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->types = []; |
152
|
|
|
$this->transactionalTypes = []; |
153
|
|
|
|
154
|
|
|
return $response; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Add type |
159
|
|
|
* |
160
|
|
|
* @param Type $type |
161
|
|
|
*/ |
162
|
|
|
public function add(Type $type): void |
163
|
|
|
{ |
164
|
|
|
if ($type instanceof Transactional) { |
165
|
|
|
$this->transactionalTypes[] = $type; |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
$this->types[] = $type; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Adds multiple types |
173
|
|
|
* |
174
|
|
|
* @param Type ...$types |
175
|
|
|
*/ |
176
|
|
|
public function addList(Type ...$types): void |
177
|
|
|
{ |
178
|
|
|
foreach ($types as $type) { |
179
|
|
|
if ($type instanceof Transactional) { |
180
|
|
|
$this->transactionalTypes[] = $type; |
181
|
|
|
continue; |
182
|
|
|
} |
183
|
|
|
$this->types[] = $type; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Counts non transactional types |
189
|
|
|
* |
190
|
|
|
* @return int |
191
|
|
|
*/ |
192
|
|
|
public function countTypes(): int |
193
|
|
|
{ |
194
|
|
|
return count($this->types); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Counts transactional types |
199
|
|
|
* |
200
|
|
|
* @return int |
201
|
|
|
*/ |
202
|
|
|
public function countTransactionalTypes(): int |
203
|
|
|
{ |
204
|
|
|
return count($this->transactionalTypes); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Resets all types and transactional types |
209
|
|
|
*/ |
210
|
|
|
public function reset(): void |
211
|
|
|
{ |
212
|
|
|
$this->types = []; |
213
|
|
|
$this->transactionalTypes = []; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|