Skip to content

Commit

Permalink
Do synchronysations when the mapping is updated or the object removed
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Jan 8, 2025
1 parent 8944343 commit 0398198
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions lib/Service/SynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,20 @@ private function fetchExtraDataForObject(Synchronization $synchronization, array
* @param Synchronization|null $synchronization
* @param array $object
* @param bool|null $isTest False by default, currently added for synchronization-test endpoint
* @param bool|null $force True if the contract wil be forced to be synced
*
* @return SynchronizationContract|Exception|array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws LoaderError
* @throws SyntaxError
*/
public function synchronizeContract(SynchronizationContract $synchronizationContract, Synchronization $synchronization = null, array $object = [], ?bool $isTest = false): SynchronizationContract|Exception|array
public function synchronizeContract(
SynchronizationContract $synchronizationContract,
Synchronization $synchronization = null,
array $object = [],
?bool $isTest = false,
?bool $force = false): SynchronizationContract|Exception|array
{
$sourceConfig = $this->callService->applyConfigDot($synchronization->getSourceConfig());

Expand All @@ -325,13 +331,32 @@ public function synchronizeContract(SynchronizationContract $synchronizationCont
unset($object['d']['vti_x005f_dirlateststamp']);
}

// Let grap the source target mapping
$sourceTargetMapping = null;
if (empty($synchronization->getSourceTargetMapping()) === false) {
try {
$sourceTargetMapping = $this->mappingMapper->find(id: $synchronization->getSourceTargetMapping());
} catch (DoesNotExistException $exception) {
return new Exception($exception->getMessage());
}
}

// Let create a source hash for the object
$originHash = md5(serialize($object));

// Let's prevent pointless updates @todo account for omnidirectional sync, unless the config has been updated since last check then we do want to rebuild and check if the tagert object has changed
if ($originHash === $synchronizationContract->getOriginHash() && $synchronization->getUpdated() < $synchronizationContract->getSourceLastChecked()) {
// The object has not changed and the config has not been updated since last check
if (
$originHash === $synchronizationContract->getOriginHash() &&
$synchronization->getUpdated() < $synchronizationContract->getSourceLastChecked() &&
$force === false &&
$synchronizationContract->getTargetId() !== null &&
$synchronizationContract->getTargetHash() !== null &&
($sourceTargetMapping === null ||
($sourceTargetMapping->getUpdated() < $synchronizationContract->getSourceLastChecked() )
)
) {
// The object has not changed, the config has not been updated since last check,
// and either there is no mapping or the mapping hasn't been updated and force sync is not enabled
return $synchronizationContract;
}

Expand All @@ -342,26 +367,19 @@ public function synchronizeContract(SynchronizationContract $synchronizationCont

// Check if object adheres to conditions.
// Take note, JsonLogic::apply() returns a range of return types, so checking it with '=== false' or '!== true' does not work properly.
if ($synchronization->getConditions() !== [] && !JsonLogic::apply($synchronization->getConditions(), $object)) {
// Skip if conditions are set and object doesn't match conditions, unless force is enabled
if (
$synchronization->getConditions() !== [] &&
!JsonLogic::apply($synchronization->getConditions(), $object) &&
$force === false) {
return $synchronizationContract;
}

// If no source target mapping is defined, use original object
if (empty($synchronization->getSourceTargetMapping()) === true) {
$targetObject = $object;
// Execute mapping if found
if ($sourceTargetMapping) {
$targetObject = $this->mappingService->executeMapping(mapping: $sourceTargetMapping, input: $object);
} else {
try {
$sourceTargetMapping = $this->mappingMapper->find(id: $synchronization->getSourceTargetMapping());
} catch (DoesNotExistException $exception) {
return new Exception($exception->getMessage());
}

// Execute mapping if found
if ($sourceTargetMapping) {
$targetObject = $this->mappingService->executeMapping(mapping: $sourceTargetMapping, input: $object);
} else {
$targetObject = $object;
}
$targetObject = $object;
}

// set the target hash
Expand Down Expand Up @@ -558,8 +576,9 @@ public function getAllObjectsFromApi(Synchronization $synchronization, ?bool $is
do {
$config = $this->getNextPage(config: $config, sourceConfig: $sourceConfig, currentPage: $currentPage);
$response = $this->callService->call(source: $source, endpoint: $endpoint, method: 'GET', config: $config)->getResponse();
$hash = md5($response['body']);


// Lets make sure that we are not fetching the same page again
$hash = md5($response['body']);
if($hash === $lastHash) {
break;
}
Expand Down

0 comments on commit 0398198

Please sign in to comment.