Skip to content

Commit

Permalink
Merge pull request #605 from CommonGateway/fix/synchronizeTemp
Browse files Browse the repository at this point in the history
Fix the synchronizeTemp function + updated docblock
  • Loading branch information
WilcoLouwerse authored Sep 20, 2024
2 parents 20d5886 + d9a6834 commit c12216a
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/Service/SynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Entity\ObjectEntity;
use App\Entity\Synchronization;
use App\Service\SynchronizationService as OldSynchronizationService;
use CommonGateway\CoreBundle\Service\CallService;
use Doctrine\ORM\EntityManagerInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\RequestException;
Expand Down Expand Up @@ -40,6 +40,16 @@ class SynchronizationService
*/
private LoggerInterface $logger;

/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $entityManager;

/**
* @var CacheService
*/
private CacheService $cacheService;

/**
* Old one from the gateway.
*
Expand All @@ -60,15 +70,21 @@ class SynchronizationService
* @param LoggerInterface $callLogger The Logger Interface.
* @param OldSynchronizationService $oldSyncService Old one from the gateway.
* @param CallService $callService The callService.
* @param EntityManagerInterface $entityManager EntityManagerInterface.
* @param CacheService $cacheService CacheService.
*/
public function __construct(
LoggerInterface $callLogger,
OldSynchronizationService $oldSyncService,
CallService $callService
CallService $callService,
EntityManagerInterface $entityManager,
CacheService $cacheService
) {
$this->logger = $callLogger;
$this->oldSyncService = $oldSyncService;
$this->callService = $callService;
$this->entityManager = $entityManager;
$this->cacheService = $cacheService;

}//end __construct()

Expand All @@ -88,24 +104,25 @@ public function setStyle(SymfonyStyle $style): self
}//end setStyle()

/**
* Temporary function as replacement of the $this->oldSyncService->synchronize() function.
* Because currently synchronize function can only pull from a source and not push to a source.
* This function was created because currently $commonGateway->synchronizationService->synchronize() function
* can only pull from a source and not push to a source.
* This function can be used as 'temporary' replacement of the synchronize function.
*
* @todo: Temp way of doing this without updating the oldSyncService->synchronize() function...
* NOTE: Before calling this function a Synchronization object must exist or be created, please use the
* $commonGateway->synchronizationService->findSyncBySource() or findSyncByObject() function for this.
*
* @param Synchronization|null $synchronization The synchronization we are going to synchronize.
* @param array $objectArray The object data we are going to synchronize.
* @param ObjectEntity $objectEntity The objectEntity which data we are going to synchronize.
* @param Schema $schema The schema the object we are going to send belongs to.
* @param string $location The path/endpoint we send the request to.
* @param string|null $idLocation The location of the id in the response body.
* @param string|null $method The request method PUT or POST.
* @param Synchronization $synchronization The synchronization we are going to synchronize.
* @param array $objectArray The object data we are going to synchronize.
* @param ObjectEntity $objectEntity The objectEntity which data we are going to synchronize.
* @param string $location The path/endpoint we send the request to.
* @param string|null $idLocation The location of the id in the response body.
* @param string|null $method The request method PUT or POST.
*
* @return array The response body of the outgoing call, or an empty array on error.
*
* @throws Exception
*/
public function synchronizeTemp(?Synchronization &$synchronization = null, array $objectArray, ObjectEntity $objectEntity, Schema $schema, string $location, ?string $idLocation = null, ?string $method = 'POST'): array
public function synchronizeTemp(Synchronization &$synchronization, array $objectArray, ObjectEntity $objectEntity, string $location, ?string $idLocation = null, ?string $method = 'POST'): array
{
$objectString = $this->oldSyncService->getObjectString($objectArray);

Expand Down Expand Up @@ -145,11 +162,6 @@ public function synchronizeTemp(?Synchronization &$synchronization = null, array

$body = $this->callService->decodeResponse($synchronization->getSource(), $result);

if (isset($synchronization) === false) {
$synchronization = new Synchronization();
$synchronization->setEntity($schema);
}

$bodyDot = new Dot($body);

if ($idLocation !== null) {
Expand All @@ -175,6 +187,10 @@ public function synchronizeTemp(?Synchronization &$synchronization = null, array
$synchronization->setLastChecked($now);
$synchronization->setHash(hash('sha384', serialize($bodyDot->jsonSerialize())));

$this->entityManager->persist($synchronization);
$this->entityManager->flush();
$this->cacheService->cacheObject($synchronization->getObject());

$this->logger->info('Synchronize '.$method.' succesfull with response body '.json_encode($body));

return $body;
Expand Down

0 comments on commit c12216a

Please sign in to comment.