Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LinkEndpointTrait | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
getEntityData | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
updateEntityWithData | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getEntityById | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Olz\Service\Endpoints; |
4 | |
5 | use Olz\Entity\Service\Link; |
6 | use Olz\Utils\WithUtilsTrait; |
7 | use PhpTypeScriptApi\HttpError; |
8 | |
9 | /** |
10 | * @phpstan-type OlzLinkId int |
11 | * @phpstan-type OlzLinkData array{ |
12 | * position?: ?int, |
13 | * name: non-empty-string, |
14 | * url: non-empty-string, |
15 | * } |
16 | */ |
17 | trait LinkEndpointTrait { |
18 | use WithUtilsTrait; |
19 | |
20 | /** @return OlzLinkData */ |
21 | public function getEntityData(Link $entity): array { |
22 | return [ |
23 | 'name' => $entity->getName() ?: '-', |
24 | 'position' => $entity->getPosition(), |
25 | 'url' => $entity->getUrl() ?: '-', |
26 | ]; |
27 | } |
28 | |
29 | /** @param OlzLinkData $input_data */ |
30 | public function updateEntityWithData(Link $entity, array $input_data): void { |
31 | $entity->setName($input_data['name']); |
32 | $entity->setPosition($input_data['position'] ?? 0); |
33 | $entity->setUrl($input_data['url']); |
34 | } |
35 | |
36 | protected function getEntityById(int $id): Link { |
37 | $repo = $this->entityManager()->getRepository(Link::class); |
38 | $entity = $repo->findOneBy(['id' => $id]); |
39 | if (!$entity) { |
40 | throw new HttpError(404, "Nicht gefunden."); |
41 | } |
42 | return $entity; |
43 | } |
44 | } |