From b8888df92f7d37201ab4c18dd31515b8b73aa2e0 Mon Sep 17 00:00:00 2001 From: Bojan Date: Mon, 5 Aug 2024 13:29:06 +0200 Subject: [PATCH] Refactor code, resolving PR comments --- .../Search/ProductSearchRoute.php | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Decorator/Core/Content/Product/SalesChannel/Search/ProductSearchRoute.php b/src/Decorator/Core/Content/Product/SalesChannel/Search/ProductSearchRoute.php index 7aeb4d85..4384df68 100644 --- a/src/Decorator/Core/Content/Product/SalesChannel/Search/ProductSearchRoute.php +++ b/src/Decorator/Core/Content/Product/SalesChannel/Search/ProductSearchRoute.php @@ -8,7 +8,10 @@ use Nosto\NostoIntegration\Model\ConfigProvider; use Nosto\NostoIntegration\Traits\SearchResultHelper; use Nosto\NostoIntegration\Utils\SearchHelper; +use Psr\Log\LoggerInterface; use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition; +use Shopware\Core\Content\Product\Events\ProductSearchResultEvent; +use Shopware\Core\Content\Product\ProductEvents; use Shopware\Core\Content\Product\SalesChannel\Listing\Processor\CompositeListingProcessor; use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult; use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter; @@ -32,13 +35,15 @@ class ProductSearchRoute extends AbstractProductSearchRoute use SearchResultHelper; public function __construct( - private readonly AbstractProductSearchRoute $decorated, - private readonly EventDispatcherInterface $eventDispatcher, + private readonly AbstractProductSearchRoute $decorated, + private readonly EventDispatcherInterface $eventDispatcher, private readonly ProductSearchBuilderInterface $searchBuilder, - private readonly SalesChannelRepository $salesChannelProductRepository, - private readonly CompositeListingProcessor $listingProcessor, - private readonly ConfigProvider $configProvider, - ) { + private readonly SalesChannelRepository $salesChannelProductRepository, + private readonly CompositeListingProcessor $listingProcessor, + private readonly ConfigProvider $configProvider, + private readonly LoggerInterface $logger + ) + { } public function getDecorated(): AbstractProductSearchRoute @@ -47,10 +52,11 @@ public function getDecorated(): AbstractProductSearchRoute } public function load( - Request $request, + Request $request, SalesChannelContext $context, - Criteria $criteria, - ): ProductSearchRouteResponse { + Criteria $criteria, + ): ProductSearchRouteResponse + { try { if (!SearchHelper::shouldHandleRequest($context, $this->configProvider)) { return $this->decorated->load($request, $context, $criteria); @@ -84,17 +90,27 @@ public function load( $this->listingProcessor->process($request, $productListing, $context); + $this->eventDispatcher->dispatch( + new ProductSearchResultEvent($request, $productListing, $context), + ProductEvents::PRODUCT_SEARCH_RESULT + ); + return new ProductSearchRouteResponse($productListing); - } catch (Exception) { + } catch (RoutingException $e) { + $this->logger->error('Routing exception occurred: ' . $e->getMessage()); + return $this->decorated->load($request, $context, $criteria); + } catch (Exception $e) { + $this->logger->error('An unexpected error occurred: ' . $e->getMessage()); return $this->decorated->load($request, $context, $criteria); } } private function fetchProductsById( - Criteria $criteria, + Criteria $criteria, SalesChannelContext $salesChannelContext, - ?string $query, - ): EntitySearchResult { + ?string $query, + ): EntitySearchResult + { if (empty($criteria->getIds())) { return $this->createEmptySearchResult($criteria, $salesChannelContext->getContext()); }