diff --git a/src/Interfaces/StorageDriver.php b/src/Interfaces/StorageDriver.php index 8dbcf91e..93b3ce80 100644 --- a/src/Interfaces/StorageDriver.php +++ b/src/Interfaces/StorageDriver.php @@ -9,6 +9,7 @@ interface StorageDriver public function getTenantById(string $uuid, array $fields = []): array; public function getTenantIdByDomain(string $domain): ?string; public function createTenant(string $domain, string $uuid): array; + public function aliasTenant(string $aliasDomain, string $uuid): array; public function deleteTenant(string $uuid): bool; public function get(string $uuid, string $key); public function getMany(string $uuid, array $keys): array; diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 10bd15f6..3c449d3c 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -53,6 +53,12 @@ class RedisStorageDriver implements StorageDriver return $this->redis->hgetall("tenants:$uuid"); } + public function aliasTenant(string $aliasDomain, string $uuid): array + { + $this->redis->hmset("domains:$aliasDomain", 'tenant_id', $uuid); + return $this->redis->hgetall("tenants:$uuid"); + } + public function deleteTenant(string $id): bool { try { diff --git a/src/TenantManager.php b/src/TenantManager.php index f305b080..31307cc7 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -83,6 +83,26 @@ class TenantManager return $tenant; } + public function alias(string $domain, string $alias): array + { + $domain = $domain ?: $this->currentDomain(); + + $uuid = $this->storage->getTenantIdByDomain($domain); + + if (!$uuid) { + throw new \Exception("Domain $domain does not exists."); + } + + if ($aliasUuid = $this->storage->getTenantIdByDomain($alias)) { + throw new \Exception("Domain Alias $domain is already occupied in use."); + } + + $tenant = $this->jsonDecodeArrayValues($this->storage->aliasTenant($alias, $uuid)); + + + return $tenant; + } + public function delete(string $uuid): bool { return $this->storage->deleteTenant($uuid);