diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 374df70..5864d46 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -20,8 +20,6 @@ jobs: strategy: matrix: include: - - { php-version: "7.4", dependencies: "lowest", pimcore_version: "", phpstan_args: "", experimental: false } - - { php-version: "8.0", dependencies: "highest", pimcore_version: "", phpstan_args: "", experimental: false } - { php-version: "8.1", dependencies: "highest", pimcore_version: "", phpstan_args: "", experimental: false } #- { php-version: "8.1", dependencies: "highest", pimcore_version: "11.x-dev", phpstan_args: "", experimental: true } steps: diff --git a/composer.json b/composer.json index ba4cbc2..c4372d5 100644 --- a/composer.json +++ b/composer.json @@ -6,10 +6,9 @@ "sort-packages": true }, "require": { + "php": "^8.1", + "pimcore/pimcore": "^11.x-dev", "elasticsearch/elasticsearch": "^8.0", - "symfony/config": "^5.2.0 || ^6.0", - "symfony/dependency-injection": "^5.2.0 || ^6.0", - "symfony/http-kernel": "^5.2.0 || ^6.0", "psr/log": "*" }, "require-dev": { diff --git a/src/DependencyInjection/PimcoreElasticsearchClientExtension.php b/src/DependencyInjection/PimcoreElasticsearchClientExtension.php index 9dd996c..3c1ace6 100644 --- a/src/DependencyInjection/PimcoreElasticsearchClientExtension.php +++ b/src/DependencyInjection/PimcoreElasticsearchClientExtension.php @@ -17,6 +17,7 @@ use Elastic\Elasticsearch\Client; use Pimcore\Bundle\ElasticsearchClientBundle\EsClientFactory; +use Pimcore\Bundle\ElasticsearchClientBundle\SearchClient\SearchClient; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -28,6 +29,7 @@ class PimcoreElasticsearchClientExtension extends ConfigurableExtension implements PrependExtensionInterface { const CLIENT_SERVICE_PREFIX = 'pimcore.elasticsearch_client.'; + const PIMCORE_CLIENT_PREFIX = 'pimcore.elasticsearch.custom_client.'; protected function loadInternal(array $mergedConfig, ContainerBuilder $container) { @@ -43,6 +45,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container $definition->setArgument('$configuration', $clientConfig); $definition->addTag('monolog.logger', ['channel' => $clientConfig['logger_channel']]); $definitions[self::CLIENT_SERVICE_PREFIX . $name] = $definition; + + $customClientDefinition = new Definition(SearchClient::class); + $customClientDefinition->setArgument('$client', $definition); + $definitions[self::PIMCORE_CLIENT_PREFIX . $name] = $customClientDefinition; } $container->addDefinitions($definitions); diff --git a/src/SearchClient/ElasticsearchClientInterface.php b/src/SearchClient/ElasticsearchClientInterface.php new file mode 100644 index 0000000..939a4e0 --- /dev/null +++ b/src/SearchClient/ElasticsearchClientInterface.php @@ -0,0 +1,25 @@ +client; + } + + /** + * @throws ClientException + */ + public function create(array $params): array + { + try { + return $this->getArrayResponse($this->client->create($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to create data'); + } + } + + /** + * @throws ClientException + */ + public function search(array $params): array + { + try { + return $this->getArrayResponse($this->client->search($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to search data'); + } + } + + /** + * @throws ClientException + */ + public function get(array $params): array + { + try { + return $this->getArrayResponse($this->client->get($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to get data'); + } + } + + /** + * @throws ClientException + */ + public function exists(array $params): bool + { + try { + return $this->getBoolResponse($this->client->exists($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithBool($exception, 'Failed to check if data exists'); + } + } + + /** + * @throws ClientException + */ + public function count(array $params): array + { + try { + return $this->getArrayResponse($this->client->count($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to count data'); + } + } + + /** + * @throws ClientException + */ + public function index(array $params): array + { + try { + return $this->getArrayResponse($this->client->index($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Index operation failed'); + } + } + + /** + * @throws ClientException + */ + public function bulk(array $params): array + { + try { + return $this->getArrayResponse($this->client->bulk($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Bulk operation failed'); + } + } + + /** + * @throws ClientException + */ + public function delete(array $params): array + { + try { + return $this->getArrayResponse($this->client->delete($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Delete operation failed'); + } + } + + /** + * @throws ClientException + */ + public function updateByQuery(array $params): array + { + try { + return $this->getArrayResponse($this->client->updateByQuery($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to update by query'); + } + } + + /** + * @throws ClientException + */ + public function deleteByQuery(array $params): array + { + try { + return $this->getArrayResponse($this->client->updateByQuery($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to delete by query'); + } + } + + /** + * @throws ClientException + */ + public function createIndex(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->create($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to create index'); + } + } + + /** + * @throws ClientException + */ + public function openIndex(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->create($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to open index'); + } + } + + /** + * @throws ClientException + */ + public function closeIndex(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->create($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to close index'); + } + } + + /** + * @throws ClientException + */ + public function getAllIndices(array $params): array + { + try { + return $this->getArrayResponse($this->client->cat()->indices($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to get all indices'); + } + } + + /** + * @throws ClientException + */ + public function existsIndex(array $params): bool + { + try { + return $this->getBoolResponse($this->client->indices()->exists($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithBool($exception, 'Failed to check if index exists'); + } + } + + /** + * @throws ClientException + */ + public function reIndex(array $params): array + { + try { + return $this->getArrayResponse($this->client->reindex($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to reindex'); + } + } + + /** + * @throws ClientException + */ + public function refreshIndex(array $params = []): array + { + try { + return $this->getArrayResponse($this->client->indices()->refresh($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to refresh index'); + } + } + + /** + * @throws ClientException + */ + public function flushIndex(array $params = []): array + { + try { + return $this->getArrayResponse($this->client->indices()->flush($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to flush index'); + } + } + + /** + * @throws ClientException + */ + public function deleteIndex(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->delete($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to delete index'); + } + } + + /** + * @throws ClientException + */ + public function existsIndexAlias(array $params): bool + { + try { + return $this->getBoolResponse($this->client->indices()->existsAlias($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithBool($exception, 'Failed to check if Alias exists'); + } + } + + /** + * @throws ClientException + */ + public function getIndexAlias(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->getAlias($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to get an Alias'); + } + } + + /** + * @throws ClientException + */ + public function deleteIndexAlias(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->deleteAlias($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to delete an Alias'); + } + } + + /** + * @throws ClientException + */ + public function getAllIndexAliases(array $params): array + { + try { + return $this->getArrayResponse($this->client->cat()->aliases($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to get all index Aliases'); + } + } + + /** + * @throws ClientException + */ + public function updateIndexAliases(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->updateAliases($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to update Aliases'); + } + } + + /** + * @throws ClientException + */ + public function putIndexMapping(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->putMapping($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to put mapping'); + } + } + + /** + * @throws ClientException + */ + public function getIndexMapping(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->getMapping($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to get mapping'); + } + } + + /** + * @throws ClientException + */ + public function getIndexSettings(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->getSettings($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to get index settings'); + } + } + + /** + * @throws ClientException + */ + public function putIndexSettings(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->putSettings($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to update index settings'); + } + } + + /** + * @throws ClientException + */ + public function getIndexStats(array $params): array + { + try { + return $this->getArrayResponse($this->client->indices()->stats($params)); + } catch (Exception $exception) { + return $this->handleExceptionsWithArray($exception, 'Failed to get index stats'); + } + } + + /** + * @throws ClientException + */ + private function handleExceptionsWIthBool(Exception $exception, string $message): bool + { + if ($exception instanceof ClientResponseException && $exception->getCode() === 404) { + return false; + } + + throw new ClientException( + sprintf('%s :%s', $message, $exception->getMessage()) + ); + } + + /** + * @throws ClientException + */ + private function handleExceptionsWithArray(Exception $exception, string $message): array + { + if ($exception instanceof ClientResponseException && $exception->getCode() === 404) { + return []; + } + + throw new ClientException( + sprintf('%s :%s', $message, $exception->getMessage()) + ); + } + + private function getArrayResponse(Elasticsearch $response): array + { + return $response->asArray(); + } + + private function getBoolResponse(Elasticsearch $response): bool + { + return $response->asBool(); + } +}