From 24923452808dc831860173ebff720f7f9979a20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 10 May 2020 20:20:44 +0200 Subject: [PATCH] Add methods to Tenant contract --- src/Contracts/Tenant.php | 5 ++++- src/Database/Models/Tenant.php | 14 +++++++++++--- src/Tenancy.php | 6 +++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Contracts/Tenant.php b/src/Contracts/Tenant.php index dcb3a289..ce7a7d98 100644 --- a/src/Contracts/Tenant.php +++ b/src/Contracts/Tenant.php @@ -3,4 +3,7 @@ namespace Stancl\Tenancy\Contracts; interface Tenant -{} \ No newline at end of file +{ + public function getTenantKeyName(): string; + public function getTenantKey(): string; +} \ No newline at end of file diff --git a/src/Database/Models/Tenant.php b/src/Database/Models/Tenant.php index c95e5382..c24224f1 100644 --- a/src/Database/Models/Tenant.php +++ b/src/Database/Models/Tenant.php @@ -7,7 +7,6 @@ use Stancl\Tenancy\DatabaseConfig; use Stancl\Tenancy\Events; use Stancl\Tenancy\Contracts; -// todo use a contract // todo @property class Tenant extends Model implements Contracts\Tenant { @@ -16,6 +15,17 @@ class Tenant extends Model implements Contracts\Tenant } public $primaryKey = 'id'; + public $guarded = []; + + public function getTenantKeyName(): string + { + return 'id'; + } + + public function getTenantKey(): string + { + return $this->getAttribute($this->getTenantKeyName()); + } public function getCasts() { @@ -29,8 +39,6 @@ class Tenant extends Model implements Contracts\Tenant return config('tenancy.id_generator') === null; } - public $guarded = []; - public static function internalPrefix(): string { return config('tenancy.database_prefix'); diff --git a/src/Tenancy.php b/src/Tenancy.php index bca42a5a..ac7fc90f 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -3,7 +3,7 @@ namespace Stancl\Tenancy; use Stancl\Tenancy\Contracts\TenancyBootstrapper; -use Stancl\Tenancy\Database\Models\Tenant; // todo contract +use Stancl\Tenancy\Contracts\Tenant; class Tenancy { @@ -18,8 +18,7 @@ class Tenancy public function initialize(Tenant $tenant): void { - // todo the id is something that should be on the contract, with a method - if ($this->initialized && $this->tenant->id === $tenant->id) { + if ($this->initialized && $this->tenant->getTenantKey() === $tenant->getTenantKey()) { return; } @@ -42,6 +41,7 @@ class Tenancy /** @return TenancyBootstrapper[] */ public function getBootstrappers(): array { + // If no callback for getting bootstrappers is set, we just return all of them. $resolve = static::$getBootstrappers ?? function (Tenant $tenant) { return config('tenancy.bootstrappers'); };