Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| SearchController | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |
0.00% |
0 / 1 |
| deleteTerm | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The Admin Search 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-10-26 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Controller\Administration\Api; |
| 21 | |
| 22 | use phpMyFAQ\Controller\AbstractController; |
| 23 | use phpMyFAQ\Enums\PermissionType; |
| 24 | use phpMyFAQ\Filter; |
| 25 | use phpMyFAQ\Search; |
| 26 | use phpMyFAQ\Session\Token; |
| 27 | use phpMyFAQ\Translation; |
| 28 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 29 | use Symfony\Component\HttpFoundation\Request; |
| 30 | use Symfony\Component\HttpFoundation\Response; |
| 31 | use Symfony\Component\Routing\Attribute\Route; |
| 32 | |
| 33 | final class SearchController extends AbstractController |
| 34 | { |
| 35 | /** |
| 36 | * @throws \Exception |
| 37 | */ |
| 38 | #[Route(path: 'search/term', name: 'admin.api.search.term', methods: ['DELETE'])] |
| 39 | public function deleteTerm(Request $request): JsonResponse |
| 40 | { |
| 41 | $this->userHasPermission(PermissionType::STATISTICS_VIEWLOGS); |
| 42 | |
| 43 | $deleteData = $this->getJsonObject($request); |
| 44 | |
| 45 | $search = new Search($this->configuration); |
| 46 | |
| 47 | if (!Token::getInstance($this->session)->verifyToken('delete-searchterm', (string) ($deleteData->csrf ?? ''))) { |
| 48 | return $this->json(['error' => Translation::get(key: 'msgNoPermission')], Response::HTTP_UNAUTHORIZED); |
| 49 | } |
| 50 | |
| 51 | $searchId = Filter::filterVar($deleteData->searchTermId ?? null, FILTER_VALIDATE_INT); |
| 52 | |
| 53 | if (!is_int($searchId)) { |
| 54 | return $this->json(['error' => false], Response::HTTP_BAD_REQUEST); |
| 55 | } |
| 56 | |
| 57 | if ($search->deleteSearchTermById($searchId)) { |
| 58 | return $this->json(['deleted' => $searchId], Response::HTTP_OK); |
| 59 | } |
| 60 | |
| 61 | return $this->json(['error' => $searchId], Response::HTTP_BAD_REQUEST); |
| 62 | } |
| 63 | } |