Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| KarteEndpointTrait | |
0.00% |
0 / 49 |
|
0.00% |
0 / 6 |
552 | |
0.00% |
0 / 1 |
| getEntityData | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
90 | |||
| updateEntityWithData | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| persistUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| editUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getEntityById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getKindForApi | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Karten\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Karten\Karte; |
| 6 | use Olz\Utils\WithUtilsTrait; |
| 7 | use PhpTypeScriptApi\HttpError; |
| 8 | |
| 9 | /** |
| 10 | * Note: `latitude` may be from -90.0 to 90.0, `longitude` from -180.0 to 180.0. |
| 11 | * |
| 12 | * @phpstan-type OlzKarteId int |
| 13 | * @phpstan-type OlzKarteData array{ |
| 14 | * kartennr?: ?int, |
| 15 | * name: non-empty-string, |
| 16 | * latitude?: ?float, |
| 17 | * longitude?: ?float, |
| 18 | * year?: ?int, |
| 19 | * scale?: ?non-empty-string, |
| 20 | * place?: ?non-empty-string, |
| 21 | * zoom?: ?int, |
| 22 | * kind?: ?OlzKarteKind, |
| 23 | * previewImageId?: ?non-empty-string, |
| 24 | * } |
| 25 | * @phpstan-type OlzKarteKind 'ol'|'stadt'|'scool' |
| 26 | */ |
| 27 | trait KarteEndpointTrait { |
| 28 | use WithUtilsTrait; |
| 29 | |
| 30 | /** @return OlzKarteData */ |
| 31 | public function getEntityData(Karte $entity): array { |
| 32 | $name = $entity->getName(); |
| 33 | $latitude = $entity->getLatitude(); |
| 34 | $longitude = $entity->getLongitude(); |
| 35 | $year = $entity->getYear(); |
| 36 | $scale = $entity->getScale(); |
| 37 | $place = $entity->getPlace(); |
| 38 | $zoom = $entity->getZoom(); |
| 39 | $preview_image_id = $entity->getPreviewImageId(); |
| 40 | |
| 41 | return [ |
| 42 | 'kartennr' => $entity->getKartenNr() ?? null, |
| 43 | 'name' => $name ? $name : '-', |
| 44 | 'latitude' => $latitude ? floatval($latitude) : null, |
| 45 | 'longitude' => $longitude ? floatval($longitude) : null, |
| 46 | 'year' => $year ? intval($year) : null, |
| 47 | 'scale' => $scale ? $scale : null, |
| 48 | 'place' => $place ? $place : null, |
| 49 | 'zoom' => $zoom ? intval($zoom) : null, |
| 50 | 'kind' => $this->getKindForApi($entity), |
| 51 | 'previewImageId' => $preview_image_id ? $preview_image_id : null, |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | /** @param OlzKarteData $input_data */ |
| 56 | public function updateEntityWithData(Karte $entity, array $input_data): void { |
| 57 | $year = $input_data['year'] ?? null; |
| 58 | $valid_year = $year ? strval($year) : null; |
| 59 | $preview_image_id = $input_data['previewImageId'] ?? null; |
| 60 | $valid_preview_image_id = $this->uploadUtils()->getValidUploadId($preview_image_id); |
| 61 | |
| 62 | $entity->setKartenNr($input_data['kartennr'] ?? null); |
| 63 | $entity->setName($input_data['name']); |
| 64 | $entity->setLatitude($input_data['latitude'] ?? null); |
| 65 | $entity->setLongitude($input_data['longitude'] ?? null); |
| 66 | $entity->setYear($valid_year); |
| 67 | $entity->setScale($input_data['scale'] ?? null); |
| 68 | $entity->setPlace($input_data['place'] ?? null); |
| 69 | $entity->setZoom($input_data['zoom'] ?? null); |
| 70 | $entity->setKind($input_data['kind'] ?? null); |
| 71 | $entity->setPreviewImageId($valid_preview_image_id); |
| 72 | } |
| 73 | |
| 74 | /** @param OlzKarteData $input_data */ |
| 75 | public function persistUploads(Karte $entity, array $input_data): void { |
| 76 | if ($entity->getPreviewImageId()) { |
| 77 | $this->persistOlzImages($entity, [$entity->getPreviewImageId()]); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public function editUploads(Karte $entity): void { |
| 82 | if ($entity->getPreviewImageId() !== null) { |
| 83 | $this->editOlzImages($entity, [$entity->getPreviewImageId()]); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | protected function getEntityById(int $id): Karte { |
| 88 | $repo = $this->entityManager()->getRepository(Karte::class); |
| 89 | $entity = $repo->findOneBy(['id' => $id]); |
| 90 | if (!$entity) { |
| 91 | throw new HttpError(404, "Nicht gefunden."); |
| 92 | } |
| 93 | return $entity; |
| 94 | } |
| 95 | |
| 96 | // --- |
| 97 | |
| 98 | /** @return ?OlzKarteKind */ |
| 99 | protected function getKindForApi(Karte $entity): ?string { |
| 100 | switch ($entity->getKind()) { |
| 101 | case 'ol': return 'ol'; |
| 102 | case 'stadt': return 'stadt'; |
| 103 | case 'scool': return 'scool'; |
| 104 | case null: return null; |
| 105 | default: throw new \Exception("Unknown karten kind: {$entity->getKind()} ({$entity})"); |
| 106 | } |
| 107 | } |
| 108 | } |