diff --git a/src/Contracts/TenantCannotBeCreatedException.php b/src/Contracts/TenantCannotBeCreatedException.php index 17de2e06..19eac15b 100644 --- a/src/Contracts/TenantCannotBeCreatedException.php +++ b/src/Contracts/TenantCannotBeCreatedException.php @@ -12,6 +12,6 @@ abstract class TenantCannotBeCreatedException extends \Exception public function __construct() { - $this->message = 'Tenant cannot be craeted. Reason: ' . $this->reason(); + $this->message = 'Tenant cannot be created. Reason: ' . $this->reason(); } } diff --git a/src/Exceptions/DomainsOccupiedByOtherTenantException.php b/src/Exceptions/DomainsOccupiedByOtherTenantException.php new file mode 100644 index 00000000..3c7be5fd --- /dev/null +++ b/src/Exceptions/DomainsOccupiedByOtherTenantException.php @@ -0,0 +1,15 @@ +id} already exists."; + } + + public function __construct(string $id) + { + parent::__construct(); + + $this->id = $id; + } +} diff --git a/src/StorageDrivers/Database/DatabaseStorageDriver.php b/src/StorageDrivers/Database/DatabaseStorageDriver.php index b82f2cd9..80984781 100644 --- a/src/StorageDrivers/Database/DatabaseStorageDriver.php +++ b/src/StorageDrivers/Database/DatabaseStorageDriver.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace Stancl\Tenancy\StorageDrivers\Database; use Stancl\Tenancy\Contracts\StorageDriver; +use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; +use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException; use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains; use Stancl\Tenancy\StorageDrivers\Database\Tenants as Tenants; use Stancl\Tenancy\Tenant; @@ -32,7 +34,14 @@ class DatabaseStorageDriver implements StorageDriver public function ensureTenantCanBeCreated(Tenant $tenant) { - // todo + // todo test this + if (Tenants::find($tenant->id)) { + throw new TenantWithThisIdAlreadyExistsException($tenant->id); + } + + if (Domains::whereIn('domain', [$tenant->domains])->exists()) { + throw new DomainOccupiedByOtherTenantException(); + } } public function getTenantIdByDomain(string $domain): ?string @@ -56,6 +65,8 @@ class DatabaseStorageDriver implements StorageDriver public function updateTenant(Tenant $tenant): void { // todo + // 1. update storage + // 2. update domains } public function deleteTenant(Tenant $tenant): void @@ -64,29 +75,48 @@ class DatabaseStorageDriver implements StorageDriver Domains::where('tenant_id', $tenant->id)->delete(); } + /** + * Get all tenants. + * + * @param string[] $ids + * @return Tenant[] + */ public function all(array $ids = []): array { return Tenants::getAllTenants($ids)->toArray(); } + /** + * Get the current tenant. + * + * @return Tenant + */ + protected function tenant() + { + return $this->app[Tenant::class]; + } + public function get(string $key, Tenant $tenant = null) { + $tenant = $tenant ?? $this->tenant(); return Tenants::find($tenant->id)->get($key); } - // todo storage methods default to current tenant public function getMany(array $keys, Tenant $tenant = null): array { + $tenant = $tenant ?? $this->tenant(); return Tenants::find($tenant->id)->getMany($keys); } public function put(string $key, $value, Tenant $tenant = null): void { + $tenant = $tenant ?? $this->tenant(); Tenants::find($tenant->id)->put($key, $value); } public function putMany(array $kvPairs, Tenant $tenant = null): void { + $tenant = $tenant ?? $this->tenant(); Tenants::find($tenant->id)->putMany($kvPairs); } } diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 93caf54e..b8a7ddde 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Tenant; +// todo transactions instead of pipelines? class RedisStorageDriver implements StorageDriver { // todo json encoding? @@ -114,7 +115,6 @@ class RedisStorageDriver implements StorageDriver public function all(array $ids = []): array { // todo $this->redis->pipeline() - return? - // todo transaction instead of pipeline? $hashes = array_map(function ($hash) { return "tenants:{$hash}"; }, $ids);