Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AutoCompleteController | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| search | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The Autocomplete Controller |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2023-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2023-07-29 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Controller\Frontend\Api; |
| 21 | |
| 22 | use phpMyFAQ\Category; |
| 23 | use phpMyFAQ\Controller\AbstractController; |
| 24 | use phpMyFAQ\Faq\Permission; |
| 25 | use phpMyFAQ\Filter; |
| 26 | use phpMyFAQ\Helper\SearchHelper; |
| 27 | use phpMyFAQ\Language\Plurals; |
| 28 | use phpMyFAQ\Search; |
| 29 | use phpMyFAQ\Search\SearchResultSet; |
| 30 | use phpMyFAQ\User\CurrentUser; |
| 31 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 32 | use Symfony\Component\HttpFoundation\Request; |
| 33 | use Symfony\Component\HttpFoundation\Response; |
| 34 | use Symfony\Component\Routing\Attribute\Route; |
| 35 | |
| 36 | final class AutoCompleteController extends AbstractController |
| 37 | { |
| 38 | public function __construct( |
| 39 | private readonly Permission $faqPermission, |
| 40 | private readonly Search $faqSearch, |
| 41 | private readonly SearchHelper $faqSearchHelper, |
| 42 | private readonly Plurals $plurals, |
| 43 | ) { |
| 44 | parent::__construct(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @throws \Exception |
| 49 | */ |
| 50 | #[Route(path: 'autocomplete', name: 'api.private.autocomplete', methods: ['GET'])] |
| 51 | public function search(Request $request): JsonResponse |
| 52 | { |
| 53 | $searchString = Filter::filterVar($request->query->get(key: 'search'), FILTER_SANITIZE_SPECIAL_CHARS); |
| 54 | |
| 55 | if (is_null($searchString) || $searchString === '' || $searchString === '0') { |
| 56 | return $this->json([], Response::HTTP_NOT_FOUND); |
| 57 | } |
| 58 | |
| 59 | [$currentUser, $currentGroups] = CurrentUser::getCurrentUserGroupId($this->currentUser); |
| 60 | |
| 61 | $category = new Category($this->configuration, $currentGroups); |
| 62 | $category->setUser($currentUser); |
| 63 | $category->setGroups($currentGroups); |
| 64 | $category->transform(categoryId: 0); |
| 65 | $category->buildCategoryTree(); |
| 66 | |
| 67 | $searchResultSet = new SearchResultSet($this->currentUser, $this->faqPermission, $this->configuration); |
| 68 | |
| 69 | $this->faqSearch->setCategory($category); |
| 70 | |
| 71 | $searchResult = $this->faqSearch->autoComplete($searchString); |
| 72 | |
| 73 | $searchResultSet->reviewResultSet($searchResult); |
| 74 | |
| 75 | $this->faqSearchHelper->setSearchTerm($searchString); |
| 76 | $this->faqSearchHelper->setCategory($category); |
| 77 | $this->faqSearchHelper->setPlurals($this->plurals); |
| 78 | |
| 79 | return $this->json($this->faqSearchHelper->createAutoCompleteResult($searchResultSet), Response::HTTP_OK); |
| 80 | } |
| 81 | } |