Skip to content
Draft
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
83 changes: 42 additions & 41 deletions src/Service/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use MongoDB\BSON\UTCDateTime;
use CommonGateway\CoreBundle\Service\Cache\MongoDbClient as Client;
use CommonGateway\CoreBundle\Service\Cache\MongoDbClient;
use CommonGateway\CoreBundle\Service\Cache\CollectionInterface as Collection;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -48,14 +48,9 @@ class CacheService
{

/**
* @var Client
* @var MongoDbClient
*/
private Client $client;

/**
* @var Client
*/
private ClientInterface $objectsClient;
private MongoDbClient $client;

/**
* @var EntityManagerInterface
Expand Down Expand Up @@ -131,7 +126,7 @@ public function __construct(
$this->objectEntityService = $objectEntityService;
$this->session = $session;
if ($this->parameters->get('cache_url', false)) {
$this->client = new Client($this->parameters->get('cache_url'), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
$this->client = new MongoDbClient($this->parameters->get('cache_url'), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
}

$this->filesystem = new Filesystem();
Expand All @@ -143,10 +138,11 @@ public function __construct(
*
* @return void
*/
private function setObjectClient()
private function getObjectClient(?Database $database = null): ?ClientInterface
{
$organization = null;
$user = $this->objectEntityService->findCurrentUser();

if ($user !== null && $user->getOrganization() !== null) {
$organization = $this->entityManager->getRepository(Organization::class)->find($user->getOrganization());
}
Expand All @@ -160,15 +156,21 @@ private function setObjectClient()
$this->logger->warning('Cannot determine tennant from application: '.$e->getMessage());
}

if ($organization !== null && $organization->getDatabase() !== null && $organization->getDatabase()->getType() === 'mongodb') {
$this->objectsClient = new Client($organization->getDatabase()->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
if ($database === null && $organization !== null && $organization->getDatabase !== null) {
$database = $organization->getDatabase();
}

if ($database->getType() === 'mongodb') {
return new MongoDbClient($organization->getDatabase()->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
}

if ($organization !== null && $organization->getDatabase() !== null && $organization->getDatabase()->getType() === 'elasticsearch') {
$this->objectsClient = new ElasticSearchClient($organization->getDatabase()->getUri(), $organization->getDatabase()->getAuth());
if ($database->getType() === 'elasticsearch') {
return new ElasticSearchClient($organization->getDatabase()->getUri(), $organization->getDatabase()->getAuth());
}

}//end setObjectClient()
return null;

}//end getObjectClient()

/**
* Set symfony style in order to output to the console.
Expand Down Expand Up @@ -203,7 +205,7 @@ public function cleanup(): void
isset($this->style) === true && $this->style->section('Cleaning Object\'s');
$objectDatabases = $this->entityManager->getRepository(Database::class)->findAll();
foreach ($objectDatabases as $database) {
$objectsClient = new Client($database->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
$objectsClient = new MongoDbClient($database->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
$collection = $objectsClient->objects->json;

$filter = [];
Expand Down Expand Up @@ -362,7 +364,7 @@ public function warmup(array $config = [], ?string $bundleToCache = null): int
$objectDatabases = $this->entityManager->getRepository(Database::class)->findAll();
if (isset($config['objects']) === false || $config['objects'] !== true) {
foreach ($objectDatabases as $database) {
$objectsClient = new Client($database->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
$objectsClient = new MongoDbClient($database->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);

$objectsClient->objects->json->createIndex(['$**' => 'text']);

Expand Down Expand Up @@ -449,12 +451,11 @@ public function cacheObject(ObjectEntity $objectEntity): ObjectEntity
return $objectEntity;
}

$this->setObjectClient();
if (isset($this->objectsClient) === true) {
$collection = $this->objectsClient->objects->json;
$objectsClient = $this->getObjectClient();
if ($objectsClient !== null) {
$collection = $objectsClient->objects->json;
} else if ($objectEntity->getOrganization() !== null && $objectEntity->getOrganization()->getDatabase() !== null) {
$database = $objectEntity->getOrganization()->getDatabase();
$objectsClient = new Client($database->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
$objectsClient = $this->getObjectClient($objectEntity->getOrganization()->getDatabase());
$collection = $objectsClient->objects->json;
} else if (isset($this->client) === true) {
$collection = $this->client->objects->json;
Expand Down Expand Up @@ -551,11 +552,11 @@ private function getObjectUser(ObjectEntity $objectEntity): ?User
*/
public function removeObject(ObjectEntity $objectEntity, bool $softDelete = false): void
{
$this->setObjectClient();
if (isset($this->objectsClient) === true) {
$collection = $this->objectsClient->objects->json;
$objectsClient = $this->getObjectClient();
if ($objectsClient !== null) {
$collection = $objectsClient->objects->json;
} else if ($objectEntity->getOrganization() !== null && $objectEntity->getOrganization()->getDatabase() !== null) {
$objectsClient = new Client($objectEntity->getOrganization()->getDatabase()->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
$objectsClient = $this->getObjectClient($objectEntity->getOrganization()->getDatabase());
$collection = $objectsClient->objects->json;
} else if (isset($this->client) === true) {
$collection = $this->client->objects->json;
Expand Down Expand Up @@ -590,13 +591,14 @@ public function removeObject(ObjectEntity $objectEntity, bool $softDelete = fals
*/
public function getObject(string $identification, string $schema = null): ?array
{
$this->setObjectClient();
if (isset($this->objectsClient) === true) {
$collection = $this->objectsClient->objects->json;

$objectsClient = $this->getObjectClient();
if ($objectsClient !== null) {
$collection = $objectsClient->objects->json;
} else {
$objectEntity = $this->entityManager->getRepository(ObjectEntity::class)->findOneBy(['id' => $identification]);
if ($objectEntity !== null && $objectEntity->getOrganization() !== null && $objectEntity->getOrganization()->getDatabase() !== null) {
$objectsClient = new Client($objectEntity->getOrganization()->getDatabase()->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService);
$objectsClient = $this->getObjectClient($objectEntity->getOrganization()->getDatabase());
$collection = $objectsClient->objects->json;
} else if (isset($this->client) === true) {
$collection = $this->client->objects->json;
Expand Down Expand Up @@ -1086,10 +1088,9 @@ public function retrieveObjectsFromCache(array $filter, ?array $options = null,
{

$this->session->set('mongoDBFilter', $filter);

$this->setObjectClient();
if (isset($this->objectsClient) === true) {
$collection = $this->objectsClient->objects->json;
$objectsClient = $this->getObjectClient();
if ($objectsClient !== null) {
$collection = $objectsClient->objects->json;
} else if (isset($this->client) === true) {
$collection = $this->client->objects->json;
} else {
Expand Down Expand Up @@ -1161,10 +1162,9 @@ public function countObjectsInCache(array $filter): int
{

$this->session->set('mongoDBFilter', $filter);

$this->setObjectClient();
if (isset($this->objectsClient) === true) {
$collection = $this->objectsClient->objects->json;
$objectsClient = $this->getObjectClient();
if ($objectsClient !== null) {
$collection = $objectsClient->objects->json;
} else if (isset($this->client) === true) {
$collection = $this->client->objects->json;
} else {
Expand Down Expand Up @@ -1240,9 +1240,10 @@ public function aggregateQueries(array $filter, array $entities)
$this->handleSearch($filter, $completeFilter, null);

$result = [];
$this->setObjectClient();
if (isset($this->objectsClient) === true) {
$collection = $this->objectsClient->objects->json;

$objectsClient = $this->getObjectClient();
if ($objectsClient !== null) {
$collection = $objectsClient->objects->json;
} else if (isset($this->client) === true) {
$collection = $this->client->objects->json;
} else {
Expand Down