From 65b997edaf091603ca53d74ad90aad648470fa52 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 20 Sep 2024 14:40:58 +0200 Subject: [PATCH 1/3] Fix the synchronizeTemp function + updated docblock --- src/Service/SynchronizationService.php | 42 ++++++++++++++++++-------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 3f4cf92b..9561fd3a 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -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; @@ -39,6 +39,16 @@ class SynchronizationService * @var LoggerInterface $logger. */ private LoggerInterface $logger; + + /** + * @var EntityManagerInterface + */ + private EntityManagerInterface $entityManager; + + /** + * @var CacheService + */ + private CacheService $cacheService; /** * Old one from the gateway. @@ -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() @@ -88,15 +104,16 @@ 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 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 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. @@ -105,7 +122,7 @@ public function setStyle(SymfonyStyle $style): self * * @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); @@ -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) { @@ -174,6 +186,10 @@ public function synchronizeTemp(?Synchronization &$synchronization = null, array $synchronization->setSourceLastChanged($now); $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)); From 6870c7c77cf02cc091439810ee601e1bd4e2348b Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Fri, 20 Sep 2024 12:41:38 +0000 Subject: [PATCH 2/3] Update src from PHP Codesniffer --- src/Service/SynchronizationService.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 9561fd3a..53388b2e 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -39,12 +39,12 @@ class SynchronizationService * @var LoggerInterface $logger. */ private LoggerInterface $logger; - + /** * @var EntityManagerInterface */ private EntityManagerInterface $entityManager; - + /** * @var CacheService */ @@ -83,8 +83,8 @@ public function __construct( $this->logger = $callLogger; $this->oldSyncService = $oldSyncService; $this->callService = $callService; - $this->entityManager = $entityManager; - $this->cacheService = $cacheService; + $this->entityManager = $entityManager; + $this->cacheService = $cacheService; }//end __construct() @@ -111,12 +111,12 @@ public function setStyle(SymfonyStyle $style): self * 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 $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. + * @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. * @@ -186,7 +186,7 @@ public function synchronizeTemp(Synchronization $synchronization, array $objectA $synchronization->setSourceLastChanged($now); $synchronization->setLastChecked($now); $synchronization->setHash(hash('sha384', serialize($bodyDot->jsonSerialize()))); - + $this->entityManager->persist($synchronization); $this->entityManager->flush(); $this->cacheService->cacheObject($synchronization->getObject()); From c3dd2ac69e8a3dd7d84e67cb8053fcc7040f5fac Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 20 Sep 2024 14:54:43 +0200 Subject: [PATCH 3/3] & --- src/Service/SynchronizationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/SynchronizationService.php b/src/Service/SynchronizationService.php index 9561fd3a..5c8c52ec 100644 --- a/src/Service/SynchronizationService.php +++ b/src/Service/SynchronizationService.php @@ -122,7 +122,7 @@ public function setStyle(SymfonyStyle $style): self * * @throws Exception */ - public function synchronizeTemp(Synchronization $synchronization, array $objectArray, ObjectEntity $objectEntity, 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);