Skip to content

Commit

Permalink
refactor: Abstract out tenancy options for hydration to flag class
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Sep 10, 2024
1 parent 2c977b7 commit a5e4797
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
10 changes: 5 additions & 5 deletions resources/config/multitenancy.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Sprout\TenancyOptions;

return [

'defaults' => [
Expand All @@ -15,11 +17,9 @@
'tenants' => [
'provider' => 'tenants',
'options' => [
'hydration' => [
'enabled' => true,
'check' => true,
'strict' => false,
],
TenancyOptions::hydrateTenantRelation(),
TenancyOptions::checkForRelationWithTenant(),
TenancyOptions::throwIfNotRelated(),
],
],

Expand Down
5 changes: 3 additions & 2 deletions src/Database/Eloquent/Relations/BelongsToManyTenants.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RuntimeException;
use Sprout\Contracts\Tenancy;
use Sprout\Support\BaseTenantRelationHandler;
use Sprout\TenancyOptions;

/**
* @template ParentModel of \Illuminate\Database\Eloquent\Model
Expand Down Expand Up @@ -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());
Expand All @@ -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()
Expand Down
69 changes: 69 additions & 0 deletions src/TenancyOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

namespace Sprout;

use Sprout\Contracts\Tenancy;

class TenancyOptions
{
/**
* Tenant relations should be automatically hydrated
*
* @return string
*/
public static function hydrateTenantRelation(): string
{
return 'tenant-relation.hydrate';
}

/**
* Check whether a model relates to the tenant before hydrating
*
* @return string
*/
public static function checkForRelationWithTenant(): string
{
return 'tenant-relation.check';
}

/**
* Throw an exception if the model isn't related to the tenant
*
* @return string
*/
public static function throwIfNotRelated(): string
{
return 'tenant-relation.strict';
}

/**
* @param \Sprout\Contracts\Tenancy<\Sprout\Contracts\Tenant> $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);
}
}

0 comments on commit a5e4797

Please sign in to comment.