1616use Symfony \Component \HttpFoundation \Request ;
1717use Symfony \Component \HttpFoundation \Response ;
1818use Symfony \Component \Routing \Attribute \Route ;
19+ use Throwable ;
1920
2021final class SearchController extends AbstractController
2122{
2223 public function __construct (
2324 private readonly XapianSearchService $ xapianSearchService ,
2425 private readonly XapianIndexService $ xapianIndexService ,
2526 private readonly EntityManagerInterface $ em ,
26- ) {
27- }
27+ ) {}
2828
2929 /**
3030 * Minimal Xapian search endpoint returning JSON.
@@ -38,12 +38,12 @@ public function __construct(
3838 )]
3939 public function xapianSearchAction (Request $ request ): JsonResponse
4040 {
41- $ q = \ trim ((string ) $ request ->query ->get ('q ' , '' ));
41+ $ q = trim ((string ) $ request ->query ->get ('q ' , '' ));
4242
43- if ($ q === '' ) {
43+ if ('' === $ q ) {
4444 return $ this ->json ([
45- 'query ' => '' ,
46- 'total ' => 0 ,
45+ 'query ' => '' ,
46+ 'total ' => 0 ,
4747 'results ' => [],
4848 ]);
4949 }
@@ -56,11 +56,11 @@ public function xapianSearchAction(Request $request): JsonResponse
5656 );
5757
5858 return $ this ->json ([
59- 'query ' => $ q ,
60- 'total ' => $ result ['count ' ],
59+ 'query ' => $ q ,
60+ 'total ' => $ result ['count ' ],
6161 'results ' => $ result ['results ' ],
6262 ]);
63- } catch (\ Throwable $ e ) {
63+ } catch (Throwable $ e ) {
6464 return $ this ->json ([
6565 'query ' => $ q ,
6666 'error ' => $ e ->getMessage (),
@@ -80,35 +80,35 @@ public function xapianSearchAction(Request $request): JsonResponse
8080 )]
8181 public function xapianSearchPageAction (Request $ request ): Response
8282 {
83- $ q = \ trim ((string ) $ request ->query ->get ('q ' , '' ));
83+ $ q = trim ((string ) $ request ->query ->get ('q ' , '' ));
8484
8585 $ total = 0 ;
8686 $ results = [];
8787 $ error = null ;
8888
89- if ($ q !== '' ) {
89+ if ('' !== $ q ) {
9090 try {
9191 $ searchResult = $ this ->xapianSearchService ->search (
9292 queryString: $ q ,
9393 offset: 0 ,
9494 length: 20
9595 );
9696
97- $ total = $ searchResult ['count ' ] ?? 0 ;
97+ $ total = $ searchResult ['count ' ] ?? 0 ;
9898 $ results = $ searchResult ['results ' ] ?? [];
9999
100100 $ results = $ this ->hydrateResultsWithCourseRootNode ($ results );
101101 $ results = $ this ->hydrateQuestionResultsWithQuizIds ($ results );
102- } catch (\ Throwable $ e ) {
102+ } catch (Throwable $ e ) {
103103 $ error = $ e ->getMessage ();
104104 }
105105 }
106106
107107 return $ this ->render ('@ChamiloCore/Search/xapian_search.html.twig ' , [
108- 'query ' => $ q ,
109- 'total ' => $ total ,
108+ 'query ' => $ q ,
109+ 'total ' => $ total ,
110110 'results ' => $ results ,
111- 'error ' => $ error ,
111+ 'error ' => $ error ,
112112 ]);
113113 }
114114
@@ -129,12 +129,12 @@ public function xapianDemoIndexAction(): JsonResponse
129129
130130 return $ this ->json ([
131131 'indexed ' => true ,
132- 'doc_id ' => $ docId ,
132+ 'doc_id ' => $ docId ,
133133 ]);
134- } catch (\ Throwable $ e ) {
134+ } catch (Throwable $ e ) {
135135 return $ this ->json ([
136136 'indexed ' => false ,
137- 'error ' => $ e ->getMessage (),
137+ 'error ' => $ e ->getMessage (),
138138 ], 500 );
139139 }
140140 }
@@ -161,22 +161,25 @@ private function hydrateResultsWithCourseRootNode(array $results): array
161161 // If the field already exists (coming from the indexer), keep it.
162162 if (!empty ($ data ['course_root_node_id ' ])) {
163163 $ result ['data ' ] = $ data ;
164+
164165 continue ;
165166 }
166167
167- $ courseId = isset ($ data ['course_id ' ]) && $ data [ ' course_id ' ] !== ''
168+ $ courseId = isset ($ data ['course_id ' ]) && '' !== $ data [ ' course_id ' ]
168169 ? (int ) $ data ['course_id ' ]
169170 : null ;
170171
171172 if (!$ courseId ) {
172173 $ result ['data ' ] = $ data ;
174+
173175 continue ;
174176 }
175177
176178 /** @var Course|null $course */
177179 $ course = $ this ->em ->find (Course::class, $ courseId );
178180 if (!$ course || !$ course ->getResourceNode ()) {
179181 $ result ['data ' ] = $ data ;
182+
180183 continue ;
181184 }
182185
@@ -239,7 +242,8 @@ private function hydrateQuestionResultsWithQuizIds(array $results): array
239242 /** @var CQuizRelQuestion|null $rel */
240243 $ rel = $ this ->em
241244 ->getRepository (CQuizRelQuestion::class)
242- ->findOneBy (['question ' => $ questionId ]);
245+ ->findOneBy (['question ' => $ questionId ])
246+ ;
243247
244248 if (!$ rel ) {
245249 $ result ['data ' ] = $ data ;
0 commit comments