From a5e47979f64e19086c4d76c5892a1d52a5aa7cde Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Tue, 10 Sep 2024 22:42:28 +0100 Subject: [PATCH] refactor: Abstract out tenancy options for hydration to flag class --- resources/config/multitenancy.php | 10 +-- .../Relations/BelongsToManyTenants.php | 5 +- src/TenancyOptions.php | 69 +++++++++++++++++++ 3 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/TenancyOptions.php diff --git a/resources/config/multitenancy.php b/resources/config/multitenancy.php index 7105928..e352ad6 100644 --- a/resources/config/multitenancy.php +++ b/resources/config/multitenancy.php @@ -1,5 +1,7 @@ [ @@ -15,11 +17,9 @@ 'tenants' => [ 'provider' => 'tenants', 'options' => [ - 'hydration' => [ - 'enabled' => true, - 'check' => true, - 'strict' => false, - ], + TenancyOptions::hydrateTenantRelation(), + TenancyOptions::checkForRelationWithTenant(), + TenancyOptions::throwIfNotRelated(), ], ], diff --git a/src/Database/Eloquent/Relations/BelongsToManyTenants.php b/src/Database/Eloquent/Relations/BelongsToManyTenants.php index 0087cd8..86a5b84 100644 --- a/src/Database/Eloquent/Relations/BelongsToManyTenants.php +++ b/src/Database/Eloquent/Relations/BelongsToManyTenants.php @@ -9,6 +9,7 @@ use RuntimeException; use Sprout\Contracts\Tenancy; use Sprout\Support\BaseTenantRelationHandler; +use Sprout\TenancyOptions; /** * @template ParentModel of \Illuminate\Database\Eloquent\Model @@ -112,7 +113,7 @@ public function hydrateRelation(ParentModel $model, Tenancy $tenancy): void // If the tenancy is configured to do a hydration check, we'll need to run // a query to see if this model IS related to the tenant - if ($tenancy->option('hydration.check', true)) { + if (TenancyOptions::shouldCheckForRelationWithTenant($tenancy)) { // Run the query $exists = $model->newQuery()->whereHas($this->getRelationName(), function (Builder $query) use ($tenant) { $query->whereKey($tenant->getTenantKey()); @@ -122,7 +123,7 @@ public function hydrateRelation(ParentModel $model, Tenancy $tenancy): void if (! $exists) { // If the hydration is set to be strict for the current tenancy, // we'll need an exception - if ($tenancy->option('hydration.strict', false)) { + if (TenancyOptions::shouldThrowIfNotRelated($tenancy)) { // TODO: Abstract out to specific exception throw new RuntimeException( 'Child model [' . $model::class . '::' . $model->getKey() diff --git a/src/TenancyOptions.php b/src/TenancyOptions.php new file mode 100644 index 0000000..c189e15 --- /dev/null +++ b/src/TenancyOptions.php @@ -0,0 +1,69 @@ + $tenancy + * + * @return bool + */ + public static function shouldHydrateTenantRelation(Tenancy $tenancy): bool + { + return (bool)$tenancy->option(static::hydrateTenantRelation(), true); + } + + /** + * @param \Sprout\Contracts\Tenancy<\Sprout\Contracts\Tenant> $tenancy + * + * @return bool + */ + public static function shouldCheckForRelationWithTenant(Tenancy $tenancy): bool + { + return (bool)$tenancy->option(static::checkForRelationWithTenant(), true); + } + + /** + * @param \Sprout\Contracts\Tenancy<\Sprout\Contracts\Tenant> $tenancy + * + * @return bool + */ + public static function shouldThrowIfNotRelated(Tenancy $tenancy): bool + { + return (bool)$tenancy->option(static::throwIfNotRelated(), false); + } +}