Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
TerminLocationEndpointTrait
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 getEntityData
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 updateEntityWithData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 persistUploads
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 editUploads
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntityById
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Olz\Termine\Endpoints;
4
5use Olz\Entity\Termine\TerminLocation;
6use Olz\Utils\MapUtils;
7use Olz\Utils\WithUtilsTrait;
8use PhpTypeScriptApi\HttpError;
9
10/**
11 * @phpstan-type OlzTerminLocationId int
12 *
13 * @phpstan-import-type OlzLocationCoordinates from MapUtils
14 *
15 * @phpstan-type OlzTerminLocationData array{
16 *   name: non-empty-string,
17 *   details: string,
18 *   location: OlzLocationCoordinates,
19 *   imageIds: array<non-empty-string>,
20 * }
21 */
22trait TerminLocationEndpointTrait {
23    use WithUtilsTrait;
24
25    /** @return OlzTerminLocationData */
26    public function getEntityData(TerminLocation $entity): array {
27        $valid_image_ids = $this->uploadUtils()->getValidUploadIds($entity->getImageIds());
28
29        return [
30            'name' => $entity->getName() ?: '-',
31            'details' => $entity->getDetails() ?? '',
32            'location' => [
33                'latitude' => $entity->getLatitude(),
34                'longitude' => $entity->getLongitude(),
35            ],
36            'imageIds' => $valid_image_ids,
37        ];
38    }
39
40    /** @param OlzTerminLocationData $input_data */
41    public function updateEntityWithData(TerminLocation $entity, array $input_data): void {
42        $valid_image_ids = $this->uploadUtils()->getValidUploadIds($input_data['imageIds']);
43
44        $entity->setName($input_data['name']);
45        $entity->setDetails($input_data['details']);
46        $entity->setLatitude($input_data['location']['latitude']);
47        $entity->setLongitude($input_data['location']['longitude']);
48        $entity->setImageIds($valid_image_ids);
49    }
50
51    public function persistUploads(TerminLocation $entity): void {
52        $this->persistOlzImages($entity, $entity->getImageIds());
53    }
54
55    public function editUploads(TerminLocation $entity): void {
56        $this->editOlzImages($entity, $entity->getImageIds());
57    }
58
59    protected function getEntityById(int $id): TerminLocation {
60        $repo = $this->entityManager()->getRepository(TerminLocation::class);
61        $entity = $repo->findOneBy(['id' => $id]);
62        if (!$entity) {
63            throw new HttpError(404, "Nicht gefunden.");
64        }
65        return $entity;
66    }
67}