mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 01:44:04 +00:00
DB driver ensureTenantCanBeCreated
This commit is contained in:
parent
553e4c381b
commit
5fb11dfc9f
5 changed files with 74 additions and 4 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue