Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AutoCompleteController
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 search
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
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
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Frontend\Api;
21
22use phpMyFAQ\Category;
23use phpMyFAQ\Controller\AbstractController;
24use phpMyFAQ\Faq\Permission;
25use phpMyFAQ\Filter;
26use phpMyFAQ\Helper\SearchHelper;
27use phpMyFAQ\Language\Plurals;
28use phpMyFAQ\Search;
29use phpMyFAQ\Search\SearchResultSet;
30use phpMyFAQ\User\CurrentUser;
31use Symfony\Component\HttpFoundation\JsonResponse;
32use Symfony\Component\HttpFoundation\Request;
33use Symfony\Component\HttpFoundation\Response;
34use Symfony\Component\Routing\Attribute\Route;
35
36final 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}