Skip to content

Commit

Permalink
Added BL for synchronization sha
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoLouwerse committed Nov 10, 2023
1 parent d88ab12 commit 2c809b4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
31 changes: 31 additions & 0 deletions api/migrations/Version20231110140316.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231110140316 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
23 changes: 22 additions & 1 deletion api/src/Entity/Synchronization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
*
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions api/src/Service/SynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class SynchronizationService
private Logger $logger;

private bool $asyncError = false;
private ?string $sha = null;

/**
* @param CallService $callService
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 2c809b4

Please sign in to comment.