diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 4166b4bd..4e605b31 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -20,7 +20,7 @@ class RedisStorageDriver implements StorageDriver if (! $id) { throw new \Exception("Tenant could not be identified on domain {$domain}"); } - return array_merge(['uuid' => $id], $this->getTenantById($id)); + return $this->getTenantById($id); } /** @@ -49,7 +49,7 @@ class RedisStorageDriver implements StorageDriver public function createTenant(string $domain, string $uuid): array { $this->redis->hmset("domains:$domain", 'tenant_id', $uuid); - $this->redis->hmset("tenants:$uuid", 'uuid', $uuid, 'domain', $domain); + $this->redis->hmset("tenants:$uuid", 'uuid', json_encode($uuid), 'domain', json_encode($domain)); return $this->redis->hgetall("tenants:$uuid"); } diff --git a/src/TenantManager.php b/src/TenantManager.php index d0acedb8..c19dfa1a 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -16,21 +16,21 @@ class TenantManager * * @var Application */ - private $app; + protected $app; /** * Storage driver for tenant metadata. * * @var StorageDriver */ - private $storage; + protected $storage; /** * Database manager. * * @var DatabaseManager */ - private $database; + protected $database; /** * Current tenant. @@ -78,7 +78,7 @@ class TenantManager throw new \Exception("Domain $domain is already occupied by tenant $id."); } - $tenant = $this->storage->createTenant($domain, \Webpatser\Uuid\Uuid::generate(1, $domain)); + $tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, \Webpatser\Uuid\Uuid::generate(1, $domain))); $this->database->create($this->getDatabaseName($tenant)); return $tenant; @@ -99,7 +99,7 @@ class TenantManager public function getTenantById(string $uuid, $fields = []) { $fields = (array) $fields; - return $this->storage->getTenantById($uuid, $fields); + return $this->jsonDecodeArrayValues($this->storage->getTenantById($uuid, $fields)); } /** @@ -163,8 +163,16 @@ class TenantManager return config('tenancy.database.prefix') . $tenant['uuid'] . config('tenancy.database.suffix'); } + /** + * Set the tenant property to a JSON decoded version of the tenant's data obtained from storage. + * + * @param array $tenant + * @return array + */ public function setTenant(array $tenant): array { + $tenant = $this->jsonDecodeArrayValues($tenant); + $this->tenant = $tenant; return $tenant; @@ -189,7 +197,10 @@ class TenantManager public function all($uuids = []) { $uuid = (array) $uuids; - return collect($this->storage->getAllTenants($uuids)); + + return collect(array_map(function ($tenant_array) { + return $this->jsonDecodeArrayValues($tenant_array); + }, $this->storage->getAllTenants($uuids))); } /** @@ -217,10 +228,10 @@ class TenantManager $uuid = $uuid ?: $this->tenant['uuid']; if (is_array($key)) { - return $this->storage->getMany($uuid, $key); + return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key)); } - return $this->storage->get($uuid, $key); + return json_decode($this->storage->get($uuid, $key)); } /** @@ -248,18 +259,19 @@ class TenantManager } if (! is_null($value)) { - return $target[$key] = $this->storage->put($uuid, $key, $value); + return $target[$key] = json_decode($this->storage->put($uuid, $key, json_encode($value))); } if (! is_array($key)) { throw new \Exception("No value supplied for key $key."); } - foreach ($this->storage->putMany($uuid, $key) as $key => $value) { - $target[$key] = $value; + foreach ($key as $k => $v) { + $target[$k] = $v; + $key[$k] = json_encode($v); } - return $key; + return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key)); } /** @@ -275,6 +287,15 @@ class TenantManager return $this->put($key, $value, $uuid); } + protected function jsonDecodeArrayValues(array $array) + { + array_walk($array, function (&$value, $key) { + $value = json_decode($value); + }); + + return $array; + } + /** * Return the identified tenant's attribute(s). * diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index ac2fe03d..5960ee4a 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -6,14 +6,7 @@ use Stancl\Tenancy\Interfaces\StorageDriver; class TenantStorageTest extends TestCase { - public function setUp() - { - parent::setUp(); - - // todo find a way to run this for each storage driver (once there are more of them) - - $this->storage = app(StorageDriver::class); - } + // todo find a way to run this for each storage driver (once there are more of them) /** @test */ public function deleting_a_tenant_works() @@ -32,7 +25,7 @@ class TenantStorageTest extends TestCase { tenant()->set('foo', 'bar'); - $this->assertSame('bar', $this->storage->get(tenant('uuid'), 'foo')); + $this->assertSame('bar', tenant()->get('foo')); } /** @test */ @@ -40,7 +33,7 @@ class TenantStorageTest extends TestCase { tenancy()->put('foo', 'bar'); - $this->assertSame('bar', $this->storage->get(tenant('uuid'), 'foo')); + $this->assertSame('bar', tenant()->get('foo')); } /** @test */ @@ -91,4 +84,26 @@ class TenantStorageTest extends TestCase $this->assertNotSame($vals, tenancy()->get($keys)); $this->assertFalse(array_intersect($data, tenant()->tenant) == $data); // assert array not subset } + + /** @test */ + public function arrays_can_be_stored() + { + tenant()->put('foo', [1, 2]); + + $this->assertSame([1, 2], tenant()->get('foo')); + } + + /** @test */ + public function put_returns_the_value_when_two_arguments_are_used() + { + $this->assertSame('bar', tenant()->put('foo', 'bar')); + } + + /** @test */ + public function put_returns_the_key_value_pairs_when_a_single_argument_is_used() + { + $value = ['foo' => 'bar', 'abc' => 'xyz']; + + $this->assertSame($value, tenancy()->put($value)); + } }