Lines 100.00% 19 / 19
Methods 100.00% 10 / 10
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 2 / 2 100.00% 1 / 1 1
 getUser 100.00% 1 / 1 100.00% 1 / 1 1
 setUser 100.00% 1 / 1 100.00% 1 / 1 1
 getGroups 100.00% 1 / 1 100.00% 1 / 1 1
 setGroups 100.00% 4 / 4 100.00% 1 / 1 3
 setOwner 100.00% 1 / 1 100.00% 1 / 1 1
 getOwner 100.00% 3 / 3 100.00% 1 / 1 2
 setModerator 100.00% 1 / 1 100.00% 1 / 1 1
 getModeratorGroupId 100.00% 1 / 1 100.00% 1 / 1 1
 clear 100.00% 4 / 4 100.00% 1 / 1 1
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}