1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 05:14:03 +00:00

Enhance Syncable and SyncMaster Interfaces to Support Customizable Attribute Synchronization

This commit is contained in:
Dipak Sarkar 2024-08-22 13:27:12 +05:30
parent 8f9c7efa45
commit 846776892a
5 changed files with 35 additions and 13 deletions

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Contracts;
interface BaseSyncable
{
public function getGlobalIdentifierKeyName(): string;
public function getGlobalIdentifierKey();
public function getCentralModelName(): string;
public function getSyncedAttributeNames(): array;
public function triggerSyncEvent();
}

View file

@ -10,9 +10,11 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
/** /**
* @property-read Tenant[]|Collection $tenants * @property-read Tenant[]|Collection $tenants
*/ */
interface SyncMaster extends Syncable interface SyncMaster extends BaseSyncable
{ {
public function tenants(): BelongsToMany; public function tenants(): BelongsToMany;
public function getTenantModelName(): string; public function getTenantModelName(): string;
public function getTenantModelFillable(): array;
} }

View file

@ -4,15 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Contracts; namespace Stancl\Tenancy\Contracts;
interface Syncable interface Syncable extends BaseSyncable
{ {
public function getGlobalIdentifierKeyName(): string; public function getCentralModelFillable(): array;
public function getGlobalIdentifierKey();
public function getCentralModelName(): string;
public function getSyncedAttributeNames(): array;
public function triggerSyncEvent();
} }

View file

@ -60,7 +60,7 @@ class UpdateSyncedResource extends QueueableListener
} else { } else {
// If the resource doesn't exist at all in the central DB,we create // If the resource doesn't exist at all in the central DB,we create
// the record with all attributes, not just the synced ones. // the record with all attributes, not just the synced ones.
$centralModel = $event->model->getCentralModelName()::create($event->model->getAttributes()); $centralModel = $event->model->getCentralModelName()::create($event->model->only($event->model->getCentralModelFillable()));
event(new SyncedResourceChangedInForeignDatabase($event->model, null)); event(new SyncedResourceChangedInForeignDatabase($event->model, null));
} }
}); });
@ -113,7 +113,7 @@ class UpdateSyncedResource extends QueueableListener
$localModel->update($syncedAttributes); $localModel->update($syncedAttributes);
} else { } else {
// When creating, we use all columns, not just the synced ones. // When creating, we use all columns, not just the synced ones.
$localModel = $localModelClass::create($eventModel->getAttributes()); $localModel = $localModelClass::create($eventModel->only($eventModel->getTenantModelFillable()));
} }
event(new SyncedResourceChangedInForeignDatabase($localModel, $tenant)); event(new SyncedResourceChangedInForeignDatabase($localModel, $tenant));

View file

@ -603,6 +603,11 @@ class CentralUser extends Model implements SyncMaster
return ResourceUser::class; return ResourceUser::class;
} }
public function getTenantModelFillable(): array
{
return (new ResourceUser)->getFillable();
}
public function getGlobalIdentifierKey() public function getGlobalIdentifierKey()
{ {
return $this->getAttribute($this->getGlobalIdentifierKeyName()); return $this->getAttribute($this->getGlobalIdentifierKeyName());
@ -651,6 +656,11 @@ class ResourceUser extends Model implements Syncable
return CentralUser::class; return CentralUser::class;
} }
public function getCentralModelFillable(): array
{
return (new CentralUser)->getFillable();
}
public function getSyncedAttributeNames(): array public function getSyncedAttributeNames(): array
{ {
return [ return [