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