diff --git a/api/migrations/Version20231110140316.php b/api/migrations/Version20231110140316.php new file mode 100644 index 000000000..72fb31735 --- /dev/null +++ b/api/migrations/Version20231110140316.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE synchronization ADD sha VARCHAR(255) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE synchronization DROP sha'); + } +} diff --git a/api/src/Entity/Synchronization.php b/api/src/Entity/Synchronization.php index 4bd2025e7..678594050 100644 --- a/api/src/Entity/Synchronization.php +++ b/api/src/Entity/Synchronization.php @@ -154,7 +154,16 @@ class Synchronization private ?string $hash = ''; /** - * @var bool Whether or not the synchronization is blocked + * @var ?string The sha(256) used to check if a Sync should be triggered cause the object has changed + * + * @Groups({"read","write"}) + * + * @ORM\Column(type="string", nullable=true) + */ + private ?string $sha = null; + + /** + * @var bool Whether the synchronization is blocked * * @Groups({"read", "write"}) * @@ -361,6 +370,18 @@ public function setHash(?string $hash): self return $this; } + public function getSha(): ?string + { + return $this->sha; + } + + public function setSha(?string $sha): self + { + $this->sha = $sha; + + return $this; + } + public function getSourceLastChanged(): ?\DateTimeInterface { return $this->sourceLastChanged; diff --git a/api/src/Service/SynchronizationService.php b/api/src/Service/SynchronizationService.php index cd48120b9..b70a62ee0 100644 --- a/api/src/Service/SynchronizationService.php +++ b/api/src/Service/SynchronizationService.php @@ -66,6 +66,7 @@ class SynchronizationService private Logger $logger; private bool $asyncError = false; + private ?string $sha = null; /** * @param CallService $callService @@ -917,6 +918,29 @@ public function handleSync(Synchronization $synchronization, array $sourceObject return $synchronization; } + /** + * This function checks if the sha of $synchronization matches the given $sha. + * When $synchronization->getSha() doesn't match with the given $sha, the given $sha will be stored in the SynchronizationService. + * Always call the ->synchronize() function after this, because only then the stored $sha will be used to update $synchronization->setSha(). + * + * @param Synchronization $synchronization The Synchronization to check the sha of. + * @param string $sha The sha to check / compare. + * + * @return bool Returns True if sha matches, and false if it does not match. + */ + public function doesShaMatch(Synchronization $synchronization, string $sha): bool + { + $this->sha = null; + + if ($synchronization->getSha() === $sha) { + return true; + } + + $this->sha = $sha; + + return false; + } + /** * Executes the synchronization between source and gateway. * @@ -988,6 +1012,12 @@ public function synchronize(Synchronization $synchronization, array $sourceObjec $sourceObject = $this->mappingService->mapping($synchronization->getMapping(), $sourceObject); } $synchronization->getObject()->hydrate($sourceObject, $unsafe); + + if ($this->sha !== null) { + $synchronization->setSha($this->sha); + $this->sha = null; + } + $this->entityManager->persist($synchronization->getObject()); $this->entityManager->persist($synchronization);