Skip to content

Commit

Permalink
Merge branch 'feature/dashboardcard_installer' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Dec 12, 2022
2 parents 7c6fddc + ffc4397 commit 788e7ae
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 50 deletions.
167 changes: 161 additions & 6 deletions Service/CatalogiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class CatalogiService
private array $configuration;
private SymfonyStyle $io;

// Lets prevent unnesecery database calls
private ObjectEntity $catalogusEntity;
private ObjectEntity $componentEntity;
private ObjectEntity $organisationEntity;
private ObjectEntity $applicationEntity;


public function __construct(
EntityManagerInterface $entityManager,
SessionInterface $session,
Expand All @@ -46,6 +53,154 @@ public function __construct(
$this->synchronizationService = $synchronizationService;
}

/**
* Set symfony style in order to output to the console
*
* @param SymfonyStyle $io
* @return self
*/
public function setStyle(SymfonyStyle $io):self
{
$this->io = $io;

return $this;
}

/**
* Get and handle oll the objects of a catalogi
*
* @param ObjectEntity $catalogus
* @return void
*/
public function readCatalogi(ObjectEntity $catalogus):void{{
(isset($this->io)?$this->io->writeln(['<info>Reading catalogus'.$catalogus->getName().'</info>']):'');
var_dump('hello darkness my old friend');

// Lets grap ALL the objects for an external source
$objects = $this->callService->call(
$this->entityManager->find('App:Gateway', $catalogus->getValue('source')),
'/search',
'GET',
['query'=>['_limit'=>10000]]
)->getResponce()['results'];

// Now we can check if any objects where removed
if(!$source = $this->entityManager->getRepository('App:Gateway')->findBy(['location' =>$catalogus->getValue('location')])){
$source = New Source;
$source->setName($catalogus->getValue('name'));
$source->setDescription($catalogus->getValue('description'));
$source->setLocation($catalogus->getValue('location'));
}

$synchonizedObjects = [];
// Handle new objects
foreach($objects as $object){
// Lets make sure we have a reference
if (!isset($object['_self']['schema']['reference'])) {
continue;
}
$synchonization = $this->handleObject($object);
$synchonizedObjects[] = $synchonization->getExternalId();
$this->entityManager->persist($synchonization);
}
}

$this->entityManager->flush();

// Now we can check if any objects where removed
$synchonizations = $this->entityManager->getRepository('App:Synchronization')->findBy(['source' =>$source]);
foreach ($synchonizations as $synchonization){
if(!in_array($synchonization->getExternalId, $synchonizedObjects)){
$this->entityManager->remove($synchonization->getObject());
}
}

$this->entityManager->flush();
}

/**
* @param array $object
* @return void
*/
public function handleObject(array $object): ?Synchronization
{
// Lets make sure we have a reference, just in case this function gets ussed seperatly
if(!isset($object['_self']['schema']['reference'])) {
return null;
}

// Get The entities
$this->prebObjectEntities();

// Do our Magic
$reference = $object['_self']['schema']['reference'];

switch ($reference) {
case "https://opencatalogi.nl/catalogi.schema.json":
$entity = $this->catalogusEntity;
break;
case "https://opencatalogi.nl/organisation.schema.json":
$entity = $this->organisationEntity;
break;
case "https://opencatalogi.nl/component.schema.json":
$entity = $this->componentEntity;
break;
case "https://opencatalogi.nl/application.schema.json":
$entity = $this->applicationEntity;
break;
default:
// Unknown type, lets output something to IO
return null;
}

// Lets handle whatever we found
if($object['_self']['synchronisations'] and count($object['_self']['synchronisations']) != 0){
// We found something in a cataogi of witch that catalogus is not the source, so we need to synchorniste to that source set op that source if we dont have it yet etc etc
$baseSync = $object['_self']['synchronisations'][0];
$externalId = $baseSync['id'];

// Check for source
if(!$source = $this->entityManager->getRepository('App:Gateway')->findBy(['location' =>$baseSync['source']['location']])){
$source = new Source();
$source->setName($baseSync['source']['name']);
$source->setDescription($baseSync['source']['description']);
$source->setLocation($baseSync['source']['location']);
}
}
else{
// This catalogi is teh source so lets roll
$externalId = $object['id'];
}

// Lets se if we already have an synchronisation
if($synchonization = $this->entityManager->getRepository('App:Entity')->findBy(['externalId' =>$externalId])){
$synchonization = new Synchronization($source, $entity);
}

// Lets sync
return $this->synchronizationService->handleSync($synchonization, $object);
}

/**
* Makes sure that we have the object entities that we need
*
* @return void
*/
public function prebObjectEntities():void{
if(!isset($this->catalogusEntity)){
$this->catalogusEntity = $this->entityManager->getRepository('App:Entity')->findBy(['reference' =>'https://opencatalogi.nl/catalogi.schema.json']);
}
if(!isset($this->componentEntity)){
$this->componentEntity = $this->entityManager->getRepository('App:Entity')->findBy(['reference' =>'https://opencatalogi.nl/component.schema.json']);
}
if(!isset($this->organisationEntity)){
$this->organisationEntity = $this->entityManager->getRepository('App:Entity')->findBy(['reference' =>'https://opencatalogi.nl/organisation.schema.json']);
}
if(!isset($this->applicationEntity)){
$this->applicationEntity = $this->entityManager->getRepository('App:Entity')->findBy(['reference' =>'https://opencatalogi.nl/application.schema.json']);
}
}

/**
* Handles finding and adding unknown Catalogi. (and for now also does the same for their Components).
*
Expand Down Expand Up @@ -694,13 +849,13 @@ private function createSyncForComponent(array $data, array $addComponent): Synch
$synchronization->setLastSynced($now);
$synchronization->setSourcelastChanged(
isset($componentSync['sourceLastChanged']) ?
new DateTime($componentSync['sourceLastChanged']) :
(
// When getting the Components from other Catalogi we extend metadata.dateModified
new DateTime($componentSync['sourceLastChanged']) :
(
// When getting the Components from other Catalogi we extend metadata.dateModified
isset($componentMetaData['dateModified']) ?
new DateTime($componentMetaData['dateModified']) :
$now
)
new DateTime($componentMetaData['dateModified']) :
$now
)
);
// Note that we hash here with the x-commongateway-metadata fields (synchronizations, self and dateModified)
$synchronization->setHash(hash('sha384', serialize($addComponent)));
Expand Down
Loading

0 comments on commit 788e7ae

Please sign in to comment.