1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:14:04 +00:00

DB driver ensureTenantCanBeCreated

This commit is contained in:
Samuel Štancl 2019-09-15 11:50:19 +02:00
parent 553e4c381b
commit 5fb11dfc9f
5 changed files with 74 additions and 4 deletions

View file

@ -12,6 +12,6 @@ abstract class TenantCannotBeCreatedException extends \Exception
public function __construct() public function __construct()
{ {
$this->message = 'Tenant cannot be craeted. Reason: ' . $this->reason(); $this->message = 'Tenant cannot be created. Reason: ' . $this->reason();
} }
} }

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Exceptions;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
class DomainOccupiedByOtherTenantException extends TenantCannotBeCreatedException
{
public function reason(): string
{
return "One or more of the tenant's domains are already occupied by another tenant.";
}
}

View file

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Exceptions;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
class TenantWithThisIdAlreadyExistsException extends TenantCannotBeCreatedException
{
/** @var string */
protected $id;
public function reason(): string
{
return "Tenant with id {$this->id} already exists.";
}
public function __construct(string $id)
{
parent::__construct();
$this->id = $id;
}
}

View file

@ -5,7 +5,9 @@ declare(strict_types=1);
namespace Stancl\Tenancy\StorageDrivers\Database; namespace Stancl\Tenancy\StorageDrivers\Database;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains; use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains;
use Stancl\Tenancy\StorageDrivers\Database\Tenants as Tenants; use Stancl\Tenancy\StorageDrivers\Database\Tenants as Tenants;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
@ -32,7 +34,14 @@ class DatabaseStorageDriver implements StorageDriver
public function ensureTenantCanBeCreated(Tenant $tenant) 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 public function getTenantIdByDomain(string $domain): ?string
@ -56,6 +65,8 @@ class DatabaseStorageDriver implements StorageDriver
public function updateTenant(Tenant $tenant): void public function updateTenant(Tenant $tenant): void
{ {
// todo // todo
// 1. update storage
// 2. update domains
} }
public function deleteTenant(Tenant $tenant): void public function deleteTenant(Tenant $tenant): void
@ -64,29 +75,48 @@ class DatabaseStorageDriver implements StorageDriver
Domains::where('tenant_id', $tenant->id)->delete(); Domains::where('tenant_id', $tenant->id)->delete();
} }
/**
* Get all tenants.
*
* @param string[] $ids
* @return Tenant[]
*/
public function all(array $ids = []): array public function all(array $ids = []): array
{ {
return Tenants::getAllTenants($ids)->toArray(); 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) public function get(string $key, Tenant $tenant = null)
{ {
$tenant = $tenant ?? $this->tenant();
return Tenants::find($tenant->id)->get($key); return Tenants::find($tenant->id)->get($key);
} }
// todo storage methods default to current tenant
public function getMany(array $keys, Tenant $tenant = null): array public function getMany(array $keys, Tenant $tenant = null): array
{ {
$tenant = $tenant ?? $this->tenant();
return Tenants::find($tenant->id)->getMany($keys); return Tenants::find($tenant->id)->getMany($keys);
} }
public function put(string $key, $value, Tenant $tenant = null): void public function put(string $key, $value, Tenant $tenant = null): void
{ {
$tenant = $tenant ?? $this->tenant();
Tenants::find($tenant->id)->put($key, $value); Tenants::find($tenant->id)->put($key, $value);
} }
public function putMany(array $kvPairs, Tenant $tenant = null): void public function putMany(array $kvPairs, Tenant $tenant = null): void
{ {
$tenant = $tenant ?? $this->tenant();
Tenants::find($tenant->id)->putMany($kvPairs); Tenants::find($tenant->id)->putMany($kvPairs);
} }
} }

View file

@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
// todo transactions instead of pipelines?
class RedisStorageDriver implements StorageDriver class RedisStorageDriver implements StorageDriver
{ {
// todo json encoding? // todo json encoding?
@ -114,7 +115,6 @@ class RedisStorageDriver implements StorageDriver
public function all(array $ids = []): array public function all(array $ids = []): array
{ {
// todo $this->redis->pipeline() - return? // todo $this->redis->pipeline() - return?
// todo transaction instead of pipeline?
$hashes = array_map(function ($hash) { $hashes = array_map(function ($hash) {
return "tenants:{$hash}"; return "tenants:{$hash}";
}, $ids); }, $ids);