Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
CategoryPermissionContext
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
10 / 10
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGroups
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setGroups
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 setOwner
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOwner
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setModerator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getModeratorGroupId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clear
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Permission context for categories.
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-20
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Category;
21
22/**
23 * Manages permission-related data for categories.
24 * Consolidates user, group, owner, and moderator information.
25 */
26final class CategoryPermissionContext
27{
28    /**
29     * User ID.
30     */
31    private int $user = -1;
32
33    /**
34     * Groups.
35     * @var int[]
36     */
37    private array $groups = [-1];
38
39    /**
40     * Entity owners
41     * @var array<int, int>
42     */
43    private array $owner = [];
44
45    /**
46     * Entity moderators
47     * @var array<int, int>
48     */
49    private array $moderators = [];
50
51    public function __construct(array $groups = [], int $user = -1)
52    {
53        $this->setGroups($groups);
54        $this->setUser($user);
55    }
56
57    public function getUser(): int
58    {
59        return $this->user;
60    }
61
62    public function setUser(int $user): void
63    {
64        $this->user = $user;
65    }
66
67    /**
68     * @return int[]
69     */
70    public function getGroups(): array
71    {
72        return $this->groups;
73    }
74
75    /**
76     * @param array<array-key, mixed> $groups
77     */
78    public function setGroups(array $groups): void
79    {
80        $normalizedGroups = [];
81        foreach ($groups as $group) {
82            $normalizedGroups[] = (int) $group;
83        }
84
85        $this->groups = $normalizedGroups === [] ? [-1] : $normalizedGroups;
86    }
87
88    public function setOwner(int $categoryId, int $userId): void
89    {
90        $this->owner[$categoryId] = $userId;
91    }
92
93    public function getOwner(?int $categoryId = null): int
94    {
95        if ($categoryId === null) {
96            return 1;
97        }
98
99        return $this->owner[$categoryId] ?? 1;
100    }
101
102    public function setModerator(int $categoryId, int $groupId): void
103    {
104        $this->moderators[$categoryId] = $groupId;
105    }
106
107    public function getModeratorGroupId(int $categoryId): int
108    {
109        return $this->moderators[$categoryId] ?? 0;
110    }
111
112    /**
113     * Clears all permission data.
114     */
115    public function clear(): void
116    {
117        $this->user = -1;
118        $this->groups = [-1];
119        $this->owner = [];
120        $this->moderators = [];
121    }
122}