1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArangoClient\Transactions; |
6
|
|
|
|
7
|
|
|
use ArangoClient\Exceptions\ArangoException; |
8
|
|
|
use ArangoClient\Http\HttpRequestOptions; |
9
|
|
|
use stdClass; |
10
|
|
|
|
11
|
|
|
trait SupportsTransactions |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TransactionManager|null |
15
|
|
|
*/ |
16
|
|
|
protected ?TransactionManager $transactionManager = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return TransactionManager |
20
|
|
|
*/ |
21
|
18 |
|
public function transactions(): TransactionManager |
22
|
|
|
{ |
23
|
18 |
|
if (! isset($this->transactionManager)) { |
24
|
18 |
|
$this->transactionManager = new TransactionManager($this); |
25
|
|
|
} |
26
|
|
|
|
27
|
18 |
|
return $this->transactionManager; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Shortcut to begin() on the transactionManager |
32
|
|
|
* |
33
|
|
|
* @param array<string, array<string>> $collections |
34
|
|
|
* @param array<mixed> $options |
35
|
|
|
* @return string |
36
|
|
|
* |
37
|
|
|
* @throws ArangoException |
38
|
|
|
*/ |
39
|
1 |
|
public function begin(array $collections = [], array $options = []): string |
40
|
|
|
{ |
41
|
1 |
|
return $this->transactions()->begin($collections, $options); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Shortcut to begin() on the transactionManager |
46
|
|
|
* |
47
|
|
|
* @param array<string, array<string>> $collections |
48
|
|
|
* @param array<mixed> $options |
49
|
|
|
* @return string |
50
|
|
|
* |
51
|
|
|
* @throws ArangoException |
52
|
|
|
*/ |
53
|
4 |
|
public function beginTransaction(array $collections = [], array $options = []): string |
54
|
|
|
{ |
55
|
4 |
|
return $this->transactions()->begin($collections, $options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Shortcut to abort() on the transactionManager |
60
|
|
|
* |
61
|
|
|
* @param string|null $id |
62
|
|
|
* @return bool |
63
|
|
|
* |
64
|
|
|
* @throws ArangoException |
65
|
|
|
*/ |
66
|
3 |
|
public function abort(string $id = null): bool |
67
|
|
|
{ |
68
|
3 |
|
return $this->transactions()->abort($id); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Shortcut to abort() on the transactionManager |
73
|
|
|
* |
74
|
|
|
* @param string|null $id |
75
|
|
|
* @return bool |
76
|
|
|
* |
77
|
|
|
* @throws ArangoException |
78
|
|
|
*/ |
79
|
1 |
|
public function rollBack(string $id = null): bool |
80
|
|
|
{ |
81
|
1 |
|
return $this->transactions()->abort($id); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Shortcut to commit() on the transactionManager |
86
|
|
|
* |
87
|
|
|
* @param string|null $id |
88
|
|
|
* @return bool |
89
|
|
|
* |
90
|
|
|
* @throws ArangoException |
91
|
|
|
*/ |
92
|
1 |
|
public function commit(string $id = null): bool |
93
|
|
|
{ |
94
|
1 |
|
return $this->transactions()->commit($id); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @psalm-suppress MixedReturnStatement |
99
|
|
|
* |
100
|
|
|
* @param string $method |
101
|
|
|
* @param string $uri |
102
|
|
|
* @param array<mixed>|HttpRequestOptions $options |
103
|
|
|
* @param string|null $database |
104
|
|
|
* @param int|null $transactionId |
105
|
|
|
* @return stdClass |
106
|
|
|
* |
107
|
|
|
* @throws ArangoException |
108
|
|
|
*/ |
109
|
13 |
|
public function transactionAwareRequest( |
110
|
|
|
string $method, |
111
|
|
|
string $uri, |
112
|
|
|
$options = [], |
113
|
|
|
?string $database = null, |
114
|
|
|
?int $transactionId = null |
115
|
|
|
): stdClass { |
116
|
13 |
|
if (is_array($options)) { |
117
|
13 |
|
$options = $this->prepareRequestOptions($options); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
try { |
120
|
13 |
|
if (! isset($transactionId)) { |
121
|
13 |
|
$transactionId = $this->transactions()->getTransaction(); |
122
|
|
|
} |
123
|
1 |
|
$options->addHeader('x-arango-trx-id', $transactionId); |
124
|
|
|
} finally { |
125
|
13 |
|
return $this->request($method, $uri, $options, $database); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function setTransactionManager(TransactionManager $transactionManager): void |
130
|
|
|
{ |
131
|
1 |
|
$this->transactionManager = $transactionManager; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return TransactionManager|null |
136
|
|
|
*/ |
137
|
1 |
|
public function getTransactionManager(): ?TransactionManager |
138
|
|
|
{ |
139
|
1 |
|
return $this->transactionManager; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|