From c252b6ecc0dbc81d95a61641a0d52411ce327123 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Thu, 6 Jun 2024 11:57:05 +0200 Subject: [PATCH 1/2] Use lokal variable instead of global --- src/Service/CacheService.php | 81 ++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/Service/CacheService.php b/src/Service/CacheService.php index bb347702..ffc2d354 100644 --- a/src/Service/CacheService.php +++ b/src/Service/CacheService.php @@ -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; @@ -48,14 +48,9 @@ class CacheService { /** - * @var Client + * @var MongoDbClient */ - private Client $client; - - /** - * @var Client - */ - private ClientInterface $objectsClient; + private MongoDbClient $client; /** * @var EntityManagerInterface @@ -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(); @@ -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()); } @@ -160,14 +156,20 @@ 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 ($organization !== null && $organization->getDatabase() !== null && $organization->getDatabase()->getType() === 'elasticsearch') { - $this->objectsClient = new ElasticSearchClient($organization->getDatabase()->getUri(), $organization->getDatabase()->getAuth()); + if ($database->getType() === 'mongodb') { + return new MongoDbClient($organization->getDatabase()->getUri(), entityManager: $this->entityManager, objectEntityService: $this->objectEntityService); } + if ($database->getType() === 'elasticsearch') { + return new ElasticSearchClient($organization->getDatabase()->getUri(), $organization->getDatabase()->getAuth()); + } + + return null; + }//end setObjectClient() /** @@ -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 = []; @@ -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']); @@ -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; @@ -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; @@ -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; @@ -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 { @@ -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 { @@ -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 { From 52ef1e48b30561435f2936055552b705cc77c229 Mon Sep 17 00:00:00 2001 From: GitHub Actions <> Date: Thu, 6 Jun 2024 09:57:47 +0000 Subject: [PATCH 2/2] Update src from PHP Codesniffer --- src/Service/CacheService.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Service/CacheService.php b/src/Service/CacheService.php index ffc2d354..a11cbbbd 100644 --- a/src/Service/CacheService.php +++ b/src/Service/CacheService.php @@ -170,7 +170,7 @@ private function getObjectClient(?Database $database = null): ?ClientInterface return null; - }//end setObjectClient() + }//end getObjectClient() /** * Set symfony style in order to output to the console. @@ -453,7 +453,7 @@ public function cacheObject(ObjectEntity $objectEntity): ObjectEntity $objectsClient = $this->getObjectClient(); if ($objectsClient !== null) { - $collection = $objectsClient->objects->json; + $collection = $objectsClient->objects->json; } else if ($objectEntity->getOrganization() !== null && $objectEntity->getOrganization()->getDatabase() !== null) { $objectsClient = $this->getObjectClient($objectEntity->getOrganization()->getDatabase()); $collection = $objectsClient->objects->json; @@ -554,7 +554,7 @@ public function removeObject(ObjectEntity $objectEntity, bool $softDelete = fals { $objectsClient = $this->getObjectClient(); if ($objectsClient !== null) { - $collection = $objectsClient->objects->json; + $collection = $objectsClient->objects->json; } else if ($objectEntity->getOrganization() !== null && $objectEntity->getOrganization()->getDatabase() !== null) { $objectsClient = $this->getObjectClient($objectEntity->getOrganization()->getDatabase()); $collection = $objectsClient->objects->json; @@ -594,7 +594,7 @@ public function getObject(string $identification, string $schema = null): ?array $objectsClient = $this->getObjectClient(); if ($objectsClient !== null) { - $collection = $objectsClient->objects->json; + $collection = $objectsClient->objects->json; } else { $objectEntity = $this->entityManager->getRepository(ObjectEntity::class)->findOneBy(['id' => $identification]); if ($objectEntity !== null && $objectEntity->getOrganization() !== null && $objectEntity->getOrganization()->getDatabase() !== null) { @@ -1090,7 +1090,7 @@ public function retrieveObjectsFromCache(array $filter, ?array $options = null, $this->session->set('mongoDBFilter', $filter); $objectsClient = $this->getObjectClient(); if ($objectsClient !== null) { - $collection = $objectsClient->objects->json; + $collection = $objectsClient->objects->json; } else if (isset($this->client) === true) { $collection = $this->client->objects->json; } else { @@ -1164,7 +1164,7 @@ public function countObjectsInCache(array $filter): int $this->session->set('mongoDBFilter', $filter); $objectsClient = $this->getObjectClient(); if ($objectsClient !== null) { - $collection = $objectsClient->objects->json; + $collection = $objectsClient->objects->json; } else if (isset($this->client) === true) { $collection = $this->client->objects->json; } else { @@ -1243,7 +1243,7 @@ public function aggregateQueries(array $filter, array $entities) $objectsClient = $this->getObjectClient(); if ($objectsClient !== null) { - $collection = $objectsClient->objects->json; + $collection = $objectsClient->objects->json; } else if (isset($this->client) === true) { $collection = $this->client->objects->json; } else {