From ada332c7cd74d5477ce1b3d4d826a9a0a5d1cacb Mon Sep 17 00:00:00 2001 From: Andre Smith Date: Mon, 14 Jan 2019 13:08:37 +0200 Subject: [PATCH] Introducing ability to specify a namespace for redis storage (#10) --- README.md | 2 +- src/Storage/RedisStorage.php | 17 ++++++++++++++--- tests/Storage/RedisStorageTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5055a43..696106e 100644 --- a/README.md +++ b/README.md @@ -369,7 +369,7 @@ To enable toggler inside symfony, register the bundle $bundles = array( ... - new Toggler\Symfony\TogglerBundle(), + new SolidWorx\Toggler\Symfony\TogglerBundle(), ... ); ``` diff --git a/src/Storage/RedisStorage.php b/src/Storage/RedisStorage.php index 3dd8aee..0deb721 100644 --- a/src/Storage/RedisStorage.php +++ b/src/Storage/RedisStorage.php @@ -20,13 +20,19 @@ class RedisStorage implements PersistenStorageInterface */ private $redis; - public function __construct($redis) + /** + * @var string + */ + private $namespace; + + public function __construct($redis, ?string $namespace = null) { if (!$redis instanceof \Redis && !$redis instanceof \RedisArray && !$redis instanceof \RedisCluster && !$redis instanceof \Predis\Client) { throw new \InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, is_object($redis) ? get_class($redis) : gettype($redis))); } $this->redis = $redis; + $this->namespace = $namespace; } /** @@ -34,7 +40,7 @@ public function __construct($redis) */ public function get(string $key) { - return $this->redis->get($key); + return $this->redis->get($this->generateKey($key)); } /** @@ -42,6 +48,11 @@ public function get(string $key) */ public function set(string $key, bool $value) { - return $this->redis->set($key, $value); + return $this->redis->set($this->generateKey($key), $value); + } + + private function generateKey(string $key): string + { + return $this->namespace ? "{$this->namespace}:$key" : $key; } } diff --git a/tests/Storage/RedisStorageTest.php b/tests/Storage/RedisStorageTest.php index 93a976a..003d3e5 100644 --- a/tests/Storage/RedisStorageTest.php +++ b/tests/Storage/RedisStorageTest.php @@ -59,4 +59,34 @@ public function testSet() $storage->set('foobar', false); } + + public function testGetWithNamespace() + { + $namespace = 'fooNamespace'; + $this->redis->expects($this->at(0)) + ->method('get') + ->with($namespace.':foobar') + ->willReturn(true); + + $this->redis->expects($this->at(1)) + ->method('get') + ->with($namespace.':baz'); + + $storage = new RedisStorage($this->redis, $namespace); + + $this->assertTrue($storage->get('foobar')); + $this->assertNull($storage->get('baz')); + } + + public function testSetWithNamespace() + { + $namespace = 'fooNamespace'; + $this->redis->expects($this->at(0)) + ->method('set') + ->with($namespace.':foobar', false); + + $storage = new RedisStorage($this->redis, $namespace); + + $storage->set('foobar', false); + } }