1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArangoClient\Transactions; |
6
|
|
|
|
7
|
|
|
use ArangoClient\Exceptions\ArangoException; |
8
|
|
|
|
9
|
|
|
trait SupportsTransactions |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var TransactionManager|null |
13
|
|
|
*/ |
14
|
|
|
protected ?TransactionManager $transactionManager = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return TransactionManager |
18
|
|
|
*/ |
19
|
17 |
|
public function transactions(): TransactionManager |
20
|
|
|
{ |
21
|
17 |
|
if (! isset($this->transactionManager)) { |
22
|
17 |
|
$this->transactionManager = new TransactionManager($this); |
23
|
|
|
} |
24
|
17 |
|
return $this->transactionManager; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Shortcut to begin() on the transactionManager |
29
|
|
|
* |
30
|
|
|
* @param array{read?: string[], write?: string[], exclusive?: string[]} $collections |
31
|
|
|
* @param array<mixed> $options |
32
|
|
|
* @return string |
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{read?: string[], write?: string[], exclusive?: string[]} $collections |
44
|
|
|
* @param array<mixed> $options |
45
|
|
|
* @return string |
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
|
|
|
* @return bool |
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
|
|
|
* @return bool |
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
|
|
|
* @param string|null $id |
80
|
|
|
* @return bool |
81
|
|
|
* @throws ArangoException |
82
|
|
|
*/ |
83
|
1 |
|
public function commit(string $id = null): bool |
84
|
|
|
{ |
85
|
1 |
|
return $this->transactions()->commit($id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @psalm-suppress MixedReturnStatement |
90
|
|
|
* |
91
|
|
|
* @param string $method |
92
|
|
|
* @param string $uri |
93
|
|
|
* @param array<mixed> $options |
94
|
|
|
* @param string|null $database |
95
|
|
|
* @param int|null $transactionId |
96
|
|
|
* @return array<mixed> |
97
|
|
|
* @throws ArangoException |
98
|
|
|
*/ |
99
|
12 |
|
public function transactionAwareRequest( |
100
|
|
|
string $method, |
101
|
|
|
string $uri, |
102
|
|
|
array $options = [], |
103
|
|
|
?string $database = null, |
104
|
|
|
?int $transactionId = null |
105
|
|
|
): array { |
106
|
|
|
try { |
107
|
12 |
|
if (! isset($transactionId)) { |
108
|
12 |
|
$transactionId = $this->transactions()->getTransaction(); |
109
|
|
|
} |
110
|
1 |
|
if (! isset($options['headers'])) { |
111
|
1 |
|
$options['headers'] = []; |
112
|
|
|
} |
113
|
1 |
|
if (! is_array($options['headers'])) { |
114
|
|
|
$options['headers'] = [$options['headers']]; |
115
|
|
|
} |
116
|
1 |
|
$options['headers']['x-arango-trx-id'] = $transactionId; |
117
|
|
|
} finally { |
118
|
12 |
|
return $this->request($method, $uri, $options, $database); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
public function setTransactionManager(TransactionManager $transactionManager): void |
123
|
|
|
{ |
124
|
1 |
|
$this->transactionManager = $transactionManager; |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return TransactionManager|null |
129
|
|
|
*/ |
130
|
1 |
|
public function getTransactionManager(): ?TransactionManager |
131
|
|
|
{ |
132
|
1 |
|
return $this->transactionManager; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|