Skip to content
Open
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
23 changes: 21 additions & 2 deletions Service/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use MongoDB\Client;
use Monolog\Logger;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Cache\Adapter\AdapterInterface as CacheInterface;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -38,10 +39,16 @@ class CacheService
private ParameterBagInterface $parameters;
private SerializerInterface $serializer;

// Add monolog logger bundle for the generic logging interface
private Logger $logger;

/**
* @param AuthenticationService $authenticationService
* Setting up the base class with required services
*
* @param EntityManagerInterface $entityManager
* @param FileService $fileService
* @param CacheInterface $cache
* @param ParameterBagInterface $parameters
* @param SerializerInterface $serializer
*/
public function __construct(
EntityManagerInterface $entityManager,
Expand All @@ -56,6 +63,7 @@ public function __construct(
if ($this->parameters->get('cache_url', false)) {
$this->client = new Client($this->parameters->get('cache_url'));
}
$this->logger = New Logger('cache');
}

/**
Expand Down Expand Up @@ -239,8 +247,12 @@ public function cacheObject(ObjectEntity $objectEntity): ObjectEntity
['upsert'=>true]
)) {
(isset($this->io) ? $this->io->writeln('Updated object '.$objectEntity->getId()->toString().' of type '.$objectEntity->getEntity()->getName().' to cache') : '');

$this->logger->debug("updated object to cache");
} else {
(isset($this->io) ? $this->io->writeln('Wrote object '.$objectEntity->getId()->toString().' of type '.$objectEntity->getEntity()->getName().' to cache') : '');

$this->logger->debug("Added object to cache");
}

return $objectEntity;
Expand All @@ -264,6 +276,8 @@ public function removeObject(ObjectEntity $object): void
$collection = $this->client->objects->json;

$collection->findOneAndDelete(['_id'=>$id]);

$this->logger->debug("Removed object from cache");
}

/**
Expand All @@ -284,11 +298,13 @@ public function getObject(string $id)

// Check if object is in the cache ????
if ($object = $collection->findOne(['_id'=>$id])) {
$this->logger->debug("Retrieved cached object from cache");
return $object;
}

// Fall back tot the entity manager
if ($object = $this->entityManager->getRepository('App:ObjectEntity')->findOneBy(['id'=>$id])) {
$this->logger->debug("Retrieved uncached object from cache");
return $this->cacheObject($object)->toArray(['embedded' => true]);
}

Expand Down Expand Up @@ -365,6 +381,9 @@ public function searchObjects(string $search = null, array $filter = [], array $
$results = $collection->find($filter, ['limit' => $limit, 'skip' => $start, 'sort' => $order])->toArray();
$total = $collection->count($filter);


$this->logger->debug("Searched cache for objects");

// Make sure to add the pagination properties in response
return $this->handleResultPagination($completeFilter, $results, $total);
}
Expand Down
116 changes: 97 additions & 19 deletions Service/InstallationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Monolog\Logger;

class InstallationService
{
Expand All @@ -19,6 +20,16 @@ class InstallationService
private SymfonyStyle $io;
private $container;

// Add monolog logger bundle for the generic logging interface
private Logger $logger;

/**
* Setting up the base class with required services
*
* @param ComposerService $composerService
* @param EntityManagerInterface $em
* @param Kernel $kernel
*/
public function __construct(
ComposerService $composerService,
EntityManagerInterface $em,
Expand All @@ -28,6 +39,8 @@ public function __construct(
$this->em = $em;
$this->container = $kernel->getContainer();
$this->collection = null;
$this->logger = New Logger('installation');

}

/**
Expand Down Expand Up @@ -59,7 +72,10 @@ public function composerupdate(): int
]);
}

$this->logger->debug("Running plugin installer");

foreach ($plugins as $plugin) {

$this->install($plugin['name']);
}

Expand All @@ -84,6 +100,8 @@ public function install(string $bundle, bool $noSchema = false): int
]);
}

$this->logger->debug('Trying to install: '.$bundle);

$packages = $this->composerService->getAll();

$found = array_filter($packages, function ($v, $k) use ($bundle) {
Expand Down Expand Up @@ -123,13 +141,16 @@ public function install(string $bundle, bool $noSchema = false): int
// We want each plugin to also be a collection (if it contains schema's that is)
if (count($schemas) > 0) {
if (!$this->collection = $this->em->getRepository('App:CollectionEntity')->findOneBy(['plugin' => $package['name']])) {

$this->logger->debug('Created a collection for plugin '.$bundle);
$this->io->writeln(['Created a collection for this plugin', '']);
$this->collection = new CollectionEntity();
$this->collection->setName($package['name']);
$this->collection->setPlugin($package['name']);
isset($package['description']) && $this->collection->setDescription($package['description']);
} else {
$this->io->writeln(['Found a collection for this plugin', '']);
$this->logger->debug('Found a collection for plugin '.$bundle);
}
}

Expand All @@ -145,6 +166,7 @@ public function install(string $bundle, bool $noSchema = false): int
//$progressBar->finish();
} else {
$this->io->writeln('No schema folder found');
$this->logger->debug('No schema folder found for plugin '.$bundle);
}

// Handling the data
Expand All @@ -163,6 +185,7 @@ public function install(string $bundle, bool $noSchema = false): int

// We need to clear the finder
} else {
$this->logger->debug('No data folder found for plugin '.$bundle);
$this->io->writeln('No data folder found');
}

Expand All @@ -179,10 +202,12 @@ public function install(string $bundle, bool $noSchema = false): int
$this->handleInstaller($installer);
}
} else {
$this->logger->debug('No Installation folder found for plugin '.$bundle);
$this->io->writeln('No Installation folder found');
}

$this->io->success('All Done');
$this->logger->debug('All Done installing plugin '.$bundle);

return Command::SUCCESS;
}
Expand Down Expand Up @@ -295,31 +320,15 @@ public function handleData($file)

if (array_key_exists('_id', $object) && $objectEntity = $this->em->getRepository('App:ObjectEntity')->findOneBy(['id' => $object['_id']])) {
$this->io->writeln(['', 'Object '.$object['_id'].' already exists, so updating']);
} elseif (array_key_exists('_id', $object)) {
$this->io->writeln('Set id to '.$object['_id']);

// Nice doctrine setId shizzle
$objectEntity = new ObjectEntity();
$this->em->persist($objectEntity);
$objectEntity->setId($object['_id']);
$this->em->persist($objectEntity);
$this->em->flush();
$this->em->refresh($objectEntity);

$objectEntity = $this->em->getRepository('App:ObjectEntity')->findOneBy(['id' => $object['_id']]);

$objectEntity->setEntity($entity);

$this->io->writeln('Creating new object with existing id '.$objectEntity->getId());
} else {
$objectEntity = new ObjectEntity($entity);
$this->io->writeln(['', 'Creating new object']);
}

$this->io->writeln('Writing data to the object');
$objectEntity->hydrate($object);
$this->em->persist($objectEntity);
$this->em->flush();

$this->saveOnFixedId($objectEntity);

$this->io->writeln(['Object saved as '.$objectEntity->getId(), '']);
}
}
Expand Down Expand Up @@ -349,4 +358,73 @@ public function handleInstaller($file)

return $installationService->install();
}

/**
* Handles forced id's on object entities
*
* @param ObjectEntity $objectEntity
* @return ObjectEntity
*/
private function saveOnFixedId(ObjectEntity $objectEntity): ObjectEntity{
// This savetey dosn't make sense but we need it
if(!$objectEntity->getEntity()){

$this->logger->error('Object can\'t be persisted due to missing schema');
$this->io->writeln(['', 'Object can\'t be persisted due to missing schema']);
return $objectEntity;
}

// Save the values
$values = $objectEntity->getObjectValues()->toArray();
$objectEntity->clearAllValues();

// We have an object entity with a fixed id that isn't in the database, so we need to act
if($objectEntity->getId() && !$this->em->contains($objectEntity)){

$this->io->writeln(['', 'Creating new object ('.$objectEntity->getEntity()->getName().') on a fixed id ('.$objectEntity->getId().')']);

// Sve the id
$id = $objectEntity->getId();
// Create the entity
$this->em->persist($objectEntity);
$this->em->flush();
$this->em->refresh($objectEntity);
// Reset the id
$objectEntity->setId($id);
$this->em->persist($objectEntity);
$this->em->flush();
$objectEntity = $this->em->getRepository('App:ObjectEntity')->findOneBy(['id' => $id]);
}
else{
$this->io->writeln(['', 'Creating new object ('.$objectEntity->getEntity()->getName().') on a generated id']);
}

// Loop trough the values
foreach ($values as $objectValue){
$objectEntity->addObjectValue($objectValue);

// If the value itsself is an object it might also contain fixed id's
foreach ($objectValue->getObjects() as $subobject){

$this->io->writeln(['', 'Found sub object ('.$subobject->getEntity()->getName().')']);

if($subobject->getEntity()){
$this->io->writeln(['subobject has schema so can be saved']);
$subobject = $this->saveOnFixedId($subobject);
}
else{
$this->io->warning(['subobject has NO schema so can\'t be saved']);
$objectValue->removeObject($subobject);
}
}
}

$this->em->persist($objectEntity);
$this->em->flush();

return $objectEntity;
}



}
Loading