From a051505f7a46007035a9757612104d5d59290595 Mon Sep 17 00:00:00 2001 From: clementtalleu Date: Fri, 7 Jun 2024 13:38:40 +0200 Subject: [PATCH 1/2] refacto + handle exceptions --- src/Client/RedisClient.php | 16 +++++++++------- src/Client/RedisClientInterface.php | 6 +++--- src/Om/Converters/AbstractNullConverter.php | 5 ++++- .../Converters/HashModel/HashObjectConverter.php | 1 - src/Om/Converters/HashModel/NullConverter.php | 5 ----- src/Om/Converters/JsonModel/NullConverter.php | 5 ----- .../JsonModel/StandardClassConverter.php | 2 -- 7 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Client/RedisClient.php b/src/Client/RedisClient.php index 7c78b60..5a34ef6 100644 --- a/src/Client/RedisClient.php +++ b/src/Client/RedisClient.php @@ -58,25 +58,27 @@ public function jsonGet(string $key): ?string $result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, static::convertPrefix($key)); if ($result === false) { - $this->handleError(RedisCommands::JSON_GET->value, $this->redis->getLastError()); + $this->handleError(__METHOD__, $this->redis->getLastError()); } return $result; } - public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): ?bool + public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): void { $result = $this->redis->rawCommand(RedisCommands::JSON_SET->value, static::convertPrefix($key), $path, $value); if (!$result) { - $this->handleError(RedisCommands::JSON_SET->value, $this->redis->getLastError()); + $this->handleError(__METHOD__, $this->redis->getLastError()); } - - return true; } - public function jsonDel(string $key, ?string $path = '$'): ?bool + public function jsonDel(string $key, ?string $path = '$'): void { - return $this->redis->rawCommand(RedisCommands::JSON_DELETE->value, static::convertPrefix($key), $path); + $result = $this->redis->rawCommand(RedisCommands::JSON_DELETE->value, static::convertPrefix($key), $path); + + if (!$result) { + $this->handleError(__METHOD__, $this->redis->getLastError()); + } } /** diff --git a/src/Client/RedisClientInterface.php b/src/Client/RedisClientInterface.php index ca191f5..94686fc 100644 --- a/src/Client/RedisClientInterface.php +++ b/src/Client/RedisClientInterface.php @@ -13,9 +13,9 @@ public function hMSet(string $key, array $data): void; public function hGetAll(string $key): array; public function del(string $key): void; public function jsonGet(string $key): ?string; - public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): ?bool; - public function jsonDel(string $key, ?string $path = '$'): ?bool; - public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array $properties = []): bool; + public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): void; + public function jsonDel(string $key, ?string $path = '$'): void; + public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array $properties = []): void; public function dropIndex(string $prefixKey): bool; public function count(string $prefixKey, array $criterias = []): int; public function search(string $prefixKey, array $search, array $orderBy, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null): array; diff --git a/src/Om/Converters/AbstractNullConverter.php b/src/Om/Converters/AbstractNullConverter.php index 981618d..5aaa3a2 100644 --- a/src/Om/Converters/AbstractNullConverter.php +++ b/src/Om/Converters/AbstractNullConverter.php @@ -11,7 +11,10 @@ abstract class AbstractNullConverter implements ConverterInterface */ abstract public function convert($data); - abstract public function revert($data, string $type): mixed; + public function revert($data, string $type): null + { + return null; + } public function supportsConversion(string $type, mixed $data): bool { diff --git a/src/Om/Converters/HashModel/HashObjectConverter.php b/src/Om/Converters/HashModel/HashObjectConverter.php index 7f6e164..56e28e0 100644 --- a/src/Om/Converters/HashModel/HashObjectConverter.php +++ b/src/Om/Converters/HashModel/HashObjectConverter.php @@ -4,7 +4,6 @@ namespace Talleu\RedisOm\Om\Converters\HashModel; -use Talleu\RedisOm\Exception\BadPropertyConfigurationException; use Talleu\RedisOm\Om\Converters\AbstractObjectConverter; use Talleu\RedisOm\Om\Converters\AbstractDateTimeConverter; use Talleu\RedisOm\Om\Mapping\Property; diff --git a/src/Om/Converters/HashModel/NullConverter.php b/src/Om/Converters/HashModel/NullConverter.php index e205730..3144edf 100644 --- a/src/Om/Converters/HashModel/NullConverter.php +++ b/src/Om/Converters/HashModel/NullConverter.php @@ -15,9 +15,4 @@ public function convert($data): string { return 'null'; } - - public function revert($data, string $type): mixed - { - return null; - } } diff --git a/src/Om/Converters/JsonModel/NullConverter.php b/src/Om/Converters/JsonModel/NullConverter.php index a14710c..444d281 100644 --- a/src/Om/Converters/JsonModel/NullConverter.php +++ b/src/Om/Converters/JsonModel/NullConverter.php @@ -15,9 +15,4 @@ public function convert($data): null { return null; } - - public function revert($data, string $type): mixed - { - return null; - } } diff --git a/src/Om/Converters/JsonModel/StandardClassConverter.php b/src/Om/Converters/JsonModel/StandardClassConverter.php index cada94e..40e36e5 100644 --- a/src/Om/Converters/JsonModel/StandardClassConverter.php +++ b/src/Om/Converters/JsonModel/StandardClassConverter.php @@ -5,9 +5,7 @@ namespace Talleu\RedisOm\Om\Converters\JsonModel; use Talleu\RedisOm\Om\Converters\AbstractStandardClassConverter; -use Talleu\RedisOm\Om\Converters\HashModel\ArrayConverter; use Talleu\RedisOm\Om\Converters\HashModel\ConverterFactory; -use Talleu\RedisOm\Om\Converters\HashModel\HashObjectConverter; final class StandardClassConverter extends AbstractStandardClassConverter { From 86caee2aff79ae470a9d2ef6a247bd8e6a3d4c50 Mon Sep 17 00:00:00 2001 From: clementtalleu Date: Fri, 7 Jun 2024 13:47:13 +0200 Subject: [PATCH 2/2] fix interface contract --- src/Client/RedisClient.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Client/RedisClient.php b/src/Client/RedisClient.php index 5a34ef6..6a53c56 100644 --- a/src/Client/RedisClient.php +++ b/src/Client/RedisClient.php @@ -84,7 +84,7 @@ public function jsonDel(string $key, ?string $path = '$'): void /** * @param \ReflectionProperty[] $properties */ - public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array $properties = []): bool + public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array $properties = []): void { $prefixKey = static::convertPrefix($prefixKey); @@ -128,10 +128,11 @@ public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array throw new BadPropertyConfigurationException(sprintf("Your class %s does not have any typed property", $prefixKey)); } - /** @var bool $rawResult */ $rawResult = call_user_func_array([$this->redis, 'rawCommand'], $arguments); - return $rawResult; + if (!$rawResult) { + $this->handleError(__METHOD__, $this->redis->getLastError()); + } } public function dropIndex(string $prefixKey): bool @@ -156,7 +157,11 @@ public function count(string $prefixKey, array $criterias = []): int $rawResult = call_user_func_array([$this->redis, 'rawCommand'], $arguments); - return (int)$rawResult[0]; + if (!$rawResult) { + $this->handleError(__METHOD__, $this->redis->getLastError()); + } + + return (int) $rawResult[0]; } public function scanKeys(string $prefixKey): array