Skip to content

Commit

Permalink
Merge pull request #1 from Micro-PHP/v1.0
Browse files Browse the repository at this point in the history
v1.0 review
  • Loading branch information
Asisyas authored Dec 24, 2022
2 parents 9728903 + aded395 commit f4a524e
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 36 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Micro Framework: Redis component",
"type": "library",
"license": "MIT",
"version": "0.1",
"version": "1.0",
"autoload": {
"psr-4": {
"Micro\\Plugin\\Redis\\": "src/"
Expand All @@ -16,6 +16,6 @@
}
],
"require": {
"php": ">=8.0"
"micro/kernel": "^1"
}
}
29 changes: 18 additions & 11 deletions src/Business/Redis/RedisBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@

use Micro\Plugin\Redis\Configuration\ClientOptionsConfigurationInterface;
use Micro\Plugin\Redis\Configuration\RedisClientConfigurationInterface;
use Micro\Plugin\Redis\Redis\RedisInterface;
use Micro\Plugin\Redis\RedisPluginConfigurationInterface;
use \Redis;
use Redis;

/**
* @author Stanislau Komar <[email protected]>
*/
class RedisBuilder implements RedisBuilderInterface
{
/**
* @param RedisPluginConfigurationInterface $pluginConfiguration
* @param RedisFactoryInterface $redisFactory
*/
public function __construct(
private RedisPluginConfigurationInterface $pluginConfiguration,
private RedisFactoryInterface $redisFactory
private readonly RedisPluginConfigurationInterface $pluginConfiguration,
private readonly RedisFactoryInterface $redisFactory
)
{
}

/**
* @param string $redisAlias
*
* @return Redis
* {@inheritDoc}
*/
public function create(string $redisAlias): Redis
public function create(string $redisAlias): RedisInterface
{
$clientConfiguration = $this->pluginConfiguration->getClientConfiguration($redisAlias);
$redis = $this->redisFactory->create();
Expand All @@ -36,11 +38,12 @@ public function create(string $redisAlias): Redis
}

/**
* @param Redis $redis
* @param RedisInterface $redis
* @param RedisClientConfigurationInterface $configuration
*
* @return void
*/
protected function initialize(Redis $redis, RedisClientConfigurationInterface $configuration): void
protected function initialize(RedisInterface $redis, RedisClientConfigurationInterface $configuration): void
{
$connectionMethod = $this->getConnectionMethod($configuration);

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

/**
* @param Redis $redis
* @param RedisInterface $redis
* @param ClientOptionsConfigurationInterface $configuration
*
* @return void
*/
protected function setOptions(Redis $redis, ClientOptionsConfigurationInterface $configuration): void
protected function setOptions(RedisInterface $redis, ClientOptionsConfigurationInterface $configuration): void
{
$redis->setOption(Redis::OPT_SERIALIZER, $this->getRedisOptionValue($configuration->serializer()));
$redis->setOption(Redis::OPT_PREFIX, $configuration->prefix());
}

/**
* @param string $redisOption
*
* @return int
*/
protected function getRedisOptionValue(string $redisOption): int
Expand All @@ -84,6 +89,7 @@ protected function getRedisOptionValue(string $redisOption): int

/**
* @param RedisClientConfigurationInterface $configuration
*
* @return string
*/
protected function getConnectionMethod(RedisClientConfigurationInterface $configuration): string
Expand All @@ -93,6 +99,7 @@ protected function getConnectionMethod(RedisClientConfigurationInterface $config

/**
* @param RedisClientConfigurationInterface $configuration
*
* @return string|null
*/
protected function getPersistentId(RedisClientConfigurationInterface $configuration): ?string
Expand Down
4 changes: 2 additions & 2 deletions src/Business/Redis/RedisBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class RedisBuilderFactory implements RedisBuilderFactoryInterface
* @param RedisFactoryInterface $redisFactory
*/
public function __construct(
private RedisPluginConfigurationInterface $configuration,
private RedisFactoryInterface $redisFactory
private readonly RedisPluginConfigurationInterface $configuration,
private readonly RedisFactoryInterface $redisFactory
)
{
}
Expand Down
6 changes: 4 additions & 2 deletions src/Business/Redis/RedisBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Micro\Plugin\Redis\Business\Redis;

use Micro\Plugin\Redis\Redis\RedisInterface;

interface RedisBuilderInterface
{
/**
* @param string $redisAlias
* @return \Redis
* @return RedisInterface
*/
public function create(string $redisAlias): \Redis;
public function create(string $redisAlias): RedisInterface;
}
12 changes: 9 additions & 3 deletions src/Business/Redis/RedisFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace Micro\Plugin\Redis\Business\Redis;


use Micro\Plugin\Redis\Redis\Decorator\BaseRedisDecorator;
use Micro\Plugin\Redis\Redis\RedisInterface;

class RedisFactory implements RedisFactoryInterface
{
/**
* @return \Redis
* {@inheritDoc}
*/
public function create(): \Redis
public function create(): RedisInterface
{
return new \Redis();
$redis = new \Redis();

return new BaseRedisDecorator($redis);
}
}
6 changes: 4 additions & 2 deletions src/Business/Redis/RedisFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Micro\Plugin\Redis\Business\Redis;

use Micro\Plugin\Redis\Redis\RedisInterface;

interface RedisFactoryInterface
{
/**
* @return \Redis
* @return RedisInterface
*/
public function create(): \Redis;
public function create(): RedisInterface;
}
9 changes: 4 additions & 5 deletions src/Business/Redis/RedisManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

namespace Micro\Plugin\Redis\Business\Redis;

use \Redis;
use Micro\Plugin\Redis\Redis\RedisInterface;

class RedisManager implements RedisManagerInterface
{

/**
* @var array<string, Redis>
* @var array<string, RedisInterface>
*/
private array $redisCollection;

/**
* @param RedisBuilderFactoryInterface $redisBuilderFactory
*/
public function __construct(private RedisBuilderFactoryInterface $redisBuilderFactory)
public function __construct(private readonly RedisBuilderFactoryInterface $redisBuilderFactory)
{
$this->redisCollection = [];
}

/**
* {@inheritDoc}
*/
public function getClient(string $clientName): Redis
public function getClient(string $clientName): RedisInterface
{
if(!array_key_exists($clientName, $this->redisCollection)) {
$this->redisCollection[$clientName] = $this->redisBuilderFactory
Expand Down
7 changes: 5 additions & 2 deletions src/Business/Redis/RedisManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Micro\Plugin\Redis\Business\Redis;

use Micro\Plugin\Redis\Redis\RedisInterface;

interface RedisManagerInterface
{
/**
* @param string $clientName
* @return \Redis
*
* @return RedisInterface
*/
public function getClient(string $clientName): \Redis;
public function getClient(string $clientName): RedisInterface;
}
170 changes: 170 additions & 0 deletions src/Redis/Decorator/BaseRedisDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace Micro\Plugin\Redis\Redis\Decorator;


use Micro\Plugin\Redis\Redis\RedisInterface;

class BaseRedisDecorator implements RedisInterface
{
/**
* @param RedisInterface $redis
*/
public function __construct(private readonly \Redis $redis)
{
}

/**
* @param string $host can be a host, or the path to a unix domain socket
* @param int $port optional
* @param float $timeout value in seconds (optional, default is 0 meaning unlimited)
* @param string|null $persistentId identity for the requested persistent connection
* @param int $retryInterval retry interval in milliseconds.
* @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited)
*
* @return bool
*/
public function connect(string $host,
int $port = 6379,
float $timeout = 0.0,
string $persistentId = null,
int $retryInterval = 0,
float $readTimeout = 0.0): bool
{
return $this->redis->connect(
$host,
$port,
$timeout,
$persistentId,
$retryInterval,
$readTimeout,
);
}

/**
* @param string $host can be a host, or the path to a unix domain socket
* @param int $port optional
* @param float $timeout value in seconds (optional, default is 0 meaning unlimited)
* @param string|null $persistentId identity for the requested persistent connection
* @param int $retryInterval retry interval in milliseconds.
* @param float $readTimeout value in seconds (optional, default is 0 meaning unlimited)
*
* @return bool
*/
public function pconnect(string $host,
int $port = 6379,
float $timeout = 0.0,
string $persistentId = null,
int $retryInterval = 0,
float $readTimeout = 0.0): bool
{
return $this->redis->pconnect(
$host,
$port,
$timeout,
$persistentId,
$retryInterval,
$readTimeout
);
}

/**
* {@inheritDoc}
*/
public function subscribe(array $channels, array|string|callable $callback): mixed
{
return $this->redis->subscribe($channels, $callback);
}

/**
* {@inheritDoc}
*/
public function unsubscribe(array $channels): mixed
{
return $this->prepareReturnResult($this->redis->rawCommand('UNSUBSCRIBE', ''));
//return $this->redis->unsubscribe($channels);
}

/**
* {@inheritDoc}
*/
public function publish(string $channel, string $message): int|RedisInterface
{
return $this->prepareReturnResult($this->redis->publish($channel, $message));
}

/**
* {@inheritDoc}
*/
public function pubsub(string $keyword, array|string $argument): array|int|RedisInterface
{
return $this->prepareReturnResult($this->redis->pubsub($keyword, $argument));
}

/**
* {@inheritDoc}
*/
public function setex(string $key, int $expire, mixed $value): bool|RedisInterface
{
return $this->prepareReturnResult($this->redis->setex($key, $expire, $value));
}

/**
* {@inheritDoc}
*/
public function setOption(int $option, mixed $value): bool
{
return $this->redis->setOption($option, $value);
}

/**
* {@inheritDoc}
*/
public function set(string $key, mixed $data, int $timeout = null): bool|self
{
return $this->prepareReturnResult($this->redis->set($key, $data, $timeout));
}

/**
* {@inheritDoc}
*/
public function del($key1, ...$otherKeys): int|self
{
return $this->prepareReturnResult($this->redis->del($key1, ...$otherKeys));
}

/**
* @param mixed $result
* @return mixed
*/
protected function prepareReturnResult(mixed $result): mixed
{
if(!$result) {
return $result;
}

if($result instanceof \Redis) {
return $this;
}

return $result;
}

/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public function __call(string $name, array $arguments): mixed
{
if(!method_exists($this->redis, $name)) {
throw new \BadMethodCallException(sprintf(
'Method "%s" is not exists in the "%s"',
$name, get_class($this->redis)
));
}

return $this->prepareReturnResult($this->redis->{$name}(...$arguments));
}
}
Loading

0 comments on commit f4a524e

Please sign in to comment.