mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:14:05 +00:00
DB driver ensureTenantCanBeCreated
This commit is contained in:
parent
553e4c381b
commit
5fb11dfc9f
5 changed files with 74 additions and 4 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
src/Exceptions/DomainsOccupiedByOtherTenantException.php
Normal file
15
src/Exceptions/DomainsOccupiedByOtherTenantException.php
Normal 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.";
|
||||
}
|
||||
}
|
||||
25
src/Exceptions/TenantWithThisIdAlreadyExistsException.php
Normal file
25
src/Exceptions/TenantWithThisIdAlreadyExistsException.php
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue