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