Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.30% |
36 / 37 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| RoleEndpointTrait | |
97.30% |
36 / 37 |
|
85.71% |
6 / 7 |
12 | |
0.00% |
0 / 1 |
| getEntityData | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| updateEntityWithData | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| updateEntityWithNonParentData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| persistUploads | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| editUploads | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getEntityById | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getParentRoleId | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| 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 | * positionWithinParent?: ?float, |
| 20 | * featuredPosition?: ?float, |
| 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 | 'positionWithinParent' => $entity->getPositionWithinParent(), |
| 38 | 'featuredPosition' => $entity->getFeaturedPosition() ?? 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->setPositionWithinParent($input_data['positionWithinParent'] ?? null); |
| 48 | $entity->setFeaturedPosition($input_data['featuredPosition'] ?? 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 | } |