Skip to content

Commit

Permalink
Merge pull request #11 from clementtalleu/feat/refacto_client_interface
Browse files Browse the repository at this point in the history
refacto redis client interface
  • Loading branch information
clementtalleu authored Jun 4, 2024
2 parents c40dd54 + 8d961bd commit 5365575
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Client/RedisClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Om/Persister/AbstractPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(private ?KeyGenerator $keyGenerator = null)
}

/**
* @return array<string, string>
* @return array<string, ObjectToPersist>
*/
public function persist(Entity $objectMapper, $object): ObjectToPersist
{
Expand Down
4 changes: 2 additions & 2 deletions src/Om/Persister/HashModel/HashPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Om/Repository/HashModel/HashRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class Client extends RedisClient
public function __construct()
{
parent::__construct();
$this->redis->pconnect('redis');
$this->redis->pconnect($_SERVER['REDIS_HOST']);
}
}

0 comments on commit 5365575

Please sign in to comment.