diff --git a/src/Contracts/StorageDriver.php b/src/Contracts/StorageDriver.php index 60b1801c..6cb526a3 100644 --- a/src/Contracts/StorageDriver.php +++ b/src/Contracts/StorageDriver.php @@ -29,4 +29,12 @@ interface StorageDriver * @return Tenant */ public function findByDomain(string $domain): Tenant; + + /** + * Check if a tenant can be created. + * + * @param Tenant $tenant + * @return true|TenantCannotBeCreatedException + */ + public function canCreate(Tenant $tenant); } diff --git a/src/Contracts/TenantCannotBeCreatedException.php b/src/Contracts/TenantCannotBeCreatedException.php new file mode 100644 index 00000000..2bfae8e7 --- /dev/null +++ b/src/Contracts/TenantCannotBeCreatedException.php @@ -0,0 +1,15 @@ +message = 'Tenant cannot be craeted. Reason: ' . $this->reason(); + } +} \ No newline at end of file diff --git a/src/DatabaseManagerv2.php b/src/DatabaseManagerv2.php index 0d648e01..e5894f7e 100644 --- a/src/DatabaseManagerv2.php +++ b/src/DatabaseManagerv2.php @@ -57,4 +57,15 @@ class DatabaseManagerv2 $this->database->reconnect($connection); $this->database->setDefaultConnection($connection); } + + /** + * Check if a tenant can be created. + * + * @param Tenant $tenant + * @return true|TenantCannotBeCreatedException + */ + public function canCreate(Tenant $tenant) + { + // todo + } } diff --git a/src/TenantManagerv2.php b/src/TenantManagerv2.php index 23a00c4c..1ef50505 100644 --- a/src/TenantManagerv2.php +++ b/src/TenantManagerv2.php @@ -5,6 +5,8 @@ declare(strict_types=1); namespace Stancl\Tenancy; use Illuminate\Foundation\Application; +use Illuminate\Contracts\Console\Kernel as Artisan; +use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException; use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; @@ -30,7 +32,7 @@ class TenantManagerv2 /** @var callable[][] */ protected $callbacks = []; - public function __construct(Application $app, Contracts\StorageDriver $storage) + public function __construct(Application $app, Contracts\StorageDriver $storage, Artisan $artisan) { $this->app = $app; $this->storage = $storage; @@ -40,14 +42,38 @@ class TenantManagerv2 public function createTenant(Tenant $tenant): self { - // todo make this atomic + $this->ensureTenantCanBeCreated($tenant); + $this->storage->createTenant($tenant); $this->database->create($tenant); - // todo create database, optionally migrate + + if ($this->migrateAfterCreation()) { + $this->artisan->call('tenants:migrate', [ + '--tenants' => [$tenant['id']], + ]); + } return $this; } + /** + * Ensure that a tenant can be created. + * + * @param Tenant $tenant + * @return void + * @throws TenantCannotBeCreatedException + */ + public function ensureTenantCanBeCreated(Tenant $tenant): void + { + if (($e = $this->storage->canCreateTenant($tenant)) instanceof TenantCannotBeCreatedException) { + throw new $e; + } + + if (($e = $this->database->canCreateTenant($tenant)) instanceof TenantCannotBeCreatedException) { + throw new $e; + } + } + public function updateTenant(Tenant $tenant): self { $this->storage->updateTenant($tenant); @@ -169,6 +195,11 @@ class TenantManagerv2 return array_key_diff($this->app['config']['tenancy.bootstrappers'], $except); } + public function migrateAfterCreation(): bool + { + return $this->app['config']['tenancy.migrate_after_creation'] ?? false; + } + /** * Add an event callback. *