|
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\Http; |
|
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 Psr\Http\Message\RequestInterface; |
|
21
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
23
|
|
|
|
|
24
|
|
|
final class TransactionalClient implements TypeSupport, TransactionSupport |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var TypeSupport |
|
28
|
|
|
*/ |
|
29
|
|
|
private $client; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ResponseFactoryInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $responseFactory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Types |
|
38
|
|
|
* |
|
39
|
|
|
* @var Type[] |
|
40
|
|
|
*/ |
|
41
|
|
|
private $types = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Types |
|
45
|
|
|
* |
|
46
|
|
|
* @var Transactional[] |
|
47
|
|
|
*/ |
|
48
|
|
|
private $transactionalTypes = []; |
|
49
|
|
|
|
|
50
|
9 |
|
public function __construct(TypeSupport $client, ResponseFactoryInterface $responseFactory) |
|
51
|
|
|
{ |
|
52
|
9 |
|
$this->client = $client; |
|
53
|
9 |
|
$this->responseFactory = $responseFactory; |
|
54
|
9 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function sendType(Type $type): ResponseInterface |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->client->sendType($type); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function sendRequest(RequestInterface $request): ResponseInterface |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->client->sendRequest($request); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
8 |
|
public function send(array $params = [], bool $waitForSync = false): ResponseInterface |
|
67
|
|
|
{ |
|
68
|
8 |
|
if (0 !== count($this->types)) { |
|
69
|
2 |
|
$this->client->sendType( |
|
70
|
2 |
|
Batch::fromTypes(...$this->types) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
8 |
|
$actions = ''; |
|
75
|
8 |
|
$collectionsWrite = [[]]; |
|
76
|
8 |
|
$collectionsRead = [[]]; |
|
77
|
8 |
|
$return = []; |
|
78
|
8 |
|
$guards = []; |
|
79
|
|
|
|
|
80
|
8 |
|
if (0 === count($this->transactionalTypes)) { |
|
81
|
1 |
|
return $this->responseFactory->createResponse(StatusCodeInterface::STATUS_OK); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
7 |
|
foreach ($this->transactionalTypes as $key => $type) { |
|
85
|
7 |
|
$collectionsWrite[] = $type->collectionsWrite(); |
|
86
|
7 |
|
$collectionsRead[] = $type->collectionsRead(); |
|
87
|
|
|
|
|
88
|
7 |
|
if ($type instanceof GuardSupport |
|
89
|
7 |
|
&& ($guard = $type->guard()) !== null |
|
90
|
|
|
) { |
|
91
|
1 |
|
$guards[] = $guard; |
|
92
|
1 |
|
$key = $guard->contentId(); |
|
93
|
|
|
} |
|
94
|
7 |
|
$actions .= str_replace('var rId', 'var rId' . $key, $type->toJs()); |
|
95
|
7 |
|
$return[] = 'rId' . $key; |
|
96
|
|
|
} |
|
97
|
7 |
|
$collectionsWrite = array_merge(...$collectionsWrite); |
|
98
|
7 |
|
$collectionsRead = array_merge(...$collectionsRead); |
|
99
|
|
|
|
|
100
|
7 |
|
$response = $this->client->sendType( |
|
101
|
7 |
|
TransactionType::with( |
|
102
|
7 |
|
sprintf( |
|
103
|
7 |
|
'function () {var db = require("@arangodb").db;%s return {%s}}', |
|
104
|
7 |
|
$actions, |
|
105
|
7 |
|
implode(',', $return) |
|
106
|
|
|
), |
|
107
|
7 |
|
array_unique($collectionsWrite), |
|
108
|
7 |
|
$params, |
|
109
|
7 |
|
array_unique($collectionsRead), |
|
110
|
7 |
|
$waitForSync |
|
111
|
|
|
) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
7 |
|
if (0 !== count($guards)) { |
|
115
|
|
|
\array_walk($guards, static function ($guard) use ($response) { |
|
116
|
1 |
|
$guard($response); |
|
117
|
1 |
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
7 |
|
$this->types = []; |
|
121
|
7 |
|
$this->transactionalTypes = []; |
|
122
|
|
|
|
|
123
|
7 |
|
return $response; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Add type |
|
128
|
|
|
* |
|
129
|
|
|
* @param Type $type |
|
130
|
|
|
*/ |
|
131
|
4 |
|
public function add(Type $type): void |
|
132
|
|
|
{ |
|
133
|
4 |
|
if ($type instanceof Transactional) { |
|
134
|
4 |
|
$this->transactionalTypes[] = $type; |
|
135
|
4 |
|
return; |
|
136
|
|
|
} |
|
137
|
3 |
|
$this->types[] = $type; |
|
138
|
3 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Adds multiple types |
|
142
|
|
|
* |
|
143
|
|
|
* @param Type ...$types |
|
144
|
|
|
*/ |
|
145
|
4 |
|
public function addList(Type ...$types): void |
|
146
|
|
|
{ |
|
147
|
4 |
|
foreach ($types as $type) { |
|
148
|
4 |
|
if ($type instanceof Transactional) { |
|
149
|
4 |
|
$this->transactionalTypes[] = $type; |
|
150
|
4 |
|
continue; |
|
151
|
|
|
} |
|
152
|
|
|
$this->types[] = $type; |
|
153
|
|
|
} |
|
154
|
4 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Counts non transactional types |
|
158
|
|
|
* |
|
159
|
|
|
* @return int |
|
160
|
|
|
*/ |
|
161
|
3 |
|
public function countTypes(): int |
|
162
|
|
|
{ |
|
163
|
3 |
|
return count($this->types); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Counts transactional types |
|
168
|
|
|
* |
|
169
|
|
|
* @return int |
|
170
|
|
|
*/ |
|
171
|
3 |
|
public function countTransactionalTypes(): int |
|
172
|
|
|
{ |
|
173
|
3 |
|
return count($this->transactionalTypes); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Resets all types and transactional types |
|
178
|
|
|
*/ |
|
179
|
1 |
|
public function reset(): void |
|
180
|
|
|
{ |
|
181
|
1 |
|
$this->types = []; |
|
182
|
1 |
|
$this->transactionalTypes = []; |
|
183
|
1 |
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|