Skip to content

Commit aded395

Browse files
committed
v1.0 review
1 parent 9728903 commit aded395

13 files changed

+378
-36
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Micro Framework: Redis component",
44
"type": "library",
55
"license": "MIT",
6-
"version": "0.1",
6+
"version": "1.0",
77
"autoload": {
88
"psr-4": {
99
"Micro\\Plugin\\Redis\\": "src/"
@@ -16,6 +16,6 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=8.0"
19+
"micro/kernel": "^1"
2020
}
2121
}

src/Business/Redis/RedisBuilder.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,30 @@
44

55
use Micro\Plugin\Redis\Configuration\ClientOptionsConfigurationInterface;
66
use Micro\Plugin\Redis\Configuration\RedisClientConfigurationInterface;
7+
use Micro\Plugin\Redis\Redis\RedisInterface;
78
use Micro\Plugin\Redis\RedisPluginConfigurationInterface;
8-
use \Redis;
9+
use Redis;
910

11+
/**
12+
* @author Stanislau Komar <[email protected]>
13+
*/
1014
class RedisBuilder implements RedisBuilderInterface
1115
{
1216
/**
1317
* @param RedisPluginConfigurationInterface $pluginConfiguration
1418
* @param RedisFactoryInterface $redisFactory
1519
*/
1620
public function __construct(
17-
private RedisPluginConfigurationInterface $pluginConfiguration,
18-
private RedisFactoryInterface $redisFactory
21+
private readonly RedisPluginConfigurationInterface $pluginConfiguration,
22+
private readonly RedisFactoryInterface $redisFactory
1923
)
2024
{
2125
}
2226

2327
/**
24-
* @param string $redisAlias
25-
*
26-
* @return Redis
28+
* {@inheritDoc}
2729
*/
28-
public function create(string $redisAlias): Redis
30+
public function create(string $redisAlias): RedisInterface
2931
{
3032
$clientConfiguration = $this->pluginConfiguration->getClientConfiguration($redisAlias);
3133
$redis = $this->redisFactory->create();
@@ -36,11 +38,12 @@ public function create(string $redisAlias): Redis
3638
}
3739

3840
/**
39-
* @param Redis $redis
41+
* @param RedisInterface $redis
4042
* @param RedisClientConfigurationInterface $configuration
43+
*
4144
* @return void
4245
*/
43-
protected function initialize(Redis $redis, RedisClientConfigurationInterface $configuration): void
46+
protected function initialize(RedisInterface $redis, RedisClientConfigurationInterface $configuration): void
4447
{
4548
$connectionMethod = $this->getConnectionMethod($configuration);
4649

@@ -63,18 +66,20 @@ protected function initialize(Redis $redis, RedisClientConfigurationInterface $c
6366
}
6467

6568
/**
66-
* @param Redis $redis
69+
* @param RedisInterface $redis
6770
* @param ClientOptionsConfigurationInterface $configuration
71+
*
6872
* @return void
6973
*/
70-
protected function setOptions(Redis $redis, ClientOptionsConfigurationInterface $configuration): void
74+
protected function setOptions(RedisInterface $redis, ClientOptionsConfigurationInterface $configuration): void
7175
{
7276
$redis->setOption(Redis::OPT_SERIALIZER, $this->getRedisOptionValue($configuration->serializer()));
7377
$redis->setOption(Redis::OPT_PREFIX, $configuration->prefix());
7478
}
7579

7680
/**
7781
* @param string $redisOption
82+
*
7883
* @return int
7984
*/
8085
protected function getRedisOptionValue(string $redisOption): int
@@ -84,6 +89,7 @@ protected function getRedisOptionValue(string $redisOption): int
8489

8590
/**
8691
* @param RedisClientConfigurationInterface $configuration
92+
*
8793
* @return string
8894
*/
8995
protected function getConnectionMethod(RedisClientConfigurationInterface $configuration): string
@@ -93,6 +99,7 @@ protected function getConnectionMethod(RedisClientConfigurationInterface $config
9399

94100
/**
95101
* @param RedisClientConfigurationInterface $configuration
102+
*
96103
* @return string|null
97104
*/
98105
protected function getPersistentId(RedisClientConfigurationInterface $configuration): ?string

src/Business/Redis/RedisBuilderFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class RedisBuilderFactory implements RedisBuilderFactoryInterface
1111
* @param RedisFactoryInterface $redisFactory
1212
*/
1313
public function __construct(
14-
private RedisPluginConfigurationInterface $configuration,
15-
private RedisFactoryInterface $redisFactory
14+
private readonly RedisPluginConfigurationInterface $configuration,
15+
private readonly RedisFactoryInterface $redisFactory
1616
)
1717
{
1818
}

src/Business/Redis/RedisBuilderInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Micro\Plugin\Redis\Business\Redis;
44

5+
use Micro\Plugin\Redis\Redis\RedisInterface;
6+
57
interface RedisBuilderInterface
68
{
79
/**
810
* @param string $redisAlias
9-
* @return \Redis
11+
* @return RedisInterface
1012
*/
11-
public function create(string $redisAlias): \Redis;
13+
public function create(string $redisAlias): RedisInterface;
1214
}

src/Business/Redis/RedisFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
namespace Micro\Plugin\Redis\Business\Redis;
44

5+
6+
use Micro\Plugin\Redis\Redis\Decorator\BaseRedisDecorator;
7+
use Micro\Plugin\Redis\Redis\RedisInterface;
8+
59
class RedisFactory implements RedisFactoryInterface
610
{
711
/**
8-
* @return \Redis
12+
* {@inheritDoc}
913
*/
10-
public function create(): \Redis
14+
public function create(): RedisInterface
1115
{
12-
return new \Redis();
16+
$redis = new \Redis();
17+
18+
return new BaseRedisDecorator($redis);
1319
}
1420
}

src/Business/Redis/RedisFactoryInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Micro\Plugin\Redis\Business\Redis;
44

5+
use Micro\Plugin\Redis\Redis\RedisInterface;
6+
57
interface RedisFactoryInterface
68
{
79
/**
8-
* @return \Redis
10+
* @return RedisInterface
911
*/
10-
public function create(): \Redis;
12+
public function create(): RedisInterface;
1113
}

src/Business/Redis/RedisManager.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22

33
namespace Micro\Plugin\Redis\Business\Redis;
44

5-
use \Redis;
5+
use Micro\Plugin\Redis\Redis\RedisInterface;
66

77
class RedisManager implements RedisManagerInterface
88
{
9-
109
/**
11-
* @var array<string, Redis>
10+
* @var array<string, RedisInterface>
1211
*/
1312
private array $redisCollection;
1413

1514
/**
1615
* @param RedisBuilderFactoryInterface $redisBuilderFactory
1716
*/
18-
public function __construct(private RedisBuilderFactoryInterface $redisBuilderFactory)
17+
public function __construct(private readonly RedisBuilderFactoryInterface $redisBuilderFactory)
1918
{
2019
$this->redisCollection = [];
2120
}
2221

2322
/**
2423
* {@inheritDoc}
2524
*/
26-
public function getClient(string $clientName): Redis
25+
public function getClient(string $clientName): RedisInterface
2726
{
2827
if(!array_key_exists($clientName, $this->redisCollection)) {
2928
$this->redisCollection[$clientName] = $this->redisBuilderFactory

src/Business/Redis/RedisManagerInterface.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Micro\Plugin\Redis\Business\Redis;
44

5+
use Micro\Plugin\Redis\Redis\RedisInterface;
6+
57
interface RedisManagerInterface
68
{
79
/**
810
* @param string $clientName
9-
* @return \Redis
11+
*
12+
* @return RedisInterface
1013
*/
11-
public function getClient(string $clientName): \Redis;
14+
public function getClient(string $clientName): RedisInterface;
1215
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Redis\Redis\Decorator;
4+
5+
6+
use Micro\Plugin\Redis\Redis\RedisInterface;
7+
8+
class BaseRedisDecorator implements RedisInterface
9+
{
10+
/**
11+
* @param RedisInterface $redis
12+
*/
13+
public function __construct(private readonly \Redis $redis)
14+
{
15+
}
16+
17+
/**
18+
* @param string $host can be a host, or the path to a unix domain socket
19+
* @param int $port optional
20+
* @param float $timeout value in seconds (optional, default is 0 meaning unlimited)
21+
* @param string|null $persistentId identity for the requested persistent connection
22+
* @param int $retryInterval retry interval in milliseconds.
23+
* @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited)
24+
*
25+
* @return bool
26+
*/
27+
public function connect(string $host,
28+
int $port = 6379,
29+
float $timeout = 0.0,
30+
string $persistentId = null,
31+
int $retryInterval = 0,
32+
float $readTimeout = 0.0): bool
33+
{
34+
return $this->redis->connect(
35+
$host,
36+
$port,
37+
$timeout,
38+
$persistentId,
39+
$retryInterval,
40+
$readTimeout,
41+
);
42+
}
43+
44+
/**
45+
* @param string $host can be a host, or the path to a unix domain socket
46+
* @param int $port optional
47+
* @param float $timeout value in seconds (optional, default is 0 meaning unlimited)
48+
* @param string|null $persistentId identity for the requested persistent connection
49+
* @param int $retryInterval retry interval in milliseconds.
50+
* @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited)
51+
*
52+
* @return bool
53+
*/
54+
public function pconnect(string $host,
55+
int $port = 6379,
56+
float $timeout = 0.0,
57+
string $persistentId = null,
58+
int $retryInterval = 0,
59+
float $readTimeout = 0.0): bool
60+
{
61+
return $this->redis->pconnect(
62+
$host,
63+
$port,
64+
$timeout,
65+
$persistentId,
66+
$retryInterval,
67+
$readTimeout
68+
);
69+
}
70+
71+
/**
72+
* {@inheritDoc}
73+
*/
74+
public function subscribe(array $channels, array|string|callable $callback): mixed
75+
{
76+
return $this->redis->subscribe($channels, $callback);
77+
}
78+
79+
/**
80+
* {@inheritDoc}
81+
*/
82+
public function unsubscribe(array $channels): mixed
83+
{
84+
return $this->prepareReturnResult($this->redis->rawCommand('UNSUBSCRIBE', ''));
85+
//return $this->redis->unsubscribe($channels);
86+
}
87+
88+
/**
89+
* {@inheritDoc}
90+
*/
91+
public function publish(string $channel, string $message): int|RedisInterface
92+
{
93+
return $this->prepareReturnResult($this->redis->publish($channel, $message));
94+
}
95+
96+
/**
97+
* {@inheritDoc}
98+
*/
99+
public function pubsub(string $keyword, array|string $argument): array|int|RedisInterface
100+
{
101+
return $this->prepareReturnResult($this->redis->pubsub($keyword, $argument));
102+
}
103+
104+
/**
105+
* {@inheritDoc}
106+
*/
107+
public function setex(string $key, int $expire, mixed $value): bool|RedisInterface
108+
{
109+
return $this->prepareReturnResult($this->redis->setex($key, $expire, $value));
110+
}
111+
112+
/**
113+
* {@inheritDoc}
114+
*/
115+
public function setOption(int $option, mixed $value): bool
116+
{
117+
return $this->redis->setOption($option, $value);
118+
}
119+
120+
/**
121+
* {@inheritDoc}
122+
*/
123+
public function set(string $key, mixed $data, int $timeout = null): bool|self
124+
{
125+
return $this->prepareReturnResult($this->redis->set($key, $data, $timeout));
126+
}
127+
128+
/**
129+
* {@inheritDoc}
130+
*/
131+
public function del($key1, ...$otherKeys): int|self
132+
{
133+
return $this->prepareReturnResult($this->redis->del($key1, ...$otherKeys));
134+
}
135+
136+
/**
137+
* @param mixed $result
138+
* @return mixed
139+
*/
140+
protected function prepareReturnResult(mixed $result): mixed
141+
{
142+
if(!$result) {
143+
return $result;
144+
}
145+
146+
if($result instanceof \Redis) {
147+
return $this;
148+
}
149+
150+
return $result;
151+
}
152+
153+
/**
154+
* @param string $name
155+
* @param array $arguments
156+
*
157+
* @return mixed
158+
*/
159+
public function __call(string $name, array $arguments): mixed
160+
{
161+
if(!method_exists($this->redis, $name)) {
162+
throw new \BadMethodCallException(sprintf(
163+
'Method "%s" is not exists in the "%s"',
164+
$name, get_class($this->redis)
165+
));
166+
}
167+
168+
return $this->prepareReturnResult($this->redis->{$name}(...$arguments));
169+
}
170+
}

0 commit comments

Comments
 (0)