From 04eb240ae8a76038f4ea047cdecc0c8f369d22f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20S=C4=85gol?= Date: Thu, 15 Feb 2024 15:09:39 +0100 Subject: [PATCH] [BUGFIX] Use DI when instantiating ElasticRepository inside SearchController --- src/Controller/SearchController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index 9256698..646b04d 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -13,6 +13,11 @@ class SearchController extends AbstractController { + public function __construct(private readonly ElasticRepository $elasticRepository) + { + } + + /** * @return Response */ @@ -32,13 +37,12 @@ public function search(Request $request): Response if ($request->query->get('q', '') === '') { return $this->redirectToRoute('index'); } - $elasticRepository = new ElasticRepository(); $searchDemand = SearchDemand::createFromRequest($request); return $this->render('search/search.html.twig', [ 'q' => $searchDemand->getQuery(), 'filters' => $request->get('filters', []), - 'results' => $elasticRepository->findByQuery($searchDemand), + 'results' => $this->elasticRepository->findByQuery($searchDemand), ]); } @@ -49,10 +53,9 @@ public function search(Request $request): Response #[Route(path: '/suggest', name: 'suggest')] public function suggest(Request $request): Response { - $elasticRepository = new ElasticRepository(); $searchDemand = SearchDemand::createFromRequest($request); - $results = $elasticRepository->suggest($searchDemand); + $results = $this->elasticRepository->suggest($searchDemand); $suggestions = []; foreach ($results['results'] as $result) { $hit = $result->getData();