-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureConfigManagerFactory.php
89 lines (76 loc) · 3.26 KB
/
FeatureConfigManagerFactory.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
<?php
// SPDX-License-Identifier: BSD-3-Clause
declare(strict_types=1);
namespace SingleA\Bundles\Redis;
use Psr\Log\LoggerInterface;
use SingleA\Contracts\FeatureConfig\FeatureConfigInterface;
use SingleA\Contracts\Marshaller\FeatureConfigEncryptorInterface;
use SingleA\Contracts\Marshaller\FeatureConfigMarshallerInterface;
use SingleA\Contracts\Persistence\FeatureConfigManagerInterface;
final class FeatureConfigManagerFactory
{
public function __invoke(
string $key,
\Predis\ClientInterface|\Redis|\RedisCluster $redis,
FeatureConfigMarshallerInterface $marshaller,
FeatureConfigEncryptorInterface $encryptor,
bool $required,
?LoggerInterface $logger = null,
): FeatureConfigManagerInterface {
return new class($key, $redis, $marshaller, $encryptor, $required, $logger) implements FeatureConfigManagerInterface {
public function __construct(
private readonly string $key,
private readonly \Predis\ClientInterface|\Redis|\RedisCluster $redis,
private readonly FeatureConfigMarshallerInterface $marshaller,
private readonly FeatureConfigEncryptorInterface $encryptor,
private readonly bool $required,
private readonly ?LoggerInterface $logger,
) {}
public function supports(FeatureConfigInterface|string $config): bool
{
return $this->marshaller->supports($config);
}
public function isRequired(): bool
{
return $this->required;
}
public function exists(string $id): bool
{
return (bool) $this->redis->hexists($this->key, $id);
}
public function persist(string $id, FeatureConfigInterface $config, string $secret): void
{
$value = $this->marshaller->marshall($config);
$value = $this->encryptor->encrypt($value, $secret);
try {
$this->redis->hset($this->key, $id, $value);
} finally {
$this->logger?->debug('Key "'.$this->key.'": config '.$id.' persisted.');
}
}
public function find(string $id, string $secret): ?FeatureConfigInterface
{
/** @var string|false $value */
$value = $this->redis->hget($this->key, $id);
if (\is_string($value)) {
return $this->marshaller->unmarshall(
$this->encryptor->decrypt($value, $secret),
);
}
return null;
}
public function remove(string ...$ids): int
{
if (empty($ids)) {
return 0;
}
try {
/** @psalm-suppress InvalidArgument, InvalidCast */
return (int) $this->redis->hdel($this->key, ...$ids); // @phan-suppress-current-line PhanParamTooManyUnpack, PhanTypeMismatchArgumentProbablyReal
} finally {
$this->logger?->debug('Key "'.$this->key.'": configs removed: '.implode(', ', $ids).'.');
}
}
};
}
}