Skip to content

Commit

Permalink
Merge pull request #609 from CommonGateway/feature/KTB-3/notification…
Browse files Browse the repository at this point in the history
…-sync-improvements

Improvements
  • Loading branch information
bbrands02 authored Oct 7, 2024
2 parents 3505435 + ad29785 commit 3a0a32f
Showing 1 changed file with 61 additions and 24 deletions.
85 changes: 61 additions & 24 deletions src/Service/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace CommonGateway\CoreBundle\Service;

use Adbar\Dot;
use App\Entity\Gateway as Source;
use App\Entity\Synchronization;
use App\Service\SynchronizationService;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Response;

/**
Expand Down Expand Up @@ -49,11 +46,6 @@ class NotificationService
*/
private SynchronizationService $syncService;

/**
* @var CallService
*/
private CallService $callService;

/**
* @var GatewayResourceService
*/
Expand All @@ -72,13 +64,11 @@ public function __construct(
EntityManagerInterface $entityManager,
LoggerInterface $notificationLogger,
SynchronizationService $syncService,
CallService $callService,
GatewayResourceService $resourceService
) {
$this->entityManager = $entityManager;
$this->logger = $notificationLogger;
$this->syncService = $syncService;
$this->callService = $callService;
$this->resourceService = $resourceService;

}//end __construct()
Expand All @@ -94,35 +84,82 @@ public function __construct(
*/
public function notificationHandler(array $data, array $configuration): array
{
if ($data['method'] !== 'POST') {
return $data;
}
$this->logger->debug('NotificationService -> notificationHandler()');

// Check if we have a method and is POST or GET.
$allowedMethods = ($this->configuration['allowedMethods'] ?? ['POST']);
if (isset($data['method']) === false || in_array($data['method'], $allowedMethods) === false) {
$message = 'Notification request method is not one of '.implode(', ', $allowedMethods);
$response = json_encode(value: ['message' => $message]);
$this->logger->error($message);

return ['response' => new Response($response, 500, ['Content-type' => 'application/json'])];
}//end if

$this->data = $data;
$this->configuration = $configuration;

$this->logger->debug('NotificationService -> notificationHandler()');
$pluginName = ($this->configuration['pluginName'] ?? 'commongateway/corebundle');

$dot = new Dot($this->data);
$url = $dot->get($this->configuration['urlLocation']);

// Get the correct Entity.
$entity = $this->resourceService->getSchema($this->configuration['entity'], 'commongateway/corebundle');
if ($entity === null) {
$response = json_encode(['Message' => "Could not find an Entity with this reference: {$this->configuration['entity']}"]);
// Get or generate url to fetch object from.
if (isset($this->configuration['urlLocation']) === true) {
$url = $dot->get($this->configuration['urlLocation']);
} else if (isset($this->configuration['source']) === true && isset($this->configuration['endpoint']) === true && isset($this->configuration['sourceIdField']) === true) {
$source = $this->resourceService->getSource(reference: $this->configuration['source'], pluginName: $pluginName);

if ($source === null) {
$message = "Could not find an Source with this reference: {$this->configuration['source']}";
$response = json_encode(['message' => $message]);
return ['response' => new Response($response, 500, ['Content-type' => 'application/json'])];
}

$url = $source->getLocation().$this->configuration['endpoint'].'/'.$dot->get($this->configuration['sourceIdField']);
}//end if

// Throw error if url not found or generated.
if (isset($url) === false) {
$message = "Could not find find or generate the url to fetch the source object from";
$response = json_encode(['message' => $message]);
$this->logger->error($message);

return ['response' => new Response($response, 500, ['Content-type' => 'application/json'])];
}
}//end if

// Get schema the fetched object will belong to.
$schema = $this->resourceService->getSchema(reference: $this->configuration['schema'], pluginName: $pluginName);
if ($schema === null) {
$message = "Could not find an Schema with this reference: {$this->configuration['schema']}";
$response = json_encode(['message' => $message]);

return ['response' => new Response($response, 500, ['Content-type' => 'application/json'])];
}//end if

// Get mapping which is optional.
$mapping = null;
if (isset($this->configuration['mapping']) === true) {
$mapping = $this->resourceService->getMapping(reference: $this->configuration['mapping'], pluginName: $pluginName);
if ($mapping === null) {
$message = "Could not find an Mapping with this reference: {$this->configuration['mapping']}";
$response = json_encode(['message' => $message]);

return ['response' => new Response($response, 500, ['Content-type' => 'application/json'])];
}
}//end if

// Fetch and synchronise the source object we are notified about.
try {
$this->syncService->aquireObject($url, $entity);
} catch (\Exception $exception) {
$this->syncService->aquireObject(url: $url, schema: $schema, mapping: $mapping);
} catch (Exception $exception) {
$response = json_encode(['Message' => "Notification call before sync returned an Exception: {$exception->getMessage()}"]);
return ['response' => new Response($response, 500, ['Content-type' => 'application/json'])];
}//end try

// Flush anything managed by the EntityManager.
$this->entityManager->flush();

$response = ['Message' => 'Notification received, object synchronized'];
// Let the notifier know the notification has been handled Successfully.
$response = ['message' => 'Notification received, object synchronized'];
$data['response'] = new Response(json_encode($response), 200, ['Content-type' => 'application/json']);

return $data;
Expand Down

0 comments on commit 3a0a32f

Please sign in to comment.