-
Notifications
You must be signed in to change notification settings - Fork 59
/
TXN.php
49 lines (40 loc) · 1.24 KB
/
TXN.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace Consul\Services;
use Consul\Client;
use Consul\ClientInterface;
use Consul\ConsulResponse;
use Consul\OptionsResolver;
final class TXN
{
private ClientInterface $client;
public function __construct(?ClientInterface $client = null)
{
$this->client = $client ?: new Client();
}
public function put(array $operations = [], array $options = []): ConsulResponse
{
$this->validate($operations);
$params = [
'json' => $operations,
'query' => OptionsResolver::resolve($options, ['dc']),
];
return $this->client->put('v1/txn', $params);
}
/**
* Validate Transaction Available Operations.
*
* @throws \InvalidArgumentException
*/
private function validate(array $operations = []): void
{
foreach ($operations as $index => $operation) {
if (!\is_int($index)) {
throw new \InvalidArgumentException('Invalid Operations Array!');
}
$invalidOperations = array_diff(array_keys($operation), ['KV', 'Node', 'Service', 'Check']);
if (\count($invalidOperations)) {
throw new \InvalidArgumentException('Invalid Operations!');
}
}
}
}