diff --git a/src/Contracts/StorageDriver.php b/src/Contracts/StorageDriver.php index ec21b18a..538135e7 100644 --- a/src/Contracts/StorageDriver.php +++ b/src/Contracts/StorageDriver.php @@ -4,26 +4,12 @@ declare(strict_types=1); namespace Stancl\Tenancy\Contracts; +use Stancl\Tenancy\Tenant; + interface StorageDriver { - public function identifyTenant(string $domain): array; - - /** @return array[] */ - public function getAllTenants(array $uuids = []): array; - - public function getTenantById(string $uuid, array $fields = []): array; - - public function getTenantIdByDomain(string $domain): ?string; - - public function createTenant(string $domain, string $uuid): array; - - public function deleteTenant(string $uuid): bool; - - public function get(string $uuid, string $key); - - public function getMany(string $uuid, array $keys): array; - - public function put(string $uuid, string $key, $value); - - public function putMany(string $uuid, array $values): array; + public function createTenant(Tenant $tenant): bool; // todo return type + public function updateTenant(Tenant $tenant): bool; // todo return type + public function findById(string $id): Tenant; + public function findByDomain(string $domain): Tenant; } diff --git a/src/Contracts/Tenant.php b/src/Contracts/Tenant.php deleted file mode 100644 index aa906a11..00000000 --- a/src/Contracts/Tenant.php +++ /dev/null @@ -1,10 +0,0 @@ -app->bind(Tenant::class, function ($app) { + return $app[TenantManager::class]->currentTenant(); + }); // todo foreach bootstrappers, singleton foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) { diff --git a/src/Tenant.php b/src/Tenant.php index 61545ca4..b99670d5 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -5,14 +5,14 @@ declare(strict_types=1); namespace Stancl\Tenancy; use ArrayAccess; -use Stancl\Tenancy\Contracts\Tenant as CurrentTenant; // todo tenant storage +// todo create tenant - create db? /** * @internal Class is subject to breaking changes in minor and patch versions. */ -class Tenant implements ArrayAccess, CurrentTenant +class Tenant implements ArrayAccess { use Traits\HasArrayAccess; @@ -85,7 +85,7 @@ class Tenant implements ArrayAccess, CurrentTenant public function save(): self { if ($this->persisted) { - $this->manager->addTenant($this); + $this->manager->createTenant($this); } else { $this->manager->updateTenant($this); } diff --git a/src/TenantManagerv2.php b/src/TenantManagerv2.php index 11a6cdfa..0498e662 100644 --- a/src/TenantManagerv2.php +++ b/src/TenantManagerv2.php @@ -18,7 +18,7 @@ class TenantManagerv2 * * @var Tenant */ - public $tenant; + protected $tenant; /** @var Application */ private $app; @@ -36,9 +36,9 @@ class TenantManagerv2 $this->storage = $storage; } - public function addTenant(Tenant $tenant): self + public function createTenant(Tenant $tenant): self { - $this->storage->addTenant($tenant); + $this->storage->createTenant($tenant); return $this; } @@ -50,6 +50,34 @@ class TenantManagerv2 return $this; } + // todo @throws + public function init(string $domain): self + { + $this->initializeTenancy($this->findByDomain($domain)); + + return $this; + } + + // todo @throws + public function initById(string $id): self + { + $this->initializeTenancy($this->find($id)); + + return $this; + } + + // todo @throws + public function find(string $id): Tenant + { + return $this->storage->findById($id); + } + + // todo @throws + public function findByDomain(string $domain): Tenant + { + return $this->storage->findByDomain($domain); + } + public function initializeTenancy(Tenant $tenant): self { $this->bootstrapTenancy($tenant); @@ -84,9 +112,18 @@ class TenantManagerv2 return $this; } - public function setTenant(Tenant $tenant): self + public function getTenant(): Tenant { - $this->app->instance(Contracts\Tenant::class, $tenant); + if (! $this->tenant) { + throw new NoTenantIdentifiedException; + } + + return $this->tenant; + } + + protected function setTenant(Tenant $tenant): self + { + $this->tenant = $tenant; return $this; }