Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CategoryPermissionService
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 buildWhereClause
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildWhereClauseWithInactive
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildPermissionWhereClause
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Category permission service class
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 2025 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     2025-10-18
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Category\Permission;
21
22final class CategoryPermissionService
23{
24    /**
25     * Builds the permission-related WHERE clause for category listing (active categories only).
26     *
27     * @param int[] $groups
28     */
29    public function buildWhereClause(array $groups, int $userId): string
30    {
31        return $this->buildPermissionWhereClause($groups, $userId, activeClause: 'AND fc.active = 1');
32    }
33
34    /**
35     * Builds the permission-related WHERE clause for category listing (including inactive categories).
36     *
37     * @param int[] $groups
38     */
39    public function buildWhereClauseWithInactive(array $groups, int $userId): string
40    {
41        return $this->buildPermissionWhereClause($groups, $userId, activeClause: '');
42    }
43
44    /**
45     * Internal helper to build the WHERE clause with permission logic.
46     *
47     * @param int[] $groups
48     */
49    private function buildPermissionWhereClause(array $groups, int $userId, string $activeClause): string
50    {
51        $groupsList = $groups === []
52            ? '-1'
53            : implode(separator: ', ', array: array_map(callback: intval(...), array: $groups));
54        return strtr('WHERE ( fg.group_id IN ({groups}) OR (fu.user_id = {userId} AND fg.group_id IN ({groups}))) {active}', [
55            '{groups}' => $groupsList,
56            '{userId}' => (string) $userId,
57            '{active}' => $activeClause,
58        ]);
59    }
60}