Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
RoleEndpointTrait | |
0.00% |
0 / 37 |
|
0.00% |
0 / 7 |
182 | |
0.00% |
0 / 1 |
getEntityData | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
updateEntityWithData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
updateEntityWithNonParentData | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
persistUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
editUploads | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getEntityById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getParentRoleId | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace Olz\Roles\Endpoints; |
4 | |
5 | use Olz\Entity\Roles\Role; |
6 | use Olz\Utils\WithUtilsTrait; |
7 | use PhpTypeScriptApi\HttpError; |
8 | |
9 | /** |
10 | * @phpstan-type OlzRoleId int |
11 | * @phpstan-type OlzRoleData array{ |
12 | * username: non-empty-string, |
13 | * name: non-empty-string, |
14 | * description: string, |
15 | * guide: string, |
16 | * imageIds: array<non-empty-string>, |
17 | * fileIds: array<non-empty-string>, |
18 | * parentRole?: ?int<1, max>, |
19 | * indexWithinParent?: ?int<0, max>, |
20 | * featuredIndex?: ?int, |
21 | * canHaveChildRoles: bool, |
22 | * } |
23 | */ |
24 | trait RoleEndpointTrait { |
25 | use WithUtilsTrait; |
26 | |
27 | /** @return OlzRoleData */ |
28 | public function getEntityData(Role $entity): array { |
29 | return [ |
30 | 'username' => $entity->getUsername() ? $entity->getUsername() : '-', |
31 | 'name' => $entity->getName() ? $entity->getName() : '-', |
32 | 'description' => $entity->getDescription(), |
33 | 'guide' => $entity->getGuide(), |
34 | 'imageIds' => $entity->getStoredImageUploadIds(), |
35 | 'fileIds' => $entity->getStoredFileUploadIds(), |
36 | 'parentRole' => $this->getParentRoleId($entity), |
37 | 'indexWithinParent' => ($entity->getIndexWithinParent() ?? -1) < 0 ? null : $entity->getIndexWithinParent(), |
38 | 'featuredIndex' => $entity->getFeaturedIndex() ?? null, |
39 | 'canHaveChildRoles' => $entity->getCanHaveChildRoles(), |
40 | ]; |
41 | } |
42 | |
43 | /** @param OlzRoleData $input_data */ |
44 | public function updateEntityWithData(Role $entity, array $input_data): void { |
45 | $this->updateEntityWithNonParentData($entity, $input_data); |
46 | $entity->setParentRoleId($input_data['parentRole'] ?? null); |
47 | $entity->setIndexWithinParent($input_data['indexWithinParent'] ?? null); |
48 | $entity->setFeaturedIndex($input_data['featuredIndex'] ?? null); |
49 | $entity->setCanHaveChildRoles($input_data['canHaveChildRoles']); |
50 | } |
51 | |
52 | /** @param OlzRoleData $input_data */ |
53 | public function updateEntityWithNonParentData(Role $entity, array $input_data): void { |
54 | $entity->setUsername($input_data['username']); |
55 | $entity->setName($input_data['name']); |
56 | $entity->setDescription($input_data['description']); |
57 | $entity->setGuide($input_data['guide']); |
58 | } |
59 | |
60 | /** @param OlzRoleData $input_data */ |
61 | public function persistUploads(Role $entity, array $input_data): void { |
62 | $this->persistOlzImages($entity, $input_data['imageIds']); |
63 | $this->persistOlzFiles($entity, $input_data['fileIds']); |
64 | } |
65 | |
66 | public function editUploads(Role $entity): void { |
67 | $image_ids = $this->uploadUtils()->getStoredUploadIds("{$entity->getImagesPathForStorage()}img/"); |
68 | $this->editOlzImages($entity, $image_ids); |
69 | $this->editOlzFiles($entity); |
70 | } |
71 | |
72 | protected function getEntityById(int $id): Role { |
73 | $repo = $this->entityManager()->getRepository(Role::class); |
74 | $entity = $repo->findOneBy(['id' => $id]); |
75 | if (!$entity) { |
76 | throw new HttpError(404, "Nicht gefunden."); |
77 | } |
78 | return $entity; |
79 | } |
80 | |
81 | // --- |
82 | |
83 | /** @return ?int<1, max> */ |
84 | protected function getParentRoleId(Role $entity): ?int { |
85 | $number = $entity->getParentRoleId(); |
86 | if ($number === null) { |
87 | return null; |
88 | } |
89 | if ($number < 1) { |
90 | throw new \Exception("Invalid parent role ID: {$number} ({$entity})"); |
91 | } |
92 | return $number; |
93 | } |
94 | } |