From ecdcf2f4ff97b8f31e3feaf10b9d7c60d1cf71a2 Mon Sep 17 00:00:00 2001 From: Etienne Gutbub Date: Tue, 30 Jul 2024 12:24:47 +0200 Subject: [PATCH 1/2] feat(Twig): Add twig method to check if elasticSearch is available --- src/Resources/config/services.yaml | 4 +++ src/Twig/Extension/RenderSearchForm.php | 20 +++---------- src/Twig/Extension/SearchExtension.php | 38 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 src/Twig/Extension/SearchExtension.php diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index a264a82e..42bf5ef3 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -269,6 +269,10 @@ services: arguments: $elasticsearchChecker: '@monsieurbiz.search.checker.elasticsearch_checker' + MonsieurBiz\SyliusSearchPlugin\Twig\Extension\SearchExtension: + arguments: + $elasticsearchChecker: '@monsieurbiz.search.checker.elasticsearch_checker' + # Routing Context MonsieurBiz\SyliusSearchPlugin\Routing\RequestContext: decorates: router.request_context diff --git a/src/Twig/Extension/RenderSearchForm.php b/src/Twig/Extension/RenderSearchForm.php index 9f2c8a3e..f6870191 100644 --- a/src/Twig/Extension/RenderSearchForm.php +++ b/src/Twig/Extension/RenderSearchForm.php @@ -24,24 +24,12 @@ class RenderSearchForm extends AbstractExtension { - private FormFactoryInterface $formFactory; - - private Environment $templatingEngine; - - private RequestStack $requestStack; - - private ElasticsearchCheckerInterface $elasticsearchChecker; - public function __construct( - FormFactoryInterface $formFactory, - Environment $templatingEngine, - RequestStack $requestStack, - ElasticsearchCheckerInterface $elasticsearchChecker + private FormFactoryInterface $formFactory, + private Environment $templatingEngine, + private RequestStack $requestStack, + private ElasticsearchCheckerInterface $elasticsearchChecker ) { - $this->formFactory = $formFactory; - $this->templatingEngine = $templatingEngine; - $this->requestStack = $requestStack; - $this->elasticsearchChecker = $elasticsearchChecker; } public function getFunctions() diff --git a/src/Twig/Extension/SearchExtension.php b/src/Twig/Extension/SearchExtension.php new file mode 100644 index 00000000..42d8c74b --- /dev/null +++ b/src/Twig/Extension/SearchExtension.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusSearchPlugin\Twig\Extension; + +use MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchCheckerInterface; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class SearchExtension extends AbstractExtension +{ + public function __construct( + private ElasticsearchCheckerInterface $elasticsearchChecker, + ) { + } + + public function getFunctions() + { + return [ + new TwigFunction('is_elasticsearch_available', [$this, 'isElasticsearchAvailable']), + ]; + } + + public function isElasticsearchAvailable(): bool + { + return $this->elasticsearchChecker->check(); + } +} From e033f242f4d784b1c558310782e3b634c21c9008 Mon Sep 17 00:00:00 2001 From: Etienne Gutbub Date: Tue, 30 Jul 2024 12:25:29 +0200 Subject: [PATCH 2/2] chore(tests): PHP cs fixer native_function_invocation rule --- .../ProductAttributeValueConfiguration.php | 2 +- src/Controller/SearchController.php | 2 +- src/DependencyInjection/DocumentableRegistryPass.php | 2 +- src/Exception/ObjectNotInstanceOfClassException.php | 2 +- src/Index/Indexer.php | 12 ++++++------ src/Mapping/YamlWithLocaleProvider.php | 2 +- src/Routing/RequestContext.php | 2 +- .../Aggregation/ProductAttributeAggregation.php | 6 +++--- .../Request/Aggregation/ProductOptionAggregation.php | 12 ++++++------ .../PostFilter/Product/AttributesPostFilter.php | 4 ++-- .../PostFilter/Product/MainTaxonPostFilter.php | 2 +- .../Request/PostFilter/Product/OptionsPostFilter.php | 8 ++++---- .../Request/QueryFilter/Product/SearchTermFilter.php | 8 ++++---- 13 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/AutoMapper/ProductAttributeValueConfiguration.php b/src/AutoMapper/ProductAttributeValueConfiguration.php index e9030893..bbe0b82d 100644 --- a/src/AutoMapper/ProductAttributeValueConfiguration.php +++ b/src/AutoMapper/ProductAttributeValueConfiguration.php @@ -75,7 +75,7 @@ public function getProductAttributeValue(ProductAttributeValueInterface $product } if (!\array_key_exists($productAttributeValue->getType(), $this->productAttributeValueReaders)) { // @phpstan-ignore-next-line The logger can't be null here - $this->logger->alert(sprintf('Missing product attribute value reader for "%s" type', $productAttributeValue->getType())); + $this->logger->alert(\sprintf('Missing product attribute value reader for "%s" type', $productAttributeValue->getType())); return null; } diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index 49e5f0cc..66586502 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -171,7 +171,7 @@ private function getDocumentable(?string $documentType): DocumentableInterface /** @phpstan-ignore-next-line */ return $this->documentableRegistry->get('search.documentable.' . $documentType); } catch (NonExistingServiceException $exception) { - throw new NotFoundHttpException(sprintf('Documentable "%s" not found', $documentType)); + throw new NotFoundHttpException(\sprintf('Documentable "%s" not found', $documentType)); } } diff --git a/src/DependencyInjection/DocumentableRegistryPass.php b/src/DependencyInjection/DocumentableRegistryPass.php index 39f866ca..68d064ba 100644 --- a/src/DependencyInjection/DocumentableRegistryPass.php +++ b/src/DependencyInjection/DocumentableRegistryPass.php @@ -49,7 +49,7 @@ private function validateDocumentableResource(string $class): void $interfaces = (array) (class_implements($class) ?? []); if (!\in_array(DocumentableInterface::class, $interfaces, true)) { - throw new InvalidArgumentException(sprintf('Class "%s" must implement "%s" to be registered as a Documentable.', $class, DocumentableInterface::class)); + throw new InvalidArgumentException(\sprintf('Class "%s" must implement "%s" to be registered as a Documentable.', $class, DocumentableInterface::class)); } } diff --git a/src/Exception/ObjectNotInstanceOfClassException.php b/src/Exception/ObjectNotInstanceOfClassException.php index a288030a..8b56004f 100644 --- a/src/Exception/ObjectNotInstanceOfClassException.php +++ b/src/Exception/ObjectNotInstanceOfClassException.php @@ -19,6 +19,6 @@ class ObjectNotInstanceOfClassException extends InvalidArgumentException { public static function fromClassName(string $className): self { - return new self(sprintf('Object is not instance of class "%s"', $className)); + return new self(\sprintf('Object is not instance of class "%s"', $className)); } } diff --git a/src/Index/Indexer.php b/src/Index/Indexer.php index 7e731a6b..c96df8fe 100644 --- a/src/Index/Indexer.php +++ b/src/Index/Indexer.php @@ -66,8 +66,8 @@ public function indexAll(?OutputInterface $output = null): void /** @var DocumentableInterface $documentable */ foreach ($this->documentableRegistry->all() as $documentable) { $documentable instanceof PrefixedDocumentableInterface && !empty($documentable->getPrefix()) ? - $output->writeln(sprintf('Indexing %s (Prefix: %s)', $documentable->getIndexCode(), $documentable->getPrefix())) - : $output->writeln(sprintf('Indexing %s', $documentable->getIndexCode())); + $output->writeln(\sprintf('Indexing %s (Prefix: %s)', $documentable->getIndexCode(), $documentable->getPrefix())) + : $output->writeln(\sprintf('Indexing %s', $documentable->getIndexCode())); $this->indexDocumentable($output, $documentable); } } @@ -165,11 +165,11 @@ private function indexDocumentable(OutputInterface $output, DocumentableInterfac foreach ($this->getLocales() as $localeCode) { $documentable instanceof PrefixedDocumentableInterface && !empty($documentable->getPrefix()) ? $output->writeln( - sprintf('Indexing %s for locale %s (Prefix: %s)', $documentable->getIndexCode(), $localeCode, $documentable->getPrefix()), + \sprintf('Indexing %s for locale %s (Prefix: %s)', $documentable->getIndexCode(), $localeCode, $documentable->getPrefix()), OutputInterface::VERBOSITY_VERBOSE ) : $output->writeln( - sprintf('Indexing %s for locale %s', $documentable->getIndexCode(), $localeCode), + \sprintf('Indexing %s for locale %s', $documentable->getIndexCode(), $localeCode), OutputInterface::VERBOSITY_VERBOSE ); @@ -198,10 +198,10 @@ private function indexDocumentable(OutputInterface $output, DocumentableInterfac $indexer->flush(); $indexBuilder->markAsLive($newIndex, $indexName); - $output->writeln(sprintf('Index %s is now live', $indexName), OutputInterface::VERBOSITY_VERBOSE); + $output->writeln(\sprintf('Index %s is now live', $indexName), OutputInterface::VERBOSITY_VERBOSE); $indexBuilder->speedUpRefresh($newIndex); $indexBuilder->purgeOldIndices($indexName); - $output->writeln(sprintf('Old indices for %s are now purged', $indexName), OutputInterface::VERBOSITY_VERBOSE); + $output->writeln(\sprintf('Old indices for %s are now purged', $indexName), OutputInterface::VERBOSITY_VERBOSE); } /** diff --git a/src/Mapping/YamlWithLocaleProvider.php b/src/Mapping/YamlWithLocaleProvider.php index feba5cb8..a73d18c9 100644 --- a/src/Mapping/YamlWithLocaleProvider.php +++ b/src/Mapping/YamlWithLocaleProvider.php @@ -68,7 +68,7 @@ public function provideMapping(string $indexName, array $context = []): ?array $mapping = (array) $mappingProviderEvent->getMapping(); if (empty($mapping['mappings'] ?? [])) { - throw new InvalidException(sprintf('Mapping no found for "%s" not found. Please check your configuration.', $indexName)); + throw new InvalidException(\sprintf('Mapping no found for "%s" not found. Please check your configuration.', $indexName)); } return $mapping; diff --git a/src/Routing/RequestContext.php b/src/Routing/RequestContext.php index 9773c48e..d9772cf5 100644 --- a/src/Routing/RequestContext.php +++ b/src/Routing/RequestContext.php @@ -58,6 +58,6 @@ public function __call(string $name, array $arguments) return \call_user_func($callback, ...$arguments); } - throw new Exception(sprintf('Method %s not found for class "%s"', $name, \get_class($this->decorated))); + throw new Exception(\sprintf('Method %s not found for class "%s"', $name, \get_class($this->decorated))); } } diff --git a/src/Search/Request/Aggregation/ProductAttributeAggregation.php b/src/Search/Request/Aggregation/ProductAttributeAggregation.php index 6a9fe6bc..ed967157 100644 --- a/src/Search/Request/Aggregation/ProductAttributeAggregation.php +++ b/src/Search/Request/Aggregation/ProductAttributeAggregation.php @@ -49,13 +49,13 @@ public function build($aggregation, array $filters) ->setFilter($filterQuery) ->addAggregation( /** @phpstan-ignore-next-line */ - $qb->aggregation()->nested($aggregation->getCode(), sprintf('attributes.%s', $aggregation->getCode())) + $qb->aggregation()->nested($aggregation->getCode(), \sprintf('attributes.%s', $aggregation->getCode())) ->addAggregation( $qb->aggregation()->terms('names') - ->setField(sprintf('attributes.%s.name', $aggregation->getCode())) + ->setField(\sprintf('attributes.%s.name', $aggregation->getCode())) ->addAggregation( $qb->aggregation()->terms('values') - ->setField(sprintf('attributes.%s.value.keyword', $aggregation->getCode())) + ->setField(\sprintf('attributes.%s.value.keyword', $aggregation->getCode())) ) ) ) diff --git a/src/Search/Request/Aggregation/ProductOptionAggregation.php b/src/Search/Request/Aggregation/ProductOptionAggregation.php index 70684f12..4e299946 100644 --- a/src/Search/Request/Aggregation/ProductOptionAggregation.php +++ b/src/Search/Request/Aggregation/ProductOptionAggregation.php @@ -55,15 +55,15 @@ public function build($aggregation, array $filters) $qb = new QueryBuilder(); $optionBoolConditions = $qb->query()->bool() - ->addMust($qb->query()->term([sprintf('options.%s.values.enabled', $aggregation->getCode()) => ['value' => true]])) + ->addMust($qb->query()->term([\sprintf('options.%s.values.enabled', $aggregation->getCode()) => ['value' => true]])) ; if ($this->enableStockFilter) { - $optionBoolConditions->addMust($qb->query()->term([sprintf('options.%s.values.is_in_stock', $aggregation->getCode()) => ['value' => true]])); + $optionBoolConditions->addMust($qb->query()->term([\sprintf('options.%s.values.is_in_stock', $aggregation->getCode()) => ['value' => true]])); } $valuesAggregation = $qb->aggregation()->filter('values', $optionBoolConditions) ->addAggregation( $qb->aggregation()->terms('values') - ->setField(sprintf('options.%s.values.value.keyword', $aggregation->getCode())) + ->setField(\sprintf('options.%s.values.value.keyword', $aggregation->getCode())) ) ; @@ -72,12 +72,12 @@ public function build($aggregation, array $filters) ->setFilter($filterQuery) ->addAggregation( /** @phpstan-ignore-next-line */ - $qb->aggregation()->nested($aggregation->getCode(), sprintf('options.%s', $aggregation->getCode())) + $qb->aggregation()->nested($aggregation->getCode(), \sprintf('options.%s', $aggregation->getCode())) ->addAggregation( $qb->aggregation()->terms('names') - ->setField(sprintf('options.%s.name', $aggregation->getCode())) + ->setField(\sprintf('options.%s.name', $aggregation->getCode())) ->addAggregation( - $qb->aggregation()->nested('values', sprintf('options.%s.values', $aggregation->getCode())) + $qb->aggregation()->nested('values', \sprintf('options.%s.values', $aggregation->getCode())) ->addAggregation( $valuesAggregation ) diff --git a/src/Search/Request/PostFilter/Product/AttributesPostFilter.php b/src/Search/Request/PostFilter/Product/AttributesPostFilter.php index c7209a7a..d637d1de 100644 --- a/src/Search/Request/PostFilter/Product/AttributesPostFilter.php +++ b/src/Search/Request/PostFilter/Product/AttributesPostFilter.php @@ -28,12 +28,12 @@ public function apply(BoolQuery $boolQuery, RequestConfiguration $requestConfigu $attributeValueQuery = $qb->query()->bool(); foreach ($values as $value) { - $termQuery = $qb->query()->term([sprintf('attributes.%s.value.keyword', $field) => SlugHelper::toLabel($value)]); + $termQuery = $qb->query()->term([\sprintf('attributes.%s.value.keyword', $field) => SlugHelper::toLabel($value)]); $attributeValueQuery->addShould($termQuery); // todo configure the "and" or "or" } $attributeQuery = $qb->query()->nested(); - $attributeQuery->setPath(sprintf('attributes.%s', $field))->setQuery($attributeValueQuery); + $attributeQuery->setPath(\sprintf('attributes.%s', $field))->setQuery($attributeValueQuery); $boolQuery->addMust($attributeQuery); } diff --git a/src/Search/Request/PostFilter/Product/MainTaxonPostFilter.php b/src/Search/Request/PostFilter/Product/MainTaxonPostFilter.php index c8ced4c8..089fc90b 100644 --- a/src/Search/Request/PostFilter/Product/MainTaxonPostFilter.php +++ b/src/Search/Request/PostFilter/Product/MainTaxonPostFilter.php @@ -33,7 +33,7 @@ public function apply(BoolQuery $boolQuery, RequestConfiguration $requestConfigu $mainTaxonQuery->addShould( $qb->query() ->term() - ->setTerm(sprintf('%s.code', $field), SlugHelper::toLabel($value)) + ->setTerm(\sprintf('%s.code', $field), SlugHelper::toLabel($value)) ); } $boolQuery->addMust( diff --git a/src/Search/Request/PostFilter/Product/OptionsPostFilter.php b/src/Search/Request/PostFilter/Product/OptionsPostFilter.php index 56217bc0..8acd81db 100644 --- a/src/Search/Request/PostFilter/Product/OptionsPostFilter.php +++ b/src/Search/Request/PostFilter/Product/OptionsPostFilter.php @@ -34,19 +34,19 @@ public function apply(BoolQuery $boolQuery, RequestConfiguration $requestConfigu foreach ($requestConfiguration->getAppliedFilters('options') as $field => $values) { $optionValueQuery = $qb->query()->bool(); foreach ($values as $value) { - $termQuery = $qb->query()->term([sprintf('options.%s.values.value.keyword', $field) => SlugHelper::toLabel($value)]); + $termQuery = $qb->query()->term([\sprintf('options.%s.values.value.keyword', $field) => SlugHelper::toLabel($value)]); $optionValueQuery->addShould($termQuery); // todo configure the "and" or "or" } $optionQuery = $qb->query()->nested(); $condition = $qb->query()->bool() - ->addMust($qb->query()->term([sprintf('options.%s.values.enabled', $field) => true])) + ->addMust($qb->query()->term([\sprintf('options.%s.values.enabled', $field) => true])) ; if ($this->enableStockFilter) { - $condition->addMust($qb->query()->term([sprintf('options.%s.values.is_in_stock', $field) => true])); + $condition->addMust($qb->query()->term([\sprintf('options.%s.values.is_in_stock', $field) => true])); } $condition->addMust($optionValueQuery); - $optionQuery->setPath(sprintf('options.%s.values', $field))->setQuery($condition); + $optionQuery->setPath(\sprintf('options.%s.values', $field))->setQuery($condition); $boolQuery->addMust($optionQuery); } diff --git a/src/Search/Request/QueryFilter/Product/SearchTermFilter.php b/src/Search/Request/QueryFilter/Product/SearchTermFilter.php index c7c37fad..1ccacd92 100644 --- a/src/Search/Request/QueryFilter/Product/SearchTermFilter.php +++ b/src/Search/Request/QueryFilter/Product/SearchTermFilter.php @@ -54,13 +54,13 @@ private function addAttributesQueries(BoolQuery $searchQuery, RequestConfigurati $attributeValueQuery = $qb->query()->multi_match(); $attributeValueQuery->setFields([ - sprintf('attributes.%s.value^%d', $productAttribute->getCode(), $productAttribute->getSearchWeight()), + \sprintf('attributes.%s.value^%d', $productAttribute->getCode(), $productAttribute->getSearchWeight()), ]); $attributeValueQuery->setQuery($requestConfiguration->getQueryText()); $attributeValueQuery->setFuzziness(MultiMatch::FUZZINESS_AUTO); $attributeQuery = $qb->query()->nested(); - $attributeQuery->setPath(sprintf('attributes.%s', $productAttribute->getCode()))->setQuery($attributeValueQuery); + $attributeQuery->setPath(\sprintf('attributes.%s', $productAttribute->getCode()))->setQuery($attributeValueQuery); $searchQuery->addShould($attributeQuery); } @@ -76,13 +76,13 @@ private function addOptionsQueries(BoolQuery $searchQuery, RequestConfiguration $attributeValueQuery = $qb->query()->multi_match(); $attributeValueQuery->setFields([ - sprintf('options.%s.values.value^%d', $productOption->getCode(), $productOption->getSearchWeight()), + \sprintf('options.%s.values.value^%d', $productOption->getCode(), $productOption->getSearchWeight()), ]); $attributeValueQuery->setQuery($requestConfiguration->getQueryText()); $attributeValueQuery->setFuzziness(MultiMatch::FUZZINESS_AUTO); $attributeQuery = $qb->query()->nested(); - $attributeQuery->setPath(sprintf('options.%s.values', $productOption->getCode()))->setQuery($attributeValueQuery); + $attributeQuery->setPath(\sprintf('options.%s.values', $productOption->getCode()))->setQuery($attributeValueQuery); $searchQuery->addShould($attributeQuery); }