-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChainAdapter.php
94 lines (81 loc) · 2.24 KB
/
ChainAdapter.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
declare(strict_types=1);
/*
* @author Aaron Scherer <aequasi@gmail.com>
* @date 2019
* @license https://opensource.org/licenses/MIT
*/
namespace Secretary\Adapter\Chain;
use Secretary\Adapter\AbstractAdapter;
use Secretary\Adapter\AdapterInterface;
use Secretary\Exception\SecretNotFoundException;
use Secretary\Secret;
/**
* Class ChainAdapter.
*
* @package Secretary\Adapter\Chain
*/
final class ChainAdapter extends AbstractAdapter
{
/**
* @var list<AdapterInterface>
*/
private array $adapters;
/**
* @param AdapterInterface[] $adapters
*/
public function __construct(array $adapters)
{
$this->adapters = $adapters;
}
/**
* Note: $options is a 0-indexed array of options. Each index corresponds to the index of the adapters
* {@inheritdoc}
*/
public function getSecret(string $key, ?array $options = []): Secret
{
foreach ($this->adapters as $index => $adapter) {
try {
return $adapter->getSecret($key, $options[$index] ?? []);
} catch (SecretNotFoundException $ignored) {
}
}
throw new SecretNotFoundException($key);
}
/**
* {@inheritdoc}
*/
public function putSecret(Secret $secret, ?array $options = []): Secret
{
foreach ($this->adapters as $index => $adapter) {
$adapter->putSecret($secret, $options[$index] ?? []);
}
return $secret;
}
/**
* {@inheritdoc}
*/
public function deleteSecret(Secret $secret, ?array $options = []): void
{
foreach ($this->adapters as $index => $adapter) {
$adapter->deleteSecret($secret, $options[$index] ?? []);
}
}
/**
* {@inheritdoc}
*/
public function deleteSecretByKey(string $key, ?array $options = []): void
{
$success = false;
foreach ($this->adapters as $index => $adapter) {
try {
$adapter->deleteSecret($adapter->getSecret($key), $options[$index] ?? []);
$success = true;
} catch (SecretNotFoundException $ignored) {
}
}
if (!$success) {
throw new SecretNotFoundException($key);
}
}
}