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