Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BL for synchronization sha #1582

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading