@@ -81,7 +81,6 @@ class FaqController extends AbstractController
8181 )]
8282 public function getByCategoryId (Request $ request ): JsonResponse
8383 {
84- $ jsonResponse = new JsonResponse ();
8584 $ configuration = Configuration::getConfigurationInstance ();
8685 $ user = CurrentUser::getCurrentUser ($ configuration );
8786
@@ -95,15 +94,10 @@ public function getByCategoryId(Request $request): JsonResponse
9594
9695 try {
9796 $ result = $ faq ->getAllAvailableFaqsByCategoryId ($ categoryId );
98- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
99- $ jsonResponse ->setData ($ result );
97+ return $ this ->json ($ result , Response::HTTP_OK );
10098 } catch (Exception $ e ) {
101- $ jsonResponse ->setStatusCode (Response::HTTP_INTERNAL_SERVER_ERROR );
102- $ jsonResponse ->setData (['error ' => $ e ->getMessage ()]);
103- return $ jsonResponse ;
99+ return $ this ->json (['error ' => $ e ->getMessage ()], Response::HTTP_INTERNAL_SERVER_ERROR );
104100 }
105-
106- return $ jsonResponse ;
107101 }
108102
109103 /**
@@ -164,7 +158,6 @@ public function getByCategoryId(Request $request): JsonResponse
164158 )]
165159 public function getById (Request $ request ): JsonResponse
166160 {
167- $ jsonResponse = new JsonResponse ();
168161 $ configuration = Configuration::getConfigurationInstance ();
169162 $ user = CurrentUser::getCurrentUser ($ configuration );
170163
@@ -181,13 +174,10 @@ public function getById(Request $request): JsonResponse
181174
182175 if ((is_countable ($ result ) ? count ($ result ) : 0 ) === 0 || $ result ['solution_id ' ] === 42 ) {
183176 $ result = new stdClass ();
184- $ jsonResponse -> setStatusCode ( Response::HTTP_NOT_FOUND );
177+ return $ this -> json ( $ result , Response::HTTP_NOT_FOUND );
185178 } else {
186- $ jsonResponse -> setStatusCode ( Response::HTTP_OK );
179+ return $ this -> json ( $ result , Response::HTTP_OK );
187180 }
188-
189- $ jsonResponse ->setData ($ result );
190- return $ jsonResponse ;
191181 }
192182
193183 /**
@@ -236,7 +226,6 @@ public function getById(Request $request): JsonResponse
236226 )]
237227 public function getByTagId (Request $ request ): JsonResponse
238228 {
239- $ jsonResponse = new JsonResponse ();
240229 $ configuration = Configuration::getConfigurationInstance ();
241230 $ user = CurrentUser::getCurrentUser ($ configuration );
242231
@@ -253,15 +242,10 @@ public function getByTagId(Request $request): JsonResponse
253242
254243 try {
255244 $ result = $ faq ->getRecordsByIds ($ recordIds );
256- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
257- $ jsonResponse ->setData ($ result );
245+ return $ this ->json ($ result , Response::HTTP_OK );
258246 } catch (Exception $ e ) {
259- $ jsonResponse ->setStatusCode (Response::HTTP_INTERNAL_SERVER_ERROR );
260- $ jsonResponse ->setData (['error ' => $ e ->getMessage ()]);
261- return $ jsonResponse ;
247+ return $ this ->json (['error ' => $ e ->getMessage ()], Response::HTTP_INTERNAL_SERVER_ERROR );
262248 }
263-
264- return $ jsonResponse ;
265249 }
266250
267251 /**
@@ -298,7 +282,6 @@ public function getByTagId(Request $request): JsonResponse
298282 )]
299283 public function getPopular (): JsonResponse
300284 {
301- $ jsonResponse = new JsonResponse ();
302285 $ configuration = Configuration::getConfigurationInstance ();
303286 $ user = CurrentUser::getCurrentUser ($ configuration );
304287
@@ -311,13 +294,10 @@ public function getPopular(): JsonResponse
311294 $ result = array_values ($ faq ->getTopTenData ());
312295
313296 if ((is_countable ($ result ) ? count ($ result ) : 0 ) === 0 ) {
314- $ jsonResponse ->setStatusCode (Response::HTTP_NOT_FOUND );
315- return $ jsonResponse ;
297+ $ this ->json ($ result , Response::HTTP_NOT_FOUND );
316298 }
317299
318- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
319- $ jsonResponse ->setData ($ result );
320- return $ jsonResponse ;
300+ return $ this ->json ($ result , Response::HTTP_OK );
321301 }
322302
323303 /**
@@ -367,13 +347,10 @@ public function getLatest(): JsonResponse
367347 $ result = array_values ($ faq ->getLatestData ());
368348
369349 if ((is_countable ($ result ) ? count ($ result ) : 0 ) === 0 ) {
370- $ jsonResponse ->setStatusCode (Response::HTTP_NOT_FOUND );
371- return $ jsonResponse ;
350+ return $ this ->json ($ result , Response::HTTP_NOT_FOUND );
372351 }
373352
374- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
375- $ jsonResponse ->setData ($ result );
376- return $ jsonResponse ;
353+ return $ this ->json ($ result , Response::HTTP_OK );
377354 }
378355
379356 /**
@@ -415,7 +392,6 @@ public function getLatest(): JsonResponse
415392 )]
416393 public function getSticky (): JsonResponse
417394 {
418- $ jsonResponse = new JsonResponse ();
419395 $ configuration = Configuration::getConfigurationInstance ();
420396 $ user = CurrentUser::getCurrentUser ($ configuration );
421397
@@ -428,13 +404,10 @@ public function getSticky(): JsonResponse
428404 $ result = array_values ($ faq ->getStickyRecordsData ());
429405
430406 if ((is_countable ($ result ) ? count ($ result ) : 0 ) === 0 ) {
431- $ jsonResponse ->setStatusCode (Response::HTTP_NOT_FOUND );
432- return $ jsonResponse ;
407+ return $ this ->json ($ result , Response::HTTP_NOT_FOUND );
433408 }
434409
435- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
436- $ jsonResponse ->setData ($ result );
437- return $ jsonResponse ;
410+ return $ this ->json ($ result , Response::HTTP_OK );
438411 }
439412
440413 /**
@@ -496,13 +469,10 @@ public function list(): JsonResponse
496469 $ result = $ faq ->faqRecords ;
497470
498471 if ((is_countable ($ result ) ? count ($ result ) : 0 ) === 0 ) {
499- $ jsonResponse ->setStatusCode (Response::HTTP_NOT_FOUND );
500- return $ jsonResponse ;
472+ return $ this ->json ($ result , Response::HTTP_NOT_FOUND );
501473 }
502474
503- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
504- $ jsonResponse ->setData ($ result );
505- return $ jsonResponse ;
475+ return $ this ->json ($ result , Response::HTTP_OK );
506476 }
507477
508478 /**
@@ -639,26 +609,22 @@ public function create(Request $request): JsonResponse
639609 if (!is_null ($ categoryName )) {
640610 $ categoryIdFound = $ category ->getCategoryIdFromName ($ categoryName );
641611 if ($ categoryIdFound === false ) {
642- $ jsonResponse ->setStatusCode (Response::HTTP_CONFLICT );
643612 $ result = [
644613 'stored ' => false ,
645614 'error ' => 'The given category name was not found. '
646615 ];
647- $ jsonResponse ->setData ($ result );
648- return $ jsonResponse ;
616+ return $ this ->json ($ result , Response::HTTP_CONFLICT );
649617 }
650618
651619 $ categoryId = $ categoryIdFound ;
652620 }
653621
654622 if ($ faq ->hasTitleAHash ($ question )) {
655- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
656623 $ result = [
657624 'stored ' => false ,
658625 'error ' => 'It is not allowed, that the question title contains a hash. '
659626 ];
660- $ jsonResponse ->setData ($ result );
661- return $ jsonResponse ;
627+ return $ this ->json ($ result , Response::HTTP_BAD_REQUEST );
662628 }
663629
664630 $ categories = [ $ categoryId ];
@@ -687,9 +653,7 @@ public function create(Request $request): JsonResponse
687653 ->setCategories ($ categories )
688654 ->save ();
689655
690- $ jsonResponse ->setStatusCode (Response::HTTP_CREATED );
691- $ jsonResponse ->setData (['stored ' => true ]);
692- return $ jsonResponse ;
656+ return $ this ->json (['stored ' => true ], Response::HTTP_CREATED );
693657 }
694658
695659 /**
@@ -699,8 +663,8 @@ public function create(Request $request): JsonResponse
699663 #[OA \Put(
700664 path: '/api/v3.0/faq/update ' ,
701665 operationId: 'updateFaq ' ,
702- tags: [ ' Endpoints with Authentication ' ] ,
703- description: ' Used to update a FAQ in one existing category. '
666+ description: ' Used to update a FAQ in one existing category. ' ,
667+ tags: [ ' Endpoints with Authentication ' ]
704668 )]
705669 #[OA \Header(
706670 header: 'Accept-Language ' ,
@@ -778,7 +742,6 @@ public function update(Request $request): JsonResponse
778742 {
779743 $ this ->hasValidToken ();
780744
781- $ jsonResponse = new JsonResponse ();
782745 $ configuration = Configuration::getConfigurationInstance ();
783746 $ user = CurrentUser::getCurrentUser ($ configuration );
784747
@@ -808,13 +771,11 @@ public function update(Request $request): JsonResponse
808771 $ isSticky = Filter::filterVar ($ data ->{'is-sticky ' }, FILTER_VALIDATE_BOOLEAN );
809772
810773 if ($ faq ->hasTitleAHash ($ question )) {
811- $ jsonResponse ->setStatusCode (Response::HTTP_BAD_REQUEST );
812774 $ result = [
813775 'stored ' => false ,
814776 'error ' => 'It is not allowed, that the question title contains a hash. '
815777 ];
816- $ jsonResponse ->setData ($ result );
817- return $ jsonResponse ;
778+ return $ this ->json ($ result , Response::HTTP_BAD_REQUEST );
818779 }
819780
820781 $ isActive = !is_null ($ isActive );
@@ -837,8 +798,6 @@ public function update(Request $request): JsonResponse
837798
838799 $ faq ->update ($ faqData );
839800
840- $ jsonResponse ->setStatusCode (Response::HTTP_OK );
841- $ jsonResponse ->setData (['stored ' => true ]);
842- return $ jsonResponse ;
801+ return $ this ->json (['stored ' => true ], Response::HTTP_OK );
843802 }
844803}
0 commit comments