Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
GlossaryController
100.00% covered (success)
100.00%
28 / 28
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%
27 / 27
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Glossary Controller
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public License,
9 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
10 * obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * @package   phpMyFAQ
13 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
14 * @copyright 2012-2026 phpMyFAQ Team
15 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
16 * @link      https://www.phpmyfaq.de
17 * @since     2012-09-03
18 */
19
20namespace phpMyFAQ\Controller\Frontend;
21
22use phpMyFAQ\Core\Exception;
23use phpMyFAQ\Filter;
24use phpMyFAQ\Glossary;
25use phpMyFAQ\Pagination;
26use phpMyFAQ\Pagination\UrlConfig;
27use phpMyFAQ\Translation;
28use phpMyFAQ\User\UserSession;
29use Symfony\Component\HttpFoundation\Request;
30use Symfony\Component\HttpFoundation\Response;
31use Symfony\Component\Routing\Attribute\Route;
32use Twig\Error\LoaderError;
33
34final class GlossaryController extends AbstractFrontController
35{
36    public function __construct(
37        private readonly UserSession $faqSession,
38        private readonly Glossary $glossary,
39    ) {
40        parent::__construct();
41    }
42
43    /**
44     * @throws Exception
45     * @throws LoaderError
46     * @throws \Exception
47     */
48    #[Route(path: '/glossary.html', name: 'public.glossary', methods: ['GET'])]
49    public function index(Request $request): Response
50    {
51        $this->faqSession->setCurrentUser($this->currentUser);
52        $this->faqSession->userTracking('glossary', 0);
53
54        $page = Filter::filterVar($request->query->get('page'), FILTER_VALIDATE_INT, 1);
55
56        $glossaryItems = $this->glossary->fetchAll();
57
58        $itemsPerPage = 8;
59        $baseUrl = sprintf('%sglossary.html?page=%d', $this->configuration->getDefaultUrl(), $page);
60
61        $pagination = new Pagination(
62            baseUrl: $baseUrl,
63            total: is_countable($glossaryItems) ? count($glossaryItems) : 0,
64            perPage: $itemsPerPage,
65            urlConfig: new UrlConfig(pageParamName: 'page'),
66        );
67
68        return $this->render('glossary.twig', [
69            ...$this->getHeader($request),
70            'title' => sprintf(
71                '%s - %s',
72                Translation::getString(key: 'ad_menu_glossary'),
73                $this->configuration->getTitle(),
74            ),
75            'metaDescription' => sprintf(
76                Translation::getString(key: 'msgGlossaryMetaDesc'),
77                $this->configuration->getTitle(),
78            ),
79            'pageHeader' => Translation::get(key: 'ad_menu_glossary'),
80            'glossaryItems' => array_slice($glossaryItems, ($page - 1) * $itemsPerPage, $itemsPerPage),
81            'pagination' => $pagination->render(),
82        ]);
83    }
84}