Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CommentsController
100.00% covered (success)
100.00%
36 / 36
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
 index
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * The Administration comments 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-12-01
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Administration;
21
22use phpMyFAQ\Comments;
23use phpMyFAQ\Core\Exception;
24use phpMyFAQ\Entity\CommentType;
25use phpMyFAQ\Enums\PermissionType;
26use phpMyFAQ\Filter;
27use phpMyFAQ\News;
28use phpMyFAQ\Pagination;
29use phpMyFAQ\Pagination\UrlConfig;
30use phpMyFAQ\Session\Token;
31use phpMyFAQ\Twig\Extensions\FaqTwigExtension;
32use phpMyFAQ\Twig\Extensions\TitleSlugifierTwigExtension;
33use Symfony\Component\HttpFoundation\Request;
34use Symfony\Component\HttpFoundation\Response;
35use Symfony\Component\Routing\Attribute\Route;
36use Twig\Error\LoaderError;
37use Twig\Extension\AttributeExtension;
38use Twig\Extra\Intl\IntlExtension;
39
40final class CommentsController extends AbstractAdministrationController
41{
42    public function __construct(
43        private readonly Comments $comments,
44        private readonly News $news,
45    ) {
46        parent::__construct();
47    }
48
49    /**
50     * @throws LoaderError
51     * @throws Exception
52     * @throws \Exception
53     */
54    #[Route(path: '/comments', name: 'admin.comments', methods: ['GET'])]
55    public function index(Request $request): Response
56    {
57        $this->userHasPermission(PermissionType::COMMENT_DELETE);
58
59        $page = Filter::filterVar($request->query->get(key: 'page'), FILTER_VALIDATE_INT, 1);
60        $page = max(1, $page);
61
62        $itemsPerPage = 10;
63        $allFaqComments = $this->comments->getAllComments();
64        $allNewsComments = $this->comments->getAllComments(CommentType::NEWS);
65
66        $faqComments = array_slice($allFaqComments, ($page - 1) * $itemsPerPage, $itemsPerPage);
67        $newsComments = array_slice($allNewsComments, ($page - 1) * $itemsPerPage, $itemsPerPage);
68
69        $newsHeader = $this->news->getHeader();
70
71        $faqCommentsPagination = new Pagination(
72            baseUrl: $request->getUri(),
73            total: is_countable($allFaqComments) ? count($allFaqComments) : 0,
74            perPage: $itemsPerPage,
75            urlConfig: new UrlConfig(pageParamName: 'page'),
76        );
77
78        $newsCommentsPagination = new Pagination(
79            baseUrl: $request->getUri(),
80            total: is_countable($allNewsComments) ? count($allNewsComments) : 0,
81            perPage: $itemsPerPage,
82            urlConfig: new UrlConfig(pageParamName: 'page'),
83        );
84
85        $this->addExtension(new IntlExtension());
86        $this->addExtension(new AttributeExtension(FaqTwigExtension::class));
87        $this->addExtension(new AttributeExtension(TitleSlugifierTwigExtension::class));
88        return $this->render(file: '@admin/content/comments.twig', context: [
89            ...$this->getHeader($request),
90            ...$this->getFooter(),
91            'currentLocale' => $this->configuration->getLanguage()->getLanguage(),
92            'faqComments' => $faqComments,
93            'newsComments' => $newsComments,
94            'faqCommentsPagination' => $faqCommentsPagination->render(),
95            'newsCommentsPagination' => $newsCommentsPagination->render(),
96            'newsHeader' => $newsHeader,
97            'csrfToken' => Token::getInstance($this->session)->getTokenString(page: 'delete-comment'),
98        ]);
99    }
100}