From 1e9e4f1e8c0dbcf0bf6e664d3f6cb89fa2c5e345 Mon Sep 17 00:00:00 2001 From: Damien Alexandre Date: Fri, 12 Jul 2024 16:25:59 +0200 Subject: [PATCH] fix(bridge): Make sure the client config option is an array (#189) Refs: #187 --- phpstan-baseline.neon | 5 ----- .../DependencyInjection/Configuration.php | 4 +++- .../DependencyInjection/ElasticallyExtension.php | 2 +- src/Messenger/IndexationRequestHandler.php | 3 ++- .../IndexationRequestSpoolSubscriber.php | 16 +++++++--------- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index cee7f76..47dd2dd 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -9,8 +9,3 @@ parameters: message: "#^Method JoliCode\\\\Elastically\\\\Index\\:\\:getClient\\(\\) should return JoliCode\\\\Elastically\\\\Client but returns Elastica\\\\Client\\.$#" count: 1 path: src/Index.php - - - - message: "#^Call to an undefined method Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\ResponseEvent\\:\\:isMasterRequest\\(\\)\\.$#" - count: 1 - path: src/Messenger/IndexationRequestSpoolSubscriber.php diff --git a/src/Bridge/Symfony/DependencyInjection/Configuration.php b/src/Bridge/Symfony/DependencyInjection/Configuration.php index a12d272..a6cbaef 100644 --- a/src/Bridge/Symfony/DependencyInjection/Configuration.php +++ b/src/Bridge/Symfony/DependencyInjection/Configuration.php @@ -28,13 +28,15 @@ public function getConfigTreeBuilder(): TreeBuilder ->normalizeKeys(false) ->prototype('array') ->children() - ->variableNode('client') + ->arrayNode('client') ->info('All options for the Elastica client constructor') ->example([ 'host' => '%env(ELASTICSEARCH_HOST)%', 'transport' => 'JoliCode\Elastically\Transport\HttpClientTransport', ]) + ->normalizeKeys(false) ->defaultValue([]) + ->prototype('variable')->end() ->end() ->scalarNode('mapping_directory') ->info('Path to the mapping directory (in YAML)') diff --git a/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php b/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php index cf178f7..2c15ea1 100644 --- a/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php +++ b/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php @@ -70,7 +70,7 @@ private function buildConnection(string $name, array $config, bool $isDefaultCon if (\array_key_exists('client', $config) && \array_key_exists('transport', $config['client'])) { $config['client']['transport'] = new Reference($config['client']['transport']); } - $client->replaceArgument('$config', $config['client']); + $client->replaceArgument('$config', $config['client'] ?? []); $client->replaceArgument('$resultSetBuilder', new Reference("elastically.{$name}.result_set_builder")); $client->replaceArgument('$indexNameMapper', new Reference("elastically.{$name}.index_name_mapper")); $container->setDefinition($id = "elastically.{$name}.client", $client); diff --git a/src/Messenger/IndexationRequestHandler.php b/src/Messenger/IndexationRequestHandler.php index 241bd0e..ccde51c 100644 --- a/src/Messenger/IndexationRequestHandler.php +++ b/src/Messenger/IndexationRequestHandler.php @@ -58,8 +58,9 @@ public function __construct(Client $client, MessageBusInterface $bus, DocumentEx /** * @throws ExceptionInterface * @throws UnrecoverableMessageHandlingException + * @throws \Symfony\Component\Messenger\Exception\ExceptionInterface */ - public function __invoke(IndexationRequestInterface $message) + public function __invoke(IndexationRequestInterface $message): void { $messages = []; if ($message instanceof MultipleIndexationRequest) { diff --git a/src/Messenger/IndexationRequestSpoolSubscriber.php b/src/Messenger/IndexationRequestSpoolSubscriber.php index cd87621..4e9b533 100644 --- a/src/Messenger/IndexationRequestSpoolSubscriber.php +++ b/src/Messenger/IndexationRequestSpoolSubscriber.php @@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\Messenger\Exception\ExceptionInterface; use Symfony\Component\Messenger\Exception\TransportException; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Transport\TransportInterface; @@ -35,15 +36,16 @@ public function __construct(TransportInterface $singleTransport, MessageBusInter $this->bus = $bus; } - public function onException() + public function onException(): void { $this->wasExceptionThrown = true; } /** * @throws TransportException + * @throws ExceptionInterface */ - public function onTerminate() + public function onTerminate(): void { if ($this->wasExceptionThrown) { return; @@ -66,15 +68,11 @@ public function onTerminate() } /** - * @throws TransportException + * @throws TransportException|ExceptionInterface */ - public function onResponse(ResponseEvent $event) + public function onResponse(ResponseEvent $event): void { - if (method_exists($event, 'isMainRequest')) { - if (!$event->isMainRequest()) { - return; - } - } elseif (!$event->isMasterRequest()) { + if (!$event->isMainRequest()) { return; }