Skip to content

Commit 3f68938

Browse files
committed
refactor: yagni for interface
Signed-off-by: Mateusz Cholewka <mateusz@cholewka.com.pl>
1 parent c255505 commit 3f68938

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

src/Prometheus/Storage/RedisClients/PHPRedis.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ public function getOption(int $option): mixed
3838
return $this->redis->getOption($option);
3939
}
4040

41-
public function eval(string $script, array $args = [], int $num_keys = 0): mixed
41+
public function eval(string $script, array $args = [], int $num_keys = 0): void
4242
{
43-
return $this->redis->eval($script, $args, $num_keys);
43+
$this->redis->eval($script, $args, $num_keys);
4444
}
4545

46-
public function set(string $key, mixed $value, mixed $options = null): string|bool
46+
public function set(string $key, mixed $value, mixed $options = null): void
4747
{
48-
return $this->redis->set($key, $value, $options);
48+
$this->redis->set($key, $value, $options);
4949
}
5050

51-
public function setNx(string $key, mixed $value): bool
51+
public function setNx(string $key, mixed $value): void
5252
{
53-
return $this->redis->setNx($key, $value);
53+
$this->redis->setNx($key, $value);
5454
}
5555

5656
public function hSetNx(string $key, string $field, mixed $value): bool
@@ -78,20 +78,15 @@ public function get(string $key): mixed
7878
return $this->redis->get($key);
7979
}
8080

81-
public function del(array|string $key, string ...$other_keys): int|false
81+
public function del(array|string $key, string ...$other_keys): void
8282
{
8383
try {
84-
return $this->redis->del($key, ...$other_keys);
84+
$this->redis->del($key, ...$other_keys);
8585
} catch (\RedisException $e) {
8686
throw new RedisClientException($e->getMessage());
8787
}
8888
}
8989

90-
public function getPrefix(): string
91-
{
92-
return $this->redis->_prefix('');
93-
}
94-
9590
/**
9691
* @throws StorageException
9792
*/

src/Prometheus/Storage/RedisClients/Predis.php

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@
55
namespace Prometheus\Storage\RedisClients;
66

77
use Predis\Client;
8-
use Predis\Configuration\Option\Prefix;
98

109
class Predis implements RedisClient
1110
{
1211
private const OPTIONS_MAP = [
13-
RedisClient::OPT_PREFIX => Prefix::class,
12+
RedisClient::OPT_PREFIX => 'prefix',
1413
];
1514

1615
private $client;
1716

18-
private $prefix = '';
17+
private $options = [];
1918

20-
public function __construct(Client $redis)
19+
public function __construct(Client $redis, array $options)
2120
{
2221
$this->client = $redis;
22+
23+
$this->options = $options;
2324
}
2425

25-
public static function create(array $options): self
26+
public static function create(array $parameters, array $options): self
2627
{
27-
$this->prefix = $options['prefix'] ?? '';
28-
$redisClient = new Client($options, ['prefix' => $options['prefix'] ?? '']);
28+
$redisClient = new Client($parameters, $options);
2929

30-
return new self($redisClient);
30+
return new self($redisClient, $options);
3131
}
3232

3333
public function getOption(int $option): mixed
@@ -38,19 +38,17 @@ public function getOption(int $option): mixed
3838

3939
$mappedOption = self::OPTIONS_MAP[$option];
4040

41-
return $this->client->getOptions()->$mappedOption;
41+
return $this->options[$mappedOption] ?? null;
4242
}
4343

44-
public function eval(string $script, array $args = [], int $num_keys = 0): mixed
44+
public function eval(string $script, array $args = [], int $num_keys = 0): void
4545
{
46-
return $this->client->eval($script, $num_keys, ...$args);
46+
$this->client->eval($script, $num_keys, ...$args);
4747
}
4848

49-
public function set(string $key, mixed $value, mixed $options = null): string|bool
49+
public function set(string $key, mixed $value, mixed $options = null): void
5050
{
51-
$result = $this->client->set($key, $value, ...$this->flattenFlags($options));
52-
53-
return (string) $result;
51+
$this->client->set($key, $value, ...$this->flattenFlags($options));
5452
}
5553

5654
private function flattenFlags(array $flags): array
@@ -68,9 +66,9 @@ private function flattenFlags(array $flags): array
6866
return $result;
6967
}
7068

71-
public function setNx(string $key, mixed $value): bool
69+
public function setNx(string $key, mixed $value): void
7270
{
73-
return $this->client->setnx($key, $value) === 1;
71+
$this->client->setnx($key, $value) === 1;
7472
}
7573

7674
public function hSetNx(string $key, string $field, mixed $value): bool
@@ -98,16 +96,9 @@ public function get(string $key): mixed
9896
return $this->client->get($key);
9997
}
10098

101-
public function del(array|string $key, string ...$other_keys): int|false
102-
{
103-
return $this->client->del($key, ...$other_keys);
104-
}
105-
106-
public function getPrefix(): string
99+
public function del(array|string $key, string ...$other_keys): void
107100
{
108-
$key = RedisClient::OPT_PREFIX;
109-
110-
return $this->prefix;
101+
$this->client->del($key, ...$other_keys);
111102
}
112103

113104
public function ensureOpenConnection(): void

src/Prometheus/Storage/RedisClients/RedisClient.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ interface RedisClient
1212

1313
public function getOption(int $option): mixed;
1414

15-
public function eval(string $script, array $args = [], int $num_keys = 0): mixed;
15+
public function eval(string $script, array $args = [], int $num_keys = 0): void;
1616

17-
public function set(string $key, mixed $value, mixed $options = null): string|bool;
17+
public function set(string $key, mixed $value, mixed $options = null): void;
1818

19-
public function setNx(string $key, mixed $value): bool;
19+
public function setNx(string $key, mixed $value): void;
2020

2121
public function hSetNx(string $key, string $field, mixed $value): bool;
2222

@@ -28,9 +28,7 @@ public function keys(string $pattern);
2828

2929
public function get(string $key): mixed;
3030

31-
public function del(array|string $key, string ...$other_keys): int|false;
32-
33-
public function getPrefix(): string;
31+
public function del(array|string $key, string ...$other_keys): void;
3432

3533
public function ensureOpenConnection(): void;
3634
}

0 commit comments

Comments
 (0)