mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 09:34:04 +00:00
Ditch models for custom repositories
This commit is contained in:
parent
58f488784f
commit
a7cdfdacb4
7 changed files with 284 additions and 274 deletions
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Illuminate\Config\Repository as ConfigRepository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanDeleteKeys;
|
||||
|
|
@ -13,8 +15,6 @@ use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
|||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||
use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
|
||||
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
||||
use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains;
|
||||
use Stancl\Tenancy\StorageDrivers\Database\TenantModel as Tenants;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
||||
|
|
@ -22,24 +22,32 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
|||
/** @var Application */
|
||||
protected $app;
|
||||
|
||||
/** @var \Illuminate\Database\Connection */
|
||||
/** @var Connection */
|
||||
protected $centralDatabase;
|
||||
|
||||
/** @var TenantRepository */
|
||||
protected $tenants;
|
||||
|
||||
/** @var DomainRepository */
|
||||
protected $domains;
|
||||
|
||||
/** @var Tenant The default tenant. */
|
||||
protected $tenant;
|
||||
|
||||
public function __construct(Application $app)
|
||||
public function __construct(Application $app, ConfigRepository $config)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->centralDatabase = $this->getCentralConnection();
|
||||
$this->tenants = new TenantRepository($this->centralDatabase, $config);
|
||||
$this->domains = new DomainRepository($this->centralDatabase, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the central database connection.
|
||||
*
|
||||
* @return \Illuminate\Database\Connection
|
||||
* @return Connection
|
||||
*/
|
||||
public static function getCentralConnection(): \Illuminate\Database\Connection
|
||||
public static function getCentralConnection(): Connection
|
||||
{
|
||||
return DB::connection(static::getCentralConnectionName());
|
||||
}
|
||||
|
|
@ -51,7 +59,7 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
|||
|
||||
public function findByDomain(string $domain): Tenant
|
||||
{
|
||||
$id = $this->getTenantIdByDomain($domain);
|
||||
$id = $this->domains->getTenantIdByDomain($domain);
|
||||
if (! $id) {
|
||||
throw new TenantCouldNotBeIdentifiedException($domain);
|
||||
}
|
||||
|
|
@ -61,14 +69,14 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
|||
|
||||
public function findById(string $id): Tenant
|
||||
{
|
||||
$tenant = Tenants::find($id);
|
||||
$tenant = $this->tenants->find($id);
|
||||
|
||||
if (! $tenant) {
|
||||
throw new TenantDoesNotExistException($id);
|
||||
}
|
||||
|
||||
return Tenant::fromStorage($tenant->decoded())
|
||||
->withDomains($this->getTenantDomains($id));
|
||||
return Tenant::fromStorage($this->tenants->decodeData($tenant))
|
||||
->withDomains($this->domains->getTenantDomains($id));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -81,31 +89,24 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
|||
*/
|
||||
public function findBy(string $key, $value): Tenant
|
||||
{
|
||||
// The key has to be a custom column. It's recommended to set up an index
|
||||
$tenant = Tenants::where($key, $value)->first();
|
||||
// The key has to be a custom column. It's recommended to set up an index // TODO can we query JSON?
|
||||
$tenant = $this->tenants->where($key, $value)->first()->toArray();
|
||||
|
||||
if (! $tenant) {
|
||||
throw new TenantDoesNotExistException($value, $key);
|
||||
}
|
||||
|
||||
return Tenant::fromStorage($tenant->decoded())
|
||||
->withDomains($this->getTenantDomains($tenant->id));
|
||||
}
|
||||
|
||||
protected function getTenantDomains($id)
|
||||
{
|
||||
return Domains::where('tenant_id', $id)->get()->map(function ($model) {
|
||||
return $model->domain;
|
||||
})->toArray();
|
||||
->withDomains($this->domains->getTenantDomains($tenant->id));
|
||||
}
|
||||
|
||||
public function ensureTenantCanBeCreated(Tenant $tenant): void
|
||||
{
|
||||
if (Tenants::find($tenant->id)) {
|
||||
if ($this->tenants->exists($tenant)) {
|
||||
throw new TenantWithThisIdAlreadyExistsException($tenant->id);
|
||||
}
|
||||
|
||||
if (Domains::whereIn('domain', $tenant->domains)->exists()) {
|
||||
if ($this->domains->occupied($tenant->domains)) {
|
||||
throw new DomainsOccupiedByOtherTenantException;
|
||||
}
|
||||
}
|
||||
|
|
@ -117,53 +118,32 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function getTenantIdByDomain(string $domain): ?string
|
||||
{
|
||||
return Domains::where('domain', $domain)->first()->tenant_id ?? null;
|
||||
}
|
||||
|
||||
public function createTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::create(array_merge(Tenants::encodeData($tenant->data), [
|
||||
'id' => $tenant->id,
|
||||
]))->toArray();
|
||||
$this->tenants->insert(array_merge(
|
||||
$this->tenants->encodeData($tenant->data),
|
||||
[] // ['id' => $tenant->id] // todo remove this line if things work
|
||||
));
|
||||
|
||||
$domainData = [];
|
||||
foreach ($tenant->domains as $domain) {
|
||||
$domainData[] = ['domain' => $domain, 'tenant_id' => $tenant->id];
|
||||
}
|
||||
|
||||
Domains::insert($domainData);
|
||||
$this->domains->insertTenantDomains($tenant);
|
||||
});
|
||||
}
|
||||
|
||||
public function updateTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::find($tenant->id)->putMany($tenant->data);
|
||||
$this->tenants->updateTenant($tenant);
|
||||
|
||||
$original_domains = Domains::where('tenant_id', $tenant->id)->get()->map(function ($model) {
|
||||
return $model->domain;
|
||||
})->toArray();
|
||||
$deleted_domains = array_diff($original_domains, $tenant->domains);
|
||||
|
||||
Domains::whereIn('domain', $deleted_domains)->delete();
|
||||
|
||||
foreach ($tenant->domains as $domain) {
|
||||
Domains::firstOrCreate([
|
||||
'tenant_id' => $tenant->id,
|
||||
'domain' => $domain,
|
||||
]);
|
||||
}
|
||||
$this->domains->updateTenantDomains($tenant);
|
||||
});
|
||||
}
|
||||
|
||||
public function deleteTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::find($tenant->id)->delete();
|
||||
Domains::where('tenant_id', $tenant->id)->delete();
|
||||
$this->tenants->find($tenant)->delete();
|
||||
$this->domains->where('tenant_id', $tenant->id)->delete();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -175,8 +155,8 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
|||
*/
|
||||
public function all(array $ids = []): array
|
||||
{
|
||||
return Tenants::getAllTenants($ids)->map(function ($data) {
|
||||
return Tenant::fromStorage($data)->withDomains($this->getTenantDomains($data['id']));
|
||||
return $this->tenants->all($ids)->map(function ($data) {
|
||||
return Tenant::fromStorage($data)->withDomains($this->domains->getTenantDomains($data['id']));
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
|
|
@ -192,53 +172,26 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys
|
|||
|
||||
public function get(string $key, Tenant $tenant = null)
|
||||
{
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
|
||||
return Tenants::find($tenant->id)->get($key);
|
||||
return $this->tenants->get($key, $tenant ?? $this->currentTenant());
|
||||
}
|
||||
|
||||
public function getMany(array $keys, Tenant $tenant = null): array
|
||||
{
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
|
||||
return Tenants::find($tenant->id)->getMany($keys);
|
||||
return $this->tenants->getMany($keys, $tenant ?? $this->currentTenant());
|
||||
}
|
||||
|
||||
public function put(string $key, $value, Tenant $tenant = null): void
|
||||
{
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
Tenants::find($tenant->id)->put($key, $value);
|
||||
$this->tenants->put($key, $value, $tenant ?? $this->currentTenant());
|
||||
}
|
||||
|
||||
public function putMany(array $kvPairs, Tenant $tenant = null): void
|
||||
{
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
Tenants::find($tenant->id)->putMany($kvPairs);
|
||||
$this->tenants->putMany($kvPairs, $tenant ?? $this->currentTenant());
|
||||
}
|
||||
|
||||
public function deleteMany(array $keys, Tenant $tenant = null): void
|
||||
{
|
||||
$tenant = $tenant ?? $this->currentTenant();
|
||||
Tenants::find($tenant->id)->deleteMany($keys);
|
||||
$this->tenants->deleteMany($keys, $tenant ?? $this->currentTenant());
|
||||
}
|
||||
}
|
||||
|
||||
class TenantModelTODO
|
||||
{
|
||||
public static function makeTenant($data, $domains)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
public static function findBy(string $key, $value)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
// TODO Use this w/ DB builder calls instead of an Eloquent model
|
||||
}
|
||||
|
||||
class DomainModelTODO
|
||||
{
|
||||
// TODO Use this w/ DB builder calls instead of an Eloquent model
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue