Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 37 |
CRAP | |
0.00% |
0 / 1 |
| Role | |
0.00% |
0 / 54 |
|
0.00% |
0 / 37 |
2256 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getUsername | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setUsername | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOldUsername | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setOldUsername | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGuide | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setGuide | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPermissions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setPermissions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParentRoleId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setParentRoleId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getUsers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPositionWithinParent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setPositionWithinParent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFeaturedPosition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setFeaturedPosition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCanHaveChildRoles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setCanHaveChildRoles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __toString | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| testOnlyGetField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getEntityNameForStorage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getEntityIdForStorage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPositionFieldName | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| getPositionForEntityField | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| getIdFieldNameForSearch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIdForSearch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTitleForSearch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCriteriaForFilter | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| getCriteriaForQuery | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Entity\Roles; |
| 4 | |
| 5 | use Doctrine\Common\Collections\ArrayCollection; |
| 6 | use Doctrine\Common\Collections\Collection; |
| 7 | use Doctrine\Common\Collections\Criteria; |
| 8 | use Doctrine\Common\Collections\Expr\Expression; |
| 9 | use Doctrine\ORM\Mapping as ORM; |
| 10 | use Olz\Entity\Common\DataStorageInterface; |
| 11 | use Olz\Entity\Common\DataStorageTrait; |
| 12 | use Olz\Entity\Common\OlzEntity; |
| 13 | use Olz\Entity\Common\PositionableInterface; |
| 14 | use Olz\Entity\Common\SearchableInterface; |
| 15 | use Olz\Entity\Common\TestableInterface; |
| 16 | use Olz\Entity\Users\User; |
| 17 | use Olz\Repository\Roles\RoleRepository; |
| 18 | |
| 19 | #[ORM\Table(name: 'roles')] |
| 20 | #[ORM\Entity(repositoryClass: RoleRepository::class)] |
| 21 | class 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 | public function getPermissions(): string { |
| 116 | return $this->permissions; |
| 117 | } |
| 118 | |
| 119 | public function setPermissions(string $new_value): void { |
| 120 | $this->permissions = $new_value; |
| 121 | } |
| 122 | |
| 123 | public function getParentRoleId(): ?int { |
| 124 | return $this->parent_role; |
| 125 | } |
| 126 | |
| 127 | public function setParentRoleId(?int $new_value): void { |
| 128 | $this->parent_role = $new_value; |
| 129 | } |
| 130 | |
| 131 | /** @return Collection<int|string, User>&iterable<User> */ |
| 132 | public function getUsers(): Collection { |
| 133 | return $this->users; |
| 134 | } |
| 135 | |
| 136 | public function addUser(User $user): void { |
| 137 | $this->users->add($user); |
| 138 | } |
| 139 | |
| 140 | public function removeUser(User $user): void { |
| 141 | $this->users->removeElement($user); |
| 142 | } |
| 143 | |
| 144 | public function getPositionWithinParent(): ?float { |
| 145 | return $this->position_within_parent; |
| 146 | } |
| 147 | |
| 148 | public function setPositionWithinParent(?float $new_value): void { |
| 149 | $this->position_within_parent = $new_value; |
| 150 | } |
| 151 | |
| 152 | public function getFeaturedPosition(): ?float { |
| 153 | return $this->featured_position; |
| 154 | } |
| 155 | |
| 156 | public function setFeaturedPosition(?float $new_value): void { |
| 157 | $this->featured_position = $new_value; |
| 158 | } |
| 159 | |
| 160 | public function getCanHaveChildRoles(): bool { |
| 161 | return $this->can_have_child_roles; |
| 162 | } |
| 163 | |
| 164 | public function setCanHaveChildRoles(bool $new_value): void { |
| 165 | $this->can_have_child_roles = $new_value; |
| 166 | } |
| 167 | |
| 168 | // --- |
| 169 | |
| 170 | public function __toString() { |
| 171 | $username = $this->getUsername(); |
| 172 | $id = $this->getId(); |
| 173 | return "{$username} (Role ID: {$id})"; |
| 174 | } |
| 175 | |
| 176 | public function testOnlyGetField(string $field_name): mixed { |
| 177 | return $this->{$field_name}; |
| 178 | } |
| 179 | |
| 180 | public static function getEntityNameForStorage(): string { |
| 181 | return 'roles'; |
| 182 | } |
| 183 | |
| 184 | public function getEntityIdForStorage(): string { |
| 185 | return "{$this->getId()}"; |
| 186 | } |
| 187 | |
| 188 | public static function getPositionFieldName(string $field): string { |
| 189 | switch ($field) { |
| 190 | case 'positionWithinParent': |
| 191 | return 'position_within_parent'; |
| 192 | case 'featuredPosition': |
| 193 | return 'featured_position'; |
| 194 | default: throw new \Exception("No such position field: {$field}"); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | public function getPositionForEntityField(string $field): ?float { |
| 199 | switch ($field) { |
| 200 | case 'positionWithinParent': |
| 201 | return $this->getPositionWithinParent(); |
| 202 | case 'featuredPosition': |
| 203 | return $this->getFeaturedPosition(); |
| 204 | default: throw new \Exception("No such position field: {$field}"); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public static function getIdFieldNameForSearch(): string { |
| 209 | return 'id'; |
| 210 | } |
| 211 | |
| 212 | public function getIdForSearch(): int { |
| 213 | return $this->getId() ?? 0; |
| 214 | } |
| 215 | |
| 216 | public function getTitleForSearch(): string { |
| 217 | return $this->getName(); |
| 218 | } |
| 219 | |
| 220 | public static function getCriteriaForFilter(string $key, string $value): Expression { |
| 221 | switch ($key) { |
| 222 | case 'parentRoleId': |
| 223 | return Criteria::expr()->eq('parent_role', intval($value) ?: null); |
| 224 | case 'featuredPositionNotNull': |
| 225 | return Criteria::expr()->isNotNull('featured_position'); |
| 226 | default: throw new \Exception("No such Role filter: {$key}"); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | public static function getCriteriaForQuery(string $query): Expression { |
| 231 | return Criteria::expr()->orX( |
| 232 | Criteria::expr()->contains('name', $query), |
| 233 | Criteria::expr()->contains('username', $query), |
| 234 | ); |
| 235 | } |
| 236 | } |