mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 01:54:04 +00:00
Enhance Syncable and SyncMaster Interfaces to Support Customizable Attribute Synchronization
This commit is contained in:
parent
8f9c7efa45
commit
846776892a
5 changed files with 35 additions and 13 deletions
18
src/Contracts/BaseSyncable.php
Normal file
18
src/Contracts/BaseSyncable.php
Normal 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();
|
||||||
|
}
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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));
|
||||||
|
|
|
||||||
|
|
@ -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 [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue