Skip to content

Commit

Permalink
Sync with Platform Relation
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 9, 2024
1 parent 7cbefea commit d7511b9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 20 deletions.
9 changes: 9 additions & 0 deletions packages/sync/src/Services/PlatformRelationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ public function modelHasPlatform($model, Platform $platform)
'platform_id' => $platform->id,
])->exists();
}

public function checkPlatformRelationForModel($modelClass, $modelId, $platformId)
{
return DB::table('model_platform')
->where('model_type', $modelClass)
->where('model_id', $modelId)
->where('platform_id', $platformId)
->exists();
}
}
86 changes: 66 additions & 20 deletions packages/sync/src/Services/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ protected function updatePlatform(array $platformData, Platform $targetPlatform)

protected function syncToSinglePlatform($modelClass, array $modelData, string $eventType, Platform $sourcePlatform, Platform $targetPlatform)
{
if ($this->shouldSyncModel($modelClass, $targetPlatform)) {
$this->processSyncEvent($modelClass, $modelData, $eventType, $targetPlatform);
if ($this->shouldSyncModel($modelClass, $modelData, $targetPlatform, $this->platformRelationService->getSync())) {
$this->processSyncEvent($modelClass, $modelData, $eventType, $targetPlatform, $this->platformRelationService->getSync());
}
}

Expand All @@ -72,14 +72,20 @@ protected function syncToAllPlatforms($modelClass, array $modelData, string $eve
$targetPlatforms = Platform::where('id', '!=', $sourcePlatform->id)->get();

foreach ($targetPlatforms as $targetPlatform) {
if ($this->shouldSyncModel($modelClass, $targetPlatform)) {
$this->processSyncEvent($modelClass, $modelData, $eventType, $targetPlatform);
if ($this->shouldSyncModel($modelClass, $modelData, $targetPlatform, $this->platformRelationService->getSync())) {
$this->processSyncEvent($modelClass, $modelData, $eventType, $targetPlatform, $this->platformRelationService->getSync());
}
}
}

protected function shouldSyncModel($modelClass, Platform $targetPlatform)
protected function shouldSyncModel($modelClass, array $modelData, Platform $targetPlatform, $sync)
{
if (! $sync->use_platform_relations) {
$this->logDebug('Platform relations not used for this sync', ['model' => $modelClass, 'targetPlatform' => $targetPlatform->id]);

return true;
}

$modelsWithPlatformRelations = Config::get('sync.models_with_platform_relations', []);

if (! in_array($modelClass, $modelsWithPlatformRelations)) {
Expand All @@ -88,30 +94,70 @@ protected function shouldSyncModel($modelClass, Platform $targetPlatform)
return false;
}

// Additional logic can be added here if needed
return true;
$modelId = $this->getModelId($modelData);
$hasRelation = $this->platformRelationService->checkPlatformRelationForModel($modelClass, $modelId, $targetPlatform->id);

$this->logDebug('Checking platform relation', [
'model' => $modelClass,
'modelId' => $modelId,
'targetPlatform' => $targetPlatform->id,
'hasRelation' => $hasRelation,
]);

return $hasRelation;
}

protected function getModelId(array $modelData)
{
$identifierFields = Config::get('sync.local_identifier_fields', ['id', 'uuid', 'ulid']);

foreach ($identifierFields as $field) {
if (isset($modelData[$field])) {
return $modelData[$field];
}
}

throw new \Exception('Unable to determine model identifier');
}

protected function processSyncEvent($modelClass, array $modelData, string $eventType, Platform $targetPlatform)
protected function processSyncEvent($modelClass, array $modelData, string $eventType, Platform $targetPlatform, $sync)
{
$this->logDebug('Processing sync event', [
'modelClass' => $modelClass,
'eventType' => $eventType,
'targetPlatform' => $targetPlatform->id,
]);

switch ($eventType) {
case 'created':
$this->createOrUpdateModel($modelClass, $modelData, $targetPlatform);
break;
case 'updated':
$this->createOrUpdateModel($modelClass, $modelData, $targetPlatform);
break;
case 'deleted':
$this->deleteModel($modelClass, $modelData, $targetPlatform);
break;
default:
$this->logDebug('Unknown event type', ['eventType' => $eventType]);
if ($this->shouldSyncModel($modelClass, $modelData, $targetPlatform, $sync)) {
switch ($eventType) {
case 'created':
case 'updated':
$this->createOrUpdateModel($modelClass, $modelData, $targetPlatform);
break;
case 'deleted':
$this->deleteModel($modelClass, $modelData, $targetPlatform);
break;
default:
$this->logDebug('Unknown event type', ['eventType' => $eventType]);
}
} elseif ($sync->use_platform_relations && $eventType !== 'deleted') {
// If the model should not be synced but exists on the target, delete it
$this->deleteModelIfExists($modelClass, $modelData, $targetPlatform);
}
}

protected function deleteModelIfExists($modelClass, array $modelData, Platform $targetPlatform)
{
$modelId = $this->getModelId($modelData);
$existingModel = $modelClass::where($this->getModelId($modelData), $modelId)->first();

if ($existingModel) {
$this->logDebug('Deleting model that no longer has relation to target platform', [
'model' => $modelClass,
'modelId' => $modelId,
'targetPlatform' => $targetPlatform->id,
]);
$this->deleteModel($modelClass, $modelData, $targetPlatform);
}
}

Expand Down

0 comments on commit d7511b9

Please sign in to comment.