Lines 100.00% 10 / 10
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 buildWhereClause 100.00% 1 / 1 100.00% 1 / 1 1
 buildWhereClauseWithInactive 100.00% 1 / 1 100.00% 1 / 1 1
 buildPermissionWhereClause 100.00% 8 / 8 100.00% 1 / 1 2
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}