Repository navigation
Expand file tree
/
Copy pathEtcdStorage.php
More file actions
46 lines (39 loc) · 1.61 KB
/
Copy pathEtcdStorage.php
File metadata and controls
46 lines (39 loc) · 1.61 KB
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
<?php
namespace Aternos\Lock\Storage;
use Aternos\Etcd\Client;
use Aternos\Etcd\ClientInterface;
use Aternos\Etcd\Exception\Status\DeadlineExceededException;
use Aternos\Etcd\Exception\Status\UnavailableException;
use Aternos\Etcd\Exception\Status\UnknownException;
class EtcdStorage implements StorageInterface
{
protected ClientInterface $client;
public function __construct(?ClientInterface $client = null)
{
$this->client = $client ?? new Client();
}
public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
{
try {
return $this->client->putIf($key, $value, $previousValue ?? false, $returnNewValueOnFail);
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
throw new StorageException($e->getMessage(), $e->getCode(), $e);
}
}
public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string|null
{
try {
return $this->client->deleteIf($key, $previousValue ?? false, $returnNewValueOnFail);
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
throw new StorageException($e->getMessage(), $e->getCode(), $e);
}
}
public function get(string $key): ?string
{
try {
return $this->client->get($key);
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
throw new StorageException($e->getMessage(), $e->getCode(), $e);
}
}
}