From 23a4fd24b0b801f78bb99f3ec9372f46a28a4569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 11 Sep 2019 20:57:30 +0200 Subject: [PATCH] StorageDrivers\Database domain --- .../Database/DatabaseStorageDriver.php | 84 +++++++++++++++++ src/StorageDrivers/Database/DomainModel.php | 23 +++++ .../Database}/TenantModel.php | 8 +- src/StorageDrivers/DatabaseStorageDriver.php | 90 ------------------- src/StorageDrivers/RedisStorageDriver.php | 2 + 5 files changed, 113 insertions(+), 94 deletions(-) create mode 100644 src/StorageDrivers/Database/DatabaseStorageDriver.php create mode 100644 src/StorageDrivers/Database/DomainModel.php rename src/{ => StorageDrivers/Database}/TenantModel.php (92%) delete mode 100644 src/StorageDrivers/DatabaseStorageDriver.php diff --git a/src/StorageDrivers/Database/DatabaseStorageDriver.php b/src/StorageDrivers/Database/DatabaseStorageDriver.php new file mode 100644 index 00000000..28f2327e --- /dev/null +++ b/src/StorageDrivers/Database/DatabaseStorageDriver.php @@ -0,0 +1,84 @@ +getTenantIdByDomain($domain); + if (! $id) { + throw new TenantCouldNotBeIdentifiedException($domain); + } + + return $this->find($id); + } + + public function find(string $id): Tenant + { + return Tenant::fromStorage(Tenants::find($id)->decoded()) + ->withDomains(Domains::where('tenant_id', $id)->all()->only('domain')->toArray()); + } + + public function getTenantIdByDomain(string $domain): ?string + { + return Domains::where('domain', $domain)->first()->tenant_id ?? null; + } + + public function createTenant(string $domain, string $id): void + { + DB::transaction(function () use ($domain, $id) { + Tenants::create(['id' => $id, 'data' => '{}'])->toArray(); + Domains::create(['domain' => $domain, 'tenant_id' => $id]); + }); + } + + public function updateTenant(Tenant $tenant): void + { + // todo + } + + public function deleteTenant(string $id): bool + { + return Tenants::find($id)->delete(); + } + + public function all(array $ids = []): array + { + return Tenants::getAllTenants($ids)->toArray(); + } + + public function get(string $id, string $key) + { + return Tenants::find($id)->get($key); + } + + public function getMany(string $id, array $keys): array + { + return Tenants::find($id)->getMany($keys); + } + + public function put(string $id, string $key, $value) + { + return Tenants::find($id)->put($key, $value); + } + + public function putMany(string $id, array $values): array + { + foreach ($values as $key => $value) { // todo performance + Tenants::find($id)->put($key, $value); + } + + return $values; + } +} diff --git a/src/StorageDrivers/Database/DomainModel.php b/src/StorageDrivers/Database/DomainModel.php new file mode 100644 index 00000000..b4351248 --- /dev/null +++ b/src/StorageDrivers/Database/DomainModel.php @@ -0,0 +1,23 @@ +originalDefaultConnectionName; + } +} diff --git a/src/TenantModel.php b/src/StorageDrivers/Database/TenantModel.php similarity index 92% rename from src/TenantModel.php rename to src/StorageDrivers/Database/TenantModel.php index 4ddc2c6d..81ed5c24 100644 --- a/src/TenantModel.php +++ b/src/StorageDrivers/Database/TenantModel.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Stancl\Tenancy; +namespace Stancl\Tenancy\StorageDrivers\Database; use Illuminate\Database\Eloquent\Model; @@ -31,9 +31,9 @@ class TenantModel extends Model return config('tenancy.storage.db.connection') ?? app(DatabaseManager::class)->originalDefaultConnectionName; } - public static function getAllTenants(array $uuids) + public static function getAllTenants(array $ids) { - $tenants = $uuids ? static::findMany($uuids) : static::all(); + $tenants = $ids ? static::findMany($ids) : static::all(); return $tenants->map([__CLASS__, 'decodeData'])->toBase(); } @@ -46,7 +46,7 @@ class TenantModel extends Model /** * Return a tenant array with data decoded into separate keys. * - * @param Tenant|array $tenant + * @param self|array $tenant * @return array */ public static function decodeData($tenant) diff --git a/src/StorageDrivers/DatabaseStorageDriver.php b/src/StorageDrivers/DatabaseStorageDriver.php deleted file mode 100644 index 9f48a74a..00000000 --- a/src/StorageDrivers/DatabaseStorageDriver.php +++ /dev/null @@ -1,90 +0,0 @@ -getTenantIdByDomain($domain); - if (! $id) { - throw new TenantCouldNotBeIdentifiedException($domain); - } - - return $this->getTenantById($id); - } - - /** - * Get information about the tenant based on his uuid. - * - * @param string $uuid - * @param array $fields - * @return array - */ - public function getTenantById(string $uuid, array $fields = []): array - { - if ($fields) { - return Tenant::decodeData(Tenant::find($uuid)->only($fields)); - } else { - return Tenant::find($uuid)->decoded(); - } - } - - public function getTenantIdByDomain(string $domain): ?string - { - return Tenant::where('domain', $domain)->first()->uuid ?? null; - } - - public function createTenant(string $domain, string $uuid): array - { - $tenant = Tenant::create(['uuid' => $uuid, 'domain' => $domain, 'data' => '{}'])->toArray(); - unset($tenant['data']); - - return $tenant; - } - - public function deleteTenant(string $id): bool - { - return Tenant::find($id)->delete(); - } - - public function getAllTenants(array $uuids = []): array - { - return Tenant::getAllTenants($uuids)->toArray(); - } - - public function get(string $uuid, string $key) - { - return Tenant::find($uuid)->get($key); - } - - public function getMany(string $uuid, array $keys): array - { - return Tenant::find($uuid)->getMany($keys); - } - - public function put(string $uuid, string $key, $value) - { - return Tenant::find($uuid)->put($key, $value); - } - - public function putMany(string $uuid, array $values): array - { - foreach ($values as $key => $value) { - Tenant::find($uuid)->put($key, $value); - } - - return $values; - } -} diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 70fb1891..95f9bb71 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -12,6 +12,8 @@ use Stancl\Tenancy\Tenant; class RedisStorageDriver implements StorageDriver { + // todo json encoding? + /** @var Application */ protected $app;