Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
SnippetEndpointTrait | |
100.00% |
24 / 24 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
getEntityData | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
updateEntityWithData | |
100.00% |
1 / 1 |
|
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% |
13 / 13 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Olz\Snippets\Endpoints; |
4 | |
5 | use Olz\Entity\Snippets\Snippet; |
6 | use Olz\Utils\WithUtilsTrait; |
7 | use PhpTypeScriptApi\HttpError; |
8 | |
9 | /** |
10 | * @phpstan-type OlzSnippetId int |
11 | * @phpstan-type OlzSnippetData array{ |
12 | * text: string, |
13 | * imageIds: array<non-empty-string>, |
14 | * fileIds: array<non-empty-string>, |
15 | * } |
16 | */ |
17 | trait SnippetEndpointTrait { |
18 | use WithUtilsTrait; |
19 | |
20 | /** @return OlzSnippetData */ |
21 | public function getEntityData(Snippet $entity): array { |
22 | return [ |
23 | 'text' => $entity->getText() ?? '', |
24 | 'imageIds' => $entity->getStoredImageUploadIds(), |
25 | 'fileIds' => $entity->getStoredFileUploadIds(), |
26 | ]; |
27 | } |
28 | |
29 | /** @param OlzSnippetData $input_data */ |
30 | public function updateEntityWithData(Snippet $entity, array $input_data): void { |
31 | $entity->setText($input_data['text']); |
32 | } |
33 | |
34 | /** @param OlzSnippetData $input_data */ |
35 | public function persistUploads(Snippet $entity, array $input_data): void { |
36 | $this->persistOlzImages($entity, $input_data['imageIds']); |
37 | $this->persistOlzFiles($entity, $input_data['fileIds']); |
38 | } |
39 | |
40 | public function editUploads(Snippet $entity): void { |
41 | $image_ids = $this->uploadUtils()->getStoredUploadIds("{$entity->getImagesPathForStorage()}img/"); |
42 | $this->editOlzImages($entity, $image_ids); |
43 | $this->editOlzFiles($entity); |
44 | } |
45 | |
46 | protected function getEntityById(int $id): Snippet { |
47 | $repo = $this->entityManager()->getRepository(Snippet::class); |
48 | $entity = $repo->findOneBy(['id' => $id]); |
49 | if (!$entity) { |
50 | $has_access = $this->authUtils()->hasPermission("snippet_{$id}"); |
51 | if (!$has_access) { |
52 | throw new HttpError(404, "Nicht gefunden."); |
53 | } |
54 | $entity = new Snippet(); |
55 | $this->entityUtils()->createOlzEntity($entity, ['onOff' => true]); |
56 | $entity->setId($id); |
57 | $entity->setText(''); |
58 | $this->entityManager()->persist($entity); |
59 | $this->entityManager()->flush(); |
60 | } |
61 | return $entity; |
62 | } |
63 | } |