Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
StatisticsSearchController
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
2 / 2
3
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
 index
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * The Search statistics Administration 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 2024-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     2024-11-30
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Administration;
21
22use phpMyFAQ\Core\Exception;
23use phpMyFAQ\Enums\PermissionType;
24use phpMyFAQ\Pagination;
25use phpMyFAQ\Pagination\UrlConfig;
26use phpMyFAQ\Search;
27use phpMyFAQ\Session\Token;
28use phpMyFAQ\Translation;
29use phpMyFAQ\Twig\Extensions\LanguageCodeTwigExtension;
30use Symfony\Component\HttpFoundation\Request;
31use Symfony\Component\HttpFoundation\Response;
32use Symfony\Component\Routing\Attribute\Route;
33use Twig\Error\LoaderError;
34use Twig\Extension\AttributeExtension;
35
36final class StatisticsSearchController extends AbstractAdministrationController
37{
38    public function __construct(
39        private readonly Search $search,
40    ) {
41        parent::__construct();
42    }
43
44    /**
45     * @throws LoaderError
46     * @throws Exception
47     * @throws \Exception
48     */
49    #[Route(path: '/statistics/search', name: 'admin.statistics.search', methods: ['GET'])]
50    public function index(Request $request): Response
51    {
52        $this->userHasPermission(PermissionType::STATISTICS_VIEWLOGS);
53        $this->addExtension(new AttributeExtension(LanguageCodeTwigExtension::class));
54
55        $perPage = 10;
56
57        $search = $this->search;
58
59        $searchesCount = $search->getSearchesCount();
60        $searchesList = $search->getMostPopularSearches($searchesCount + 1, true);
61
62        $pagination = new Pagination(
63            baseUrl: $request->getUri(),
64            total: is_countable($searchesList) ? count($searchesList) : 0,
65            perPage: $perPage,
66            urlConfig: new UrlConfig(pageParamName: 'page'),
67        );
68
69        return $this->render('@admin/statistics/search.twig', [
70            ...$this->getHeader($request),
71            ...$this->getFooter(),
72            'msgAdminElasticsearchStats' => Translation::get(key: 'msgAdminElasticsearchStats'),
73            'csrfToken' => Token::getInstance($this->session)->getTokenString('truncate-search-terms'),
74            'ad_searchterm_del' => Translation::get(key: 'ad_searchterm_del'),
75            'ad_searchstats_search_term' => Translation::get(key: 'ad_searchstats_search_term'),
76            'ad_searchstats_search_term_count' => Translation::get(key: 'ad_searchstats_search_term_count'),
77            'ad_searchstats_search_term_lang' => Translation::get(key: 'ad_searchstats_search_term_lang'),
78            'ad_searchstats_search_term_percentage' => Translation::get(key: 'ad_searchstats_search_term_percentage'),
79            'pagination' => $pagination->render(),
80            'searchesCount' => $searchesCount,
81            'searchesList' => $searchesList,
82            'csrfTokenDelete' => Token::getInstance($this->session)->getTokenString('delete-searchterm'),
83            'ad_news_delete' => Translation::get(key: 'ad_news_delete'),
84        ]);
85    }
86}