Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
45 / 54
83.78% covered (warning)
83.78%
31 / 37
CRAP
0.00% covered (danger)
0.00%
0 / 1
Role
83.33% covered (warning)
83.33%
45 / 54
83.78% covered (warning)
83.78%
31 / 37
60.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOldUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOldUsername
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDescription
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGuide
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setGuide
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPermissions
n/a
0 / 0
n/a
0 / 0
1
 setPermissions
n/a
0 / 0
n/a
0 / 0
1
 getPermissionMap
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPermissionMap
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParentRoleId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setParentRoleId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUsers
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPositionWithinParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPositionWithinParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFeaturedPosition
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFeaturedPosition
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCanHaveChildRoles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCanHaveChildRoles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 testOnlyGetField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntityNameForStorage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntityIdForStorage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPositionFieldName
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 getPositionForEntityField
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
5.02
 getIdFieldNameForSearch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIdForSearch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTitleForSearch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCriteriaForFilter
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
5.20
 getCriteriaForQuery
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Olz\Entity\Roles;
4
5use Doctrine\Common\Collections\ArrayCollection;
6use Doctrine\Common\Collections\Collection;
7use Doctrine\Common\Collections\Criteria;
8use Doctrine\Common\Collections\Expr\Expression;
9use Doctrine\ORM\Mapping as ORM;
10use Olz\Entity\Common\DataStorageInterface;
11use Olz\Entity\Common\DataStorageTrait;
12use Olz\Entity\Common\OlzEntity;
13use Olz\Entity\Common\PositionableInterface;
14use Olz\Entity\Common\SearchableInterface;
15use Olz\Entity\Common\TestableInterface;
16use Olz\Entity\Users\User;
17use Olz\Repository\Roles\RoleRepository;
18
19#[ORM\Table(name: 'roles')]
20#[ORM\Entity(repositoryClass: RoleRepository::class)]
21class Role extends OlzEntity implements DataStorageInterface, PositionableInterface, SearchableInterface, TestableInterface {
22    use DataStorageTrait;
23
24    #[ORM\Id]
25    #[ORM\Column(type: 'integer', nullable: false)]
26    #[ORM\GeneratedValue]
27    public int $id;
28
29    #[ORM\Column(type: 'text', nullable: false)]
30    public string $username;
31
32    #[ORM\Column(type: 'text', nullable: true)]
33    public ?string $old_username;
34
35    #[ORM\Column(type: 'text', nullable: false)]
36    public string $name;
37
38    #[ORM\Column(type: 'text', nullable: false, options: ['comment' => 'public'])]
39    public string $description;
40
41    #[ORM\Column(type: 'text', nullable: false)]
42    public string $permissions;
43
44    #[ORM\Column(type: 'text', nullable: false, options: ['comment' => 'restricted access'])]
45    public string $guide;
46
47    #[ORM\Column(type: 'integer', nullable: true)]
48    public ?int $parent_role;
49
50    #[ORM\Column(type: 'smallfloat', nullable: true, options: ['comment' => 'null: hide role'])]
51    public ?float $position_within_parent;
52
53    #[ORM\Column(type: 'smallfloat', nullable: true, options: ['comment' => 'null: not featured'])]
54    public ?float $featured_position;
55
56    #[ORM\Column(type: 'boolean', options: ['default' => 0])]
57    public bool $can_have_child_roles;
58
59    /** @var Collection<int|string, User>&iterable<User> */
60    #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'roles')]
61    private Collection $users;
62
63    public function __construct() {
64        $this->users = new ArrayCollection();
65    }
66
67    public function getId(): ?int {
68        return $this->id ?? null;
69    }
70
71    public function setId(int $new_id): void {
72        $this->id = $new_id;
73    }
74
75    public function getUsername(): string {
76        return $this->username;
77    }
78
79    public function setUsername(string $new_username): void {
80        $this->username = $new_username;
81    }
82
83    public function getOldUsername(): ?string {
84        return $this->old_username;
85    }
86
87    public function setOldUsername(?string $new_old_username): void {
88        $this->old_username = $new_old_username;
89    }
90
91    public function getName(): string {
92        return $this->name;
93    }
94
95    public function setName(string $new_name): void {
96        $this->name = $new_name;
97    }
98
99    public function getDescription(): string {
100        return $this->description;
101    }
102
103    public function setDescription(string $new_value): void {
104        $this->description = $new_value;
105    }
106
107    public function getGuide(): string {
108        return $this->guide;
109    }
110
111    public function setGuide(string $new_value): void {
112        $this->guide = $new_value;
113    }
114
115    /** @deprecated Use `getPermissionMap` instead. */
116    public function getPermissions(): string {
117        return $this->permissions;
118    }
119
120    /** @deprecated Use `setPermissionMap` instead. */
121    public function setPermissions(string $new_value): void {
122        $this->permissions = $new_value;
123    }
124
125    /** @return array<string, true> */
126    public function getPermissionMap(): array {
127        return $this->generalUtils()->deserializeTokenBitMap($this->permissions ?? '');
128    }
129
130    /** @param array<string, bool> $new_value */
131    public function setPermissionMap(array $new_value): void {
132        $this->permissions = $this->generalUtils()->serializeTokenBitMap($new_value);
133    }
134
135    public function getParentRoleId(): ?int {
136        return $this->parent_role;
137    }
138
139    public function setParentRoleId(?int $new_value): void {
140        $this->parent_role = $new_value;
141    }
142
143    /** @return Collection<int|string, User>&iterable<User> */
144    public function getUsers(): Collection {
145        return $this->users;
146    }
147
148    public function addUser(User $user): void {
149        $this->users->add($user);
150    }
151
152    public function removeUser(User $user): void {
153        $this->users->removeElement($user);
154    }
155
156    public function getPositionWithinParent(): ?float {
157        return $this->position_within_parent;
158    }
159
160    public function setPositionWithinParent(?float $new_value): void {
161        $this->position_within_parent = $new_value;
162    }
163
164    public function getFeaturedPosition(): ?float {
165        return $this->featured_position;
166    }
167
168    public function setFeaturedPosition(?float $new_value): void {
169        $this->featured_position = $new_value;
170    }
171
172    public function getCanHaveChildRoles(): bool {
173        return $this->can_have_child_roles;
174    }
175
176    public function setCanHaveChildRoles(bool $new_value): void {
177        $this->can_have_child_roles = $new_value;
178    }
179
180    // ---
181
182    public function __toString() {
183        $username = $this->getUsername();
184        $id = $this->getId();
185        return "{$username} (Role ID: {$id})";
186    }
187
188    public function testOnlyGetField(string $field_name): mixed {
189        return $this->{$field_name};
190    }
191
192    public static function getEntityNameForStorage(): string {
193        return 'roles';
194    }
195
196    public function getEntityIdForStorage(): string {
197        return "{$this->getId()}";
198    }
199
200    public static function getPositionFieldName(string $field): string {
201        switch ($field) {
202            case 'positionWithinParent':
203                return 'position_within_parent';
204            case 'featuredPosition':
205                return 'featured_position';
206            default: throw new \Exception("No such position field: {$field}");
207        }
208    }
209
210    public function getPositionForEntityField(string $field): ?float {
211        switch ($field) {
212            case 'positionWithinParent':
213                return $this->getPositionWithinParent();
214            case 'featuredPosition':
215                return $this->getFeaturedPosition();
216            default: throw new \Exception("No such position field: {$field}");
217        }
218    }
219
220    public static function getIdFieldNameForSearch(): string {
221        return 'id';
222    }
223
224    public function getIdForSearch(): int {
225        return $this->getId() ?? 0;
226    }
227
228    public function getTitleForSearch(): string {
229        return $this->getName();
230    }
231
232    public static function getCriteriaForFilter(string $key, string $value): Expression {
233        switch ($key) {
234            case 'parentRoleId':
235                return Criteria::expr()->eq('parent_role', intval($value) ?: null);
236            case 'featuredPositionNotNull':
237                return Criteria::expr()->isNotNull('featured_position');
238            default: throw new \Exception("No such Role filter: {$key}");
239        }
240    }
241
242    public static function getCriteriaForQuery(string $query): Expression {
243        return Criteria::expr()->orX(
244            Criteria::expr()->contains('name', $query),
245            Criteria::expr()->contains('username', $query),
246        );
247    }
248}