Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
PopularSearchesController
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
4
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
 popular
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * The Popular Searches 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 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     2026-06-20
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Frontend\Api;
21
22use phpMyFAQ\Controller\AbstractController;
23use phpMyFAQ\Search;
24use Symfony\Component\HttpFoundation\JsonResponse;
25use Symfony\Component\HttpFoundation\Response;
26use Symfony\Component\Routing\Attribute\Route;
27
28final class PopularSearchesController extends AbstractController
29{
30    public function __construct(
31        private readonly Search $faqSearch,
32    ) {
33        parent::__construct();
34    }
35
36    /**
37     * @throws \Exception
38     */
39    #[Route(path: 'searches/popular', name: 'api.private.searches.popular', methods: ['GET'])]
40    public function popular(): JsonResponse
41    {
42        $numberOfResults = (int) $this->configuration->get('search.numberSearchTerms');
43        if ($numberOfResults <= 0) {
44            $numberOfResults = 7;
45        }
46
47        // Aggregate across all languages (no withLang), consistent with the existing
48        // frontend "most popular searches" display in SearchController/search.twig.
49        $result = $this->faqSearch->getMostPopularSearches($numberOfResults);
50
51        if ($result === []) {
52            return $this->json([], Response::HTTP_NOT_FOUND);
53        }
54
55        return $this->json($result, Response::HTTP_OK);
56    }
57}