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
|
9 |
|
public function __construct( |
65
|
|
|
ClientInterface $client, |
66
|
|
|
RequestFactoryInterface $requestFactory, |
67
|
|
|
ResponseFactoryInterface $responseFactory, |
68
|
|
|
StreamFactoryInterface $streamFactory |
69
|
|
|
) { |
70
|
9 |
|
$this->client = $client; |
71
|
9 |
|
$this->requestFactory = $requestFactory; |
72
|
9 |
|
$this->responseFactory = $responseFactory; |
73
|
9 |
|
$this->streamFactory = $streamFactory; |
74
|
9 |
|
} |
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
|
8 |
|
public function send(array $params = [], bool $waitForSync = false): ResponseInterface |
91
|
|
|
{ |
92
|
8 |
|
if (0 !== count($this->types)) { |
93
|
2 |
|
$batch = Batch::fromTypes(...$this->types); |
94
|
2 |
|
$responseBatch = $this->client->sendRequest($batch->toRequest($this->requestFactory, $this->streamFactory)); |
95
|
|
|
|
96
|
2 |
|
if (null !== ($guards = $batch->guards())) { |
|
|
|
|
97
|
1 |
|
BatchResult::fromResponse($responseBatch, $this->responseFactory)->validate(...$guards); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
8 |
|
$actions = ''; |
102
|
8 |
|
$collectionsWrite = [[]]; |
103
|
8 |
|
$collectionsRead = [[]]; |
104
|
8 |
|
$return = []; |
105
|
8 |
|
$guards = []; |
106
|
|
|
|
107
|
8 |
|
if (0 === count($this->transactionalTypes)) { |
108
|
1 |
|
return $this->responseFactory->createResponse(StatusCodeInterface::STATUS_OK); |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
foreach ($this->transactionalTypes as $key => $type) { |
112
|
7 |
|
$collectionsWrite[] = $type->collectionsWrite(); |
113
|
7 |
|
$collectionsRead[] = $type->collectionsRead(); |
114
|
|
|
|
115
|
7 |
|
if ($type instanceof GuardSupport |
116
|
7 |
|
&& ($guard = $type->guard()) !== null |
117
|
|
|
) { |
118
|
1 |
|
$guards[] = $guard; |
119
|
1 |
|
$key = $guard->contentId(); |
120
|
|
|
} |
121
|
7 |
|
$actions .= str_replace('var rId', 'var rId' . $key, $type->toJs()); |
122
|
7 |
|
$return[] = 'rId' . $key; |
123
|
|
|
} |
124
|
7 |
|
$collectionsWrite = array_merge(...$collectionsWrite); |
125
|
7 |
|
$collectionsRead = array_merge(...$collectionsRead); |
126
|
|
|
|
127
|
7 |
|
$response = $this->client->sendRequest( |
128
|
7 |
|
TransactionType::with( |
129
|
7 |
|
sprintf( |
130
|
7 |
|
'function () {var db = require("@arangodb").db;%s return {%s}}', |
131
|
|
|
$actions, |
132
|
7 |
|
implode(',', $return) |
133
|
|
|
), |
134
|
7 |
|
array_unique($collectionsWrite), |
135
|
|
|
$params, |
136
|
7 |
|
array_unique($collectionsRead), |
137
|
|
|
$waitForSync |
138
|
7 |
|
)->toRequest($this->requestFactory, $this->streamFactory) |
139
|
|
|
); |
140
|
|
|
|
141
|
7 |
|
if (0 !== count($guards)) { |
142
|
|
|
\array_walk($guards, static function ($guard) use ($response) { |
143
|
1 |
|
$guard($response); |
144
|
1 |
|
}); |
145
|
|
|
} |
146
|
|
|
|
147
|
7 |
|
$this->types = []; |
148
|
7 |
|
$this->transactionalTypes = []; |
149
|
|
|
|
150
|
7 |
|
return $response; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Add type |
155
|
|
|
* |
156
|
|
|
* @param Type $type |
157
|
|
|
*/ |
158
|
4 |
|
public function add(Type $type): void |
159
|
|
|
{ |
160
|
4 |
|
if ($type instanceof Transactional) { |
161
|
4 |
|
$this->transactionalTypes[] = $type; |
162
|
4 |
|
return; |
163
|
|
|
} |
164
|
3 |
|
$this->types[] = $type; |
165
|
3 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Adds multiple types |
169
|
|
|
* |
170
|
|
|
* @param Type ...$types |
171
|
|
|
*/ |
172
|
4 |
|
public function addList(Type ...$types): void |
173
|
|
|
{ |
174
|
4 |
|
foreach ($types as $type) { |
175
|
4 |
|
if ($type instanceof Transactional) { |
176
|
4 |
|
$this->transactionalTypes[] = $type; |
177
|
4 |
|
continue; |
178
|
|
|
} |
179
|
|
|
$this->types[] = $type; |
180
|
|
|
} |
181
|
4 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Counts non transactional types |
185
|
|
|
* |
186
|
|
|
* @return int |
187
|
|
|
*/ |
188
|
3 |
|
public function countTypes(): int |
189
|
|
|
{ |
190
|
3 |
|
return count($this->types); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Counts transactional types |
195
|
|
|
* |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
3 |
|
public function countTransactionalTypes(): int |
199
|
|
|
{ |
200
|
3 |
|
return count($this->transactionalTypes); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Resets all types and transactional types |
205
|
|
|
*/ |
206
|
1 |
|
public function reset(): void |
207
|
|
|
{ |
208
|
1 |
|
$this->types = []; |
209
|
1 |
|
$this->transactionalTypes = []; |
210
|
1 |
|
} |
211
|
|
|
} |
212
|
|
|
|