diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 3bf7a73..2db2a01 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -52,6 +52,8 @@ jobs: - name: Install dependencies run: composer install --prefer-dist + - name: Test cs-fixer + run: vendor/bin/php-cs-fixer fix src --dry-run --diff --no-ansi - name: Run phpstan run: vendor/bin/phpstan analyse src - name: Run tests diff --git a/src/Client/RedisClient.php b/src/Client/RedisClient.php index 1fe2ea6..43a7d95 100644 --- a/src/Client/RedisClient.php +++ b/src/Client/RedisClient.php @@ -15,31 +15,40 @@ public function __construct(protected ?\Redis $redis = null) $this->redis = $redis ?? new \Redis($_SERVER['REDIS_HOST'] ? ['host' => $_SERVER['REDIS_HOST']] : null); } - public function hashMultiSet(string $key, array $data): bool|self + public function hMSet(string $key, array $data): void { - return $this->redis->hMSet(RedisClient::convertPrefix($key), $data); + $result = $this->redis->hMSet(RedisClient::convertPrefix($key), $data); + + if (!$result) { + $this->handleError(__METHOD__, $this->redis->getLastError()); + } } - public function hashGetAll(string $key): array + public function hGetAll(string $key): array { - return $this->redis->hGetAll(RedisClient::convertPrefix($key)); + $result = $this->redis->hGetAll(RedisClient::convertPrefix($key)); + + if ($result === false) { + $this->handleError(__METHOD__, $this->redis->getLastError()); + } + + return $result; } - public function remove(string $key): false|int|self + public function del(string $key): void { - return $this->redis->del(RedisClient::convertPrefix($key)); + $result = $this->redis->del(RedisClient::convertPrefix($key)); + if (!$result) { + $this->handleError(__METHOD__, $this->redis->getLastError()); + } } public function jsonGet(string $key): ?string { $result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, static::convertPrefix($key)); - if (!$result) { - if (($error = $this->redis->getLastError()) === null) { - return null; - } - - $this->handleError(RedisCommands::JSON_GET->value, $error); + if ($result === false) { + $this->handleError(RedisCommands::JSON_GET->value, $this->redis->getLastError()); } return $result; diff --git a/src/Client/RedisClientInterface.php b/src/Client/RedisClientInterface.php index 56ceed9..64695c5 100644 --- a/src/Client/RedisClientInterface.php +++ b/src/Client/RedisClientInterface.php @@ -8,9 +8,9 @@ interface RedisClientInterface { - public function hashMultiSet(string $key, array $data): bool|self; - public function hashGetAll(string $key): array; - public function remove(string $key): false|int|self; + 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; diff --git a/src/Om/Persister/AbstractPersister.php b/src/Om/Persister/AbstractPersister.php index bcf91e8..f49f2b4 100644 --- a/src/Om/Persister/AbstractPersister.php +++ b/src/Om/Persister/AbstractPersister.php @@ -21,7 +21,7 @@ public function __construct(private ?KeyGenerator $keyGenerator = null) } /** - * @return array + * @return array */ public function persist(Entity $objectMapper, $object): ObjectToPersist { diff --git a/src/Om/Persister/HashModel/HashPersister.php b/src/Om/Persister/HashModel/HashPersister.php index 76c2236..bef7136 100644 --- a/src/Om/Persister/HashModel/HashPersister.php +++ b/src/Om/Persister/HashModel/HashPersister.php @@ -14,11 +14,11 @@ public function doPersist(string $key, $data): void throw new \InvalidArgumentException('Redis hMSet() method #2 argument must be an array.'); } - $this->redis->hashMultiSet($key, $data); + $this->redis->hMSet($key, $data); } public function doDelete(string $key): void { - $this->redis->remove($key); + $this->redis->del($key); } } diff --git a/src/Om/Repository/HashModel/HashRepository.php b/src/Om/Repository/HashModel/HashRepository.php index e191791..323feee 100644 --- a/src/Om/Repository/HashModel/HashRepository.php +++ b/src/Om/Repository/HashModel/HashRepository.php @@ -13,7 +13,7 @@ final class HashRepository extends AbstractObjectRepository public function find(string $identifier): ?object { - $data = $this->redisClient->hashGetAll("$this->prefix:$identifier"); + $data = $this->redisClient->hGetAll("$this->prefix:$identifier"); if (!$data) { return null; } diff --git a/tests/Client/Client.php b/tests/Client/Client.php index de04e81..4c23ae6 100644 --- a/tests/Client/Client.php +++ b/tests/Client/Client.php @@ -11,6 +11,6 @@ class Client extends RedisClient public function __construct() { parent::__construct(); - $this->redis->pconnect('redis'); + $this->redis->pconnect($_SERVER['REDIS_HOST']); } }